Tic Tac Toe!

This is a discussion on Tic Tac Toe! within the C Programming forums, part of the General Programming Boards category; I am having some issues with a TIC TAC TOE program I'm working on for a challenge. It works fine ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    8

    Tic Tac Toe!

    I am having some issues with a TIC TAC TOE program I'm working on for a challenge. It works fine except if I try to play as 'O' it goes on with me playing as 'X' anyway, and when a game should theoretically end in a draw it just keeps asking me to enter a valid place to put 'X' and I can't, because all the spaces are taken.

    I'm thinking the first problem has something to do with the 'if' statement in my main function. I don't understand why the game never ends in a draw. I tried to make it so that if the main game loop ran more than 9 times (the maximum number of moves) it would default to a draw, but something is wrong.

    Any help would be immensely appreciated. These problems with it are really bothering me.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void srand ();
    void printBoard(char ary[3][3]);
    void cAction(char ary[3][3], int p);
    void pAction(char ary[3][3], int p);
    int chkGame(char ary[3][3], int p);
    
    int main(void)
    {
    	char board[3][3] = {
    							{'1','2','3'},
    							{'4','5','6'},
    							{'7','8','9'}
                           };
    	int turn = 0;
    	int player = 0;
    	int winner = 0;
    	int i = 0;
    	
    	printf("\nName: Ryan Slater\n");
    	printf("\nLet's play TIC-TAC-TOE!\n");
    	do
    	{
    		printf("\nDo you want to be X or O (X moves first)?\n");
    		printf("Enter 1 for X and 2 for O: ");
    		scanf("%d", &player);
    	} while (player != 1 && player != 2);
    	
    	for(i = 0; i < 9 && winner == 0; i++)
    	{
    		if (player = 1)
    		{
    			printBoard(board);
    			pAction(board, player);
    			winner = chkGame(board, player);
    			cAction(board, player);
    			winner = chkGame(board, player);
    		}
    		else
    		{
    			cAction(board, player);
    			winner = chkGame(board, player);
    			printBoard(board);
    			pAction(board, player);
    			winner = chkGame(board, player);
    		}
    	}
    	
    	printf("\nIt's a cat's game!\n");
    	printf("\nThe game has ended in a draw.\n");
    	return 0;
    		
    }
    
    void printBoard(char ary[3][3])
    {
    	printf("\n");
        printf("    %c | %c | %c\n", ary[0][0], ary[0][1], ary[0][2]);
        printf("   ---|---|---\n");
        printf("    %c | %c | %c\n", ary[1][0], ary[1][1], ary[1][2]);
        printf("   ---|---|---\n");
        printf("    %c | %c | %c\n\n", ary[2][0], ary[2][1], ary[2][2]);
    }
    
    void pAction(char ary[3][3], int p)
    {
    	int spot;
    	int row;
    	int col;
    	
    	do
    	{
    		printf("Enter the number of an available space, you are %c: ", (p == 1) ? 'X' : 'O');
    		scanf("%d", &spot);
    		
    		row = --spot / 3;
    		col = spot % 3;
    	} while (spot < 0 || spot > 9 || ary[row][col] > '9');
    	
    	ary[row][col] = (p == 1) ? 'X' : 'O';
    }
    
    void cAction (char ary[3][3], int p)
    {
    	int x;
    	int spot;
    	int row;
    	int col;
    	srand(time(NULL));
    	
    	do
    	{
    		x = rand();
    		x = (x % 9) + 1;
    		
    		row = --x / 3;
    		col = x % 3;
    	} while (x < 0 || x > 9 || ary[row][col] > '9');
    
    	printf("The computer picked space %c\n", ary[row][col]);
    	ary[row][col] = (p == 1) ? 'O' : 'X';
    }
    
    int chkGame (char ary[3][3], int p)
    {
    	if (p = 1)
    	{
    		if ((ary[0][0] == ary[1][1] && ary[0][0] == ary[2][2] && ary[0][0] == 'X') ||
                (ary[0][2] == ary[1][1] && ary[0][2] == ary[2][0] && ary[0][2] == 'X') ||
    			(ary[0][0] == ary[0][1] && ary[0][0] == ary[0][2] && ary[0][0] == 'X') ||
    			(ary[1][0] == ary[1][1] && ary[1][0] == ary[1][2] && ary[1][0] == 'X') ||
    			(ary[2][0] == ary[2][1] && ary[2][0] == ary[2][2] && ary[2][0] == 'X') ||
    			(ary[0][0] == ary[1][0] && ary[0][0] == ary[2][0] && ary[0][0] == 'X') ||
    			(ary[0][1] == ary[1][1] && ary[0][1] == ary[2][1] && ary[0][1] == 'X') ||
    			(ary[0][2] == ary[1][2] && ary[0][2] == ary[2][2] && ary[0][2] == 'X'))
    		{
    			printf("\nYou Win!\n");
    			printf("A WinRAR is YOU!!!\n");
    			printBoard(ary);
    			
    			exit(0);
    		}
    		else if((ary[0][0] == ary[1][1] && ary[0][0] == ary[2][2]) ||
    				(ary[0][2] == ary[1][1] && ary[0][2] == ary[2][0]) ||
    				(ary[0][0] == ary[0][1] && ary[0][0] == ary[0][2]) ||
    				(ary[1][0] == ary[1][1] && ary[1][0] == ary[1][2]) ||
    				(ary[2][0] == ary[2][1] && ary[2][0] == ary[2][2]) ||
    				(ary[0][0] == ary[1][0] && ary[0][0] == ary[2][0]) ||
    				(ary[0][1] == ary[1][1] && ary[0][1] == ary[2][1]) ||
    				(ary[0][2] == ary[1][2] && ary[0][2] == ary[2][2]))
    		{
    			printf("\nYou Lose!\n");
    			printf("You are a FAILURE!!!\n");
    			printBoard(ary);
    			
    			exit(0);
    		}
    	}	
    	else if (p = 2)
    	{
    		if ((ary[0][0] == ary[1][1] && ary[0][0] == ary[2][2] && ary[0][0] == 'O') ||
                (ary[0][2] == ary[1][1] && ary[0][2] == ary[2][0] && ary[0][2] == 'O') ||
    			(ary[0][0] == ary[0][1] && ary[0][0] == ary[0][2] && ary[0][0] == 'O') ||
    			(ary[1][0] == ary[1][1] && ary[1][0] == ary[1][2] && ary[1][0] == 'O') ||
    			(ary[2][0] == ary[2][1] && ary[2][0] == ary[2][2] && ary[2][0] == 'O') ||
    			(ary[0][0] == ary[1][0] && ary[0][0] == ary[2][0] && ary[0][0] == 'O') ||
    			(ary[0][1] == ary[1][1] && ary[0][1] == ary[2][1] && ary[0][1] == 'O') ||
    			(ary[0][2] == ary[1][2] && ary[0][2] == ary[2][2] && ary[0][2] == 'O'))
    		{
    			printf("\nYou Win!\n");
    			printf("A WinRAR is YOU!!!\n");
    			printBoard(ary);
    			
    			exit(0);
    		}
    		else if((ary[0][0] == ary[1][1] && ary[0][0] == ary[2][2]) ||
    				(ary[0][2] == ary[1][1] && ary[0][2] == ary[2][0]) ||
    				(ary[0][0] == ary[0][1] && ary[0][0] == ary[0][2]) ||
    				(ary[1][0] == ary[1][1] && ary[1][0] == ary[1][2]) ||
    				(ary[2][0] == ary[2][1] && ary[2][0] == ary[2][2]) ||
    				(ary[0][0] == ary[1][0] && ary[0][0] == ary[2][0]) ||
    				(ary[0][1] == ary[1][1] && ary[0][1] == ary[2][1]) ||
    				(ary[0][2] == ary[1][2] && ary[0][2] == ary[2][2]))
    		{
    			printf("\nYou Lose!\n");
    			printf("You are a FAILURE!!!\n");
    			printBoard(ary);
    
    			exit(0);
    		}
    	} 
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Just in a couple of places shown in red where assignment should be replaced by the test for equality ie
    To end the game in a draw maybe a better hashing function is needed.
    Quote Originally Posted by Sinensis View Post
    I am having some issues with a TIC TAC TOE program I'm working on for a challenge. It works fine except if I try to play as 'O' it goes on with me playing as 'X' anyway, and when a game should theoretically end in a draw it just keeps asking me to enter a valid place to put 'X' and I can't, because all the spaces are taken.

    I'm thinking the first problem has something to do with the 'if' statement in my main function. I don't understand why the game never ends in a draw. I tried to make it so that if the main game loop ran more than 9 times (the maximum number of moves) it would default to a draw, but something is wrong.

    Any help would be immensely appreciated. These problems with it are really bothering me.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void srand ();
    void printBoard(char ary[3][3]);
    void cAction(char ary[3][3], int p);
    void pAction(char ary[3][3], int p);
    int chkGame(char ary[3][3], int p);
    
    int main(void)
    {
    	char board[3][3] = {
    							{'1','2','3'},
    							{'4','5','6'},
    							{'7','8','9'}
                           };
    	int turn = 0;
    	int player = 0;
    	int winner = 0;
    	int i = 0;
    	
    	printf("\nName: Ryan Slater\n");
    	printf("\nLet's play TIC-TAC-TOE!\n");
    	do
    	{
    		printf("\nDo you want to be X or O (X moves first)?\n");
    		printf("Enter 1 for X and 2 for O: ");
    		scanf("&#37;d", &player);
    	} while (player != 1 && player != 2);
    	
    	for(i = 0; i < 9 && winner == 0; i++)
    	{
    		if (player == 1 || player == 2)
    		{
    			printBoard(board);
    			pAction(board, player);
    			winner = chkGame(board, player);
    			cAction(board, player);
    			winner = chkGame(board, player);
    		}
    		else
    		{
    			cAction(board, player);
    			winner = chkGame(board, player);
    			printBoard(board);
    			pAction(board, player);
    			winner = chkGame(board, player);
    		}
    	}
    	
    	printf("\nIt's a cat's game!\n");
    	printf("\nThe game has ended in a draw.\n");
    	return 0;
    		
    }
    
    void printBoard(char ary[3][3])
    {
    	printf("\n");
        printf("    %c | %c | %c\n", ary[0][0], ary[0][1], ary[0][2]);
        printf("   ---|---|---\n");
        printf("    %c | %c | %c\n", ary[1][0], ary[1][1], ary[1][2]);
        printf("   ---|---|---\n");
        printf("    %c | %c | %c\n\n", ary[2][0], ary[2][1], ary[2][2]);
    }
    
    void pAction(char ary[3][3], int p)
    {
    	int spot;
    	int row;
    	int col;
    	
    	do
    	{
    		printf("Enter the number of an available space, you are %c: ", (p == 1) ? 'X' : 'O');
    		scanf("%d", &spot);
    		
    		row = --spot / 3;
    		col = spot % 3;
    	} while (spot < 0 || spot > 9 || ary[row][col] > '9');
    	
    	ary[row][col] = (p == 1) ? 'X' : 'O';
    }
    
    void cAction (char ary[3][3], int p)
    {
    	int x;
    	int spot;
    	int row;
    	int col;
    	srand(time(NULL));
    	
    	do
    	{
    		x = rand();
    		x = (x % 9) + 1;
    		
    		row = --x / 3;
    		col = x % 3;
    	} while (x < 0 || x > 9 || ary[row][col] > '9');
    
    	printf("The computer picked space %c\n", ary[row][col]);
    	ary[row][col] = (p == 1) ? 'O' : 'X';
    }
    
    int chkGame (char ary[3][3], int p)
    {
    	if (p == 1)
    	{
    		if ((ary[0][0] == ary[1][1] && ary[0][0] == ary[2][2] && ary[0][0] == 'X') ||
                (ary[0][2] == ary[1][1] && ary[0][2] == ary[2][0] && ary[0][2] == 'X') ||
    			(ary[0][0] == ary[0][1] && ary[0][0] == ary[0][2] && ary[0][0] == 'X') ||
    			(ary[1][0] == ary[1][1] && ary[1][0] == ary[1][2] && ary[1][0] == 'X') ||
    			(ary[2][0] == ary[2][1] && ary[2][0] == ary[2][2] && ary[2][0] == 'X') ||
    			(ary[0][0] == ary[1][0] && ary[0][0] == ary[2][0] && ary[0][0] == 'X') ||
    			(ary[0][1] == ary[1][1] && ary[0][1] == ary[2][1] && ary[0][1] == 'X') ||
    			(ary[0][2] == ary[1][2] && ary[0][2] == ary[2][2] && ary[0][2] == 'X'))
    		{
    			printf("\nYou Win!\n");
    			printf("A WinRAR is YOU!!!\n");
    			printBoard(ary);
    			
    			exit(0);
    		}
    		else if((ary[0][0] == ary[1][1] && ary[0][0] == ary[2][2]) ||
    				(ary[0][2] == ary[1][1] && ary[0][2] == ary[2][0]) ||
    				(ary[0][0] == ary[0][1] && ary[0][0] == ary[0][2]) ||
    				(ary[1][0] == ary[1][1] && ary[1][0] == ary[1][2]) ||
    				(ary[2][0] == ary[2][1] && ary[2][0] == ary[2][2]) ||
    				(ary[0][0] == ary[1][0] && ary[0][0] == ary[2][0]) ||
    				(ary[0][1] == ary[1][1] && ary[0][1] == ary[2][1]) ||
    				(ary[0][2] == ary[1][2] && ary[0][2] == ary[2][2]))
    		{
    			printf("\nYou Lose!\n");
    			printf("You are a FAILURE!!!\n");
    			printBoard(ary);
    			
    			exit(0);
    		}
    	}	
    	else if (p == 2)
    	{
    		if ((ary[0][0] == ary[1][1] && ary[0][0] == ary[2][2] && ary[0][0] == 'O') ||
                (ary[0][2] == ary[1][1] && ary[0][2] == ary[2][0] && ary[0][2] == 'O') ||
    			(ary[0][0] == ary[0][1] && ary[0][0] == ary[0][2] && ary[0][0] == 'O') ||
    			(ary[1][0] == ary[1][1] && ary[1][0] == ary[1][2] && ary[1][0] == 'O') ||
    			(ary[2][0] == ary[2][1] && ary[2][0] == ary[2][2] && ary[2][0] == 'O') ||
    			(ary[0][0] == ary[1][0] && ary[0][0] == ary[2][0] && ary[0][0] == 'O') ||
    			(ary[0][1] == ary[1][1] && ary[0][1] == ary[2][1] && ary[0][1] == 'O') ||
    			(ary[0][2] == ary[1][2] && ary[0][2] == ary[2][2] && ary[0][2] == 'O'))
    		{
    			printf("\nYou Win!\n");
    			printf("A WinRAR is YOU!!!\n");
    			printBoard(ary);
    			
    			exit(0);
    		}
    		else if((ary[0][0] == ary[1][1] && ary[0][0] == ary[2][2]) ||
    				(ary[0][2] == ary[1][1] && ary[0][2] == ary[2][0]) ||
    				(ary[0][0] == ary[0][1] && ary[0][0] == ary[0][2]) ||
    				(ary[1][0] == ary[1][1] && ary[1][0] == ary[1][2]) ||
    				(ary[2][0] == ary[2][1] && ary[2][0] == ary[2][2]) ||
    				(ary[0][0] == ary[1][0] && ary[0][0] == ary[2][0]) ||
    				(ary[0][1] == ary[1][1] && ary[0][1] == ary[2][1]) ||
    				(ary[0][2] == ary[1][2] && ary[0][2] == ary[2][2]))
    		{
    			printf("\nYou Lose!\n");
    			printf("You are a FAILURE!!!\n");
    			printBoard(ary);
    
    			exit(0);
    		}
    	} 
    	
    	return 0;
    }
    Last edited by itCbitC; 10-21-2008 at 02:37 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This refers to the corrected version of the TTT code, not the original version.

    The compiler tells me that the variable "turn" is never used.

    When I choose the last available square, it begins looping forever in this function, inside the do while loop:

    Code:
    void cAction (char ary[3][3], int p)
    {
    	int x;
    	int spot;
    	int row;
    	int col;
    	srand(time(NULL));
    	
    	do
    	{
    		x = rand();
    		x = (x &#37; 9) + 1;
    		
    		row = --x / 3;
    		col = x % 3;
    	} while (x < 0 || x > 9 || ary[row][col] > '9');
    
    	printf("The computer picked space %c\n", ary[row][col]);
    	ary[row][col] = (p == 1) ? 'O' : 'X';
    }
    You need to include some logic regarding either the number of turns or a square being still empty, etc., either before (preferably), or inside, the red do while loop.
    Last edited by Adak; 10-21-2008 at 04:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  2. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 11:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21