Hey guys
I am doing well making a tic tac toe program and so far I have created the board and am able to place the "O" character as a human move in any of the nine slots. Also, I have a function that checks if the move is legal before it allows this to happen.
The problem comes when I try to ask the computer to make a move. I keeps saying "cannot move to this location" even when the square is empty. All the game does so far is call the computer move then call the player move so I can test to see if its working ok before I go through all the winning moves etc.
Here is the code that organises the computer movement each turn
This function checks if the move is legalCode:// function to determine a computer move int TicTacToe::makeComputerMove ( char m_Board[][ 3 ] ) { int square = 0; int xCorr = rand() % 8 + 0; if (( xCorr < 0 ) || ( xCorr > 8 )) { return 0; } int yCorr = rand() % 8 + 0; if (( yCorr < 0 ) || ( yCorr > 8 )) { return 0; } if (( xCorr == 0 ) && ( yCorr == 0 )) { m_Board[ xCorr ][ yCorr ]; square = 1; } else if (( xCorr == 0 ) && ( yCorr == 1 )) { m_Board[ xCorr ][ yCorr ]; square = 2; } else if (( xCorr == 0 ) && ( yCorr == 2 )) { m_Board[ xCorr ][ yCorr ]; square = 3; } else if (( xCorr == 1 ) && ( yCorr == 0 )) { m_Board[ xCorr ][ yCorr ]; square = 4; } else if (( xCorr == 1 ) && ( yCorr == 1 )) { m_Board[ xCorr ][ yCorr ]; square = 5; } else if (( xCorr == 1 ) && ( yCorr == 2 )) { m_Board[ xCorr ][ yCorr ]; square = 6; } else if (( xCorr == 2 ) && ( yCorr == 0 )) { m_Board[ xCorr ][ yCorr ]; square = 7; } else if (( xCorr == 2 ) && ( yCorr == 1 )) { m_Board[ xCorr ][ yCorr ]; square = 8; } else if (( xCorr == 2 ) && ( yCorr == 2 )) { m_Board[ xCorr ][ yCorr ]; square = 9; } isLegalComp ( m_Board, xCorr, yCorr, square ); return 0; }
The human move functions are almost identical to these and they are working perfectly, I do not understand why the computer move is not working.Code:// function to check that the move the computer // makes is a legal one int TicTacToe::isLegalComp ( char m_Board [][ 3 ], int &x, int &y, int &square ) { // first row first column if (( square == 1 ) && ( m_SquareTaken[ 0 ] == 0 )) { m_Board[ x ][ y ] = 'X'; m_SquareTaken[ 0 ]++; return 0; } // first row second column if (( square == 2 ) && ( m_SquareTaken[ 1 ] == 0 )) { m_Board[ x ][ y ] = 'X'; m_SquareTaken[ 1 ]++; return 0; } // first row third column if (( square == 3 ) && ( m_SquareTaken[ 2 ] == 0 )) { m_Board[ x ][ y ] = 'X'; m_SquareTaken[ 2 ]++; return 0; } // second row first column if (( square == 4 ) && ( m_SquareTaken[ 3 ] == 0 )) { m_Board[ x ][ y ] = 'X'; m_SquareTaken[ 3 ]++; return 0; } // second row second column if (( square == 5 ) && ( m_SquareTaken[ 4 ] == 0 )) { m_Board[ x ][ y ] = 'X'; m_SquareTaken[ 4 ]++; return 0; } // second row third column if (( square == 6 ) && ( m_SquareTaken[ 5 ] == 0 )) { m_Board[ x ][ y ] = 'X'; m_SquareTaken[ 5 ]++; return 0; } // third row first column if (( square == 7 ) && ( m_SquareTaken[ 6 ] == 0 )) { m_Board[ x ][ y ] = 'X'; m_SquareTaken[ 6 ]++; return 0; } // third row second column if (( square == 8 ) && ( m_SquareTaken[ 7 ] == 0 )) { m_Board[ x ][ y ] = 'X'; m_SquareTaken[ 7 ]++; return 0; } // third row third column if (( square == 9 ) && ( m_SquareTaken[ 8 ] == 0 )) { m_Board[ x ][ y ] = 'X'; m_SquareTaken[ 8 ]++; return 0; } // move is illegal else { std::cout << "\nCannot move to this location!\n"; return 1; } return 0; }
Any help appreiciated - I know its alot to read but the program is quite large.



LinkBack URL
About LinkBacks


