Thread: Tic Tac Toe in Two Dimensional Array Help!

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Tic Tac Toe in Two Dimensional Array Help!

    Hi,
    I'm having some trouble with converting a 1 dimensional array program into a two dimensional array program. Actually I'm getting pretty hopeless. I keep getting incompatibility errors in the verify selection. Hopefully someone can help... Here is my code:
    Code:
    #include <stdio.h>
    
    // Function Prototypes
    void displayBoard();
    int verifySelection(int, int);
    void checkForWin();
    
    // Global Variables
    char board[3][3];
    char cWhoWon = ' ';
    int iCurrentPlayer = 0;
    
    // Main Function
    main() 
    {
    
    	int x, y;
    	int iSquareNum = 0;
    
    	for ( x = 0; x <= 2; x++ )
    		
    		for ( y = 0; y <= 2; y++ )
    			board[x][y] = ' ';
    		
    	displayBoard();
    	
    	while ( cWhoWon == ' ') 
    	{
    	
    		printf("\n%c\n", cWhoWon);
    		
    		if ( iCurrentPlayer == 1 || iCurrentPlayer == 0 ) 
    		{
    		
    			printf("\nPLAYER X\n");
    			printf("Enter an available square number (1-9): ");
    			scanf("%d", &iSquareNum);
    			
    			if ( verifySelection(iSquareNum, iCurrentPlayer) == 1 )
    				iCurrentPlayer = 1;
    			else
    				iCurrentPlayer = 2;
    		}
    		
    		else
    		{
    		
    			printf("\nPLAYER O\n");
    			printf("Enter an available square number (1-9): ");
    			scanf("%d", &iSquareNum); 
    			
    			if ( verifySelection(iSquareNum, iCurrentPlayer) == 1 )
    				iCurrentPlayer = 2;
    			else
    				iCurrentPlayer = 1;
    // End If			
    		}
    		
    		displayBoard();
    		checkForWin();
    		
    // End While Loop
    	}
    	
    // End Main Function
    }
    
    // Begin Function Definition
    void displayBoard() 
    { 
    
    	system("cls");
    	printf("\n\t|\t|\n");
    	printf("\t|\t|\n");
    	printf("%c\t|%c\t|%c\n", board[0][0], board[0][1], board[0][2]);
    	printf("--------|-------|--------\n");
    	printf("\t|\t|\n");
    	printf("%c\t|%c\t|%c\n", board[1][0], board[1][1], board[1][2]);
    	printf("--------|-------|--------\n");
    	printf("\t|\t|\n");
    	printf("%c\t|%c\t|%c\n", board[2][0], board[2][1], board[2][2]);
    	printf("\t|\t|\n");
    	
    // End Function Definition
    }
    
    // Begin Function Definition
    int verifySelection(int iSquareNum, int iCurrentPlayer) 
    { 
    
    	if ( board[iSquareNum - 1] == ' ' && (iCurrentPlayer == 1 || iCurrentPlayer == 0) ) 
    	{
    		board[iSquareNum - 1] = 'X';
    		return 0;
    	}
    	
    	else if ( board[iSquareNum - 1] == ' ' && iCurrentPlayer == 2 ) 
    	{
    		board[iSquareNum - 1] = 'O';
    		return 0;
    	}
    	
    	else
    		return 1;
    // End Function Definition
    } 
    
    // Begin Function Definition
    void checkForWin() 
    {
    	int catTotal;
    	int x, y;
    	
    
    // check each row for a winner
    if (board[0][0] && board[0][1] && board[0][2] == 'X')
    	cWhoWon = 'X';
    
    // check row 2 for winner
    else if (board[1][0] && board[1][1] && board[1][2] == 'X')
    	cWhoWon = 'X';
    
    // check row 3 for winner
    else if (board[2][0] && board[2][1] && board[2][2] == 'X')
    	cWhoWon = 'X';
    
    // check column 1 for winner
    else if (board[0][0] && board[1][0] && board[2][0] == 'X')
    	cWhoWon = 'X';
    
    // check column 2 for winner
    else if (board[0][1] && board[1][1] && board[2][1] == 'X')
    	cWhoWon = 'X';
    
    // check column 3 for winner
    else if (board[0][2] && board[1][2] && board[2][2] == 'X')
    	cWhoWon = 'X';
    
    //check each diagnoal for a winner
    else if (board[0][0] && board[1][1] && board[2][2] == 'X')
    	cWhoWon = 'X';
    
    // check second diagonal for winner
    else if (board[0][2] && board[1][1] && board[2][0] == 'X')
    	cWhoWon = 'X';
    
    // check each row for a winner
    else if (board[0][0] && board[0][1] && board[0][2] == 'O')
    	cWhoWon = 'O';
    
    // check row 2 for winner
    else if (board[1][0] && board[1][1] && board[1][2] == 'O')
    	cWhoWon = 'O';
    
    // check row 3 for winner
    else if (board[2][0] && board[2][1] && board[2][2] == 'O')
    	cWhoWon = 'O';
    
    // check column 1 for winner
    else if (board[0][0] && board[1][0] && board[2][0] == 'O')
    	cWhoWon = 'O';
    
    // check column 2 for winner
    else if (board[0][1] && board[1][1] && board[2][1] == 'O')
    	cWhoWon = 'O';
    
    // check column 3 for winner
    else if (board[0][2] && board[1][2] && board[2][2] == 'O')
    	cWhoWon = 'O';
    
    
    // check each diagnoal for a winner
    else if (board[0][0] && board[1][1] && board[2][2] == 'O')
    	cWhoWon = 'O';
    
    // check second diagonal for winner
    else if (board[0][2] && board[1][1] && board[2][0] == 'O')
    	cWhoWon = 'O';
    
    		
    	if (cWhoWon == 'X') 
    	{
    		printf("\nX Wins!\n");
    		return;
    	} 
    	
    	if (cWhoWon == 'O') 
    	{
    		printf("\nO Wins!\n");
    		return;
    	} 
    	
    // Check for CAT or Tie Game
    	for ( x = 0; x < 9; x++ )
    	
    		for ( y = 0; y < 9; y++ )
    		{
    			if ( board[x][y] != ' ')
    				catTotal += 1;
    			
    // End For Loop
    	}
    	
    	if ( catTotal == 9 )
    	{
    		cWhoWon = 'C';
    		printf("\nCAT Game!\n");
    		return;
    		
    // End If
    	}
    	
    	
    // End Function Definition
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly are you having a problem with?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    I can't figure out how to change the verifySelection to be compatible with the displayBoard. I get compatibility type errors on two lines:
    board[iSquareNum - 1] = 'X';
    &
    board[iSquareNum - 1] = 'O';

    The user is entering 1 thru 9 for boards 0 thru 9 so I need to subtract 1 from whatever the user selects. But I can't figure out how to write this code.
    This section:
    Code:
    // Begin Function Definition
    void displayBoard() 
    { 
    
    	system("cls");
    	printf("\n\t|\t|\n");
    	printf("\t|\t|\n");
    	printf("%c\t|%c\t|%c\n", board[0][0], board[0][1], board[0][2]);
    	printf("--------|-------|--------\n");
    	printf("\t|\t|\n");
    	printf("%c\t|%c\t|%c\n", board[1][0], board[1][1], board[1][2]);
    	printf("--------|-------|--------\n");
    	printf("\t|\t|\n");
    	printf("%c\t|%c\t|%c\n", board[2][0], board[2][1], board[2][2]);
    	printf("\t|\t|\n");
    	
    // End Function Definition
    }
    
    // Begin Function Definition
    int verifySelection(int iSquareNum, int iCurrentPlayer) 
    { 
    
    	if ( board[iSquareNum - 1] == ' ' && (iCurrentPlayer == 1 || iCurrentPlayer == 0) ) 
    	{
    		board[iSquareNum - 1] = 'X';
    		return 0;
    	}
    	
    	else if ( board[iSquareNum - 1] == ' ' && iCurrentPlayer == 2 ) 
    	{
    		board[iSquareNum - 1] = 'O';
    		return 0;
    	}
    	
    	else
    		return 1;
    // End Function Definition
    }

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is what you get for scoop and poop coding... Problems in code that you don't know the first thing about.

    Hint: Given all the time and effort you're putting into trying to doctor someone else's code, you could probably have written your own with half the effort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [B]2 Dimensional Array [/B]
    By whitey300 in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2009, 03:25 PM
  2. Replies: 24
    Last Post: 11-11-2008, 01:39 PM
  3. 2 Dimensional Array
    By mancode1009 in forum C Programming
    Replies: 1
    Last Post: 11-25-2007, 02:58 PM
  4. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  5. Two-dimensional array
    By Nir in forum C Programming
    Replies: 3
    Last Post: 04-14-2003, 10:54 PM