1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

tic tac toe

Discussion in 'C' started by plike922, Oct 3, 2008.

  1. plike922

    plike922 New Member

    Joined:
    Oct 3, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Please help
    Code:
    #include <stdio.h>
    #include "strlib.h"
    #include "simpio.h"
    #include "genlib.h"
    #include "string.h"
    
    #define size 9
    
    void main()
    {
    	int rand, x = 0, i, player = 0, turn = 0, row = 0, column = 0, line = 0, winner = 0, used[size], check;
    	bool space;
    	char board[size][size] = { {'1','2','3'}, {'4','5','6'},{'7','8','9'}};
    
    	printf("Welcome to Tic Tac Toe: Player 1 = you-Player 2 = computer\n");
    
    	for(check=0; check<10; check++)
    			{
    				used[check] = 1;
    			}
    	for( i = 1; i<size && winner==0; i++)
    	{
     
    	//-----------------------------Display board--------------------------------//
    	printf("\n\n");
    	printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
    	printf("-----------\n");
    	printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
    	printf("-----------\n");
    	printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
    	//--------------------------------------------------------------------------//
    
    	player = i%2 + 1;
      
    	if(player == 1)
    	{
    	printf("\nPlayer 1, place your X: ");
    	turn = GetInteger();
    	used[x] = turn;
    	x++;
    	}
    	else
    	{
    		if(x==0)
    		{
    			turn = 1;
    			used[x] = 1;
    			x++;
    		}
    		else
    		{
    		printf("\nPlayer 2, place your O: ");
    		space = true;
    			for(rand=9; rand>1 && space != true; rand--)
    			{
    				space = true;
    				for(check=0; check<10; check++)
    				{
    					if(used[check] == rand) 
    					{
    						space = false;
    					}
    					else;
    				}
    				if(space == true)
    				{ 
    					rand = -1;
    				}
    				else;
    			}
    		turn = rand;
    		used[x] = turn;
    		x++;
    		}
    		printf("%d", turn);
    	}
    
    	row = --turn/3;
    	column = turn%3;
    	while(turn<0 || turn>size || board[row][column]>'9');
    	board[row][column] = (player == 1) ? 'X' : 'O';
    
    
    	if((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[0][2] == board[2][0]))
    		{
    			winner = player;
    		}
    	else
    		{
    			for(line = 0; line <= 2; line ++) if((board[line][0] == board[line][1] && board[line][0] == board[line][2]) || (board[0][line] == board[1][line] && board[0][line] == board[2][line]))winner = player;
    		}
    	}
    	//-----------------------------Display board--------------------------------//
    	printf("\n\n");
    	printf(" %c | %c | %c\n", board[0][0], board[0][1], board[0][2]);
    	printf("---+---+---\n");
    	printf(" %c | %c | %c\n", board[1][0], board[1][1], board[1][2]);
    	printf("---+---+---\n");
    	printf(" %c | %c | %c\n", board[2][0], board[2][1], board[2][2]);
    	//--------------------------------------------------------------------------//
    
        if(winner == 0)
    	{
          printf("\nIt is a draw\n");
    	}
    	else
    	{
          printf("\nCongratulations, player %d!\n", winner);
    	}
    
       system("pause");
    }
     
    Last edited by a moderator: Oct 3, 2008
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What help do you want?
     
  3. sun_kangane

    sun_kangane New Member

    Joined:
    Mar 20, 2007
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    what problem u have..................

    i have done it by some other way...................

    search for TIC TAC TO.CPP
     
  4. sun_kangane

    sun_kangane New Member

    Joined:
    Mar 20, 2007
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0

Share This Page