1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

tic tac toe

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

  1. plike922

    plike922 New Member

    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

    What help do you want?
     
  3. sun_kangane

    sun_kangane New Member

    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

Share This Page