I am writing a program to run a tictactoe game but cant seem to figure this out
having trouble with my occupysquare function and the other two being illegal when calling
here is the code
implementation:
#include <iostream>
if (x > 2 || x < 0 || y > 2 || y < 0)
{
return false;
}
if (board [x][y] != ' ')
{
return false;
}
if (x < 3 && x >= 0 && y < 3 && y >= 0)
{
return true;
}
if (board[x][y] = ' ')
{
return true;
}
}
char TicTacToe::CheckForWinner()
{
// check for a win on diagonals
if ( board[ 0 ][ 0 ] != ' ' && board[ 0 ][ 0 ] == board[ 1 ][ 1 ] &&
board[ 0 ][ 0 ] == board[ 2 ][ 2 ] )
{
return WIN;
}
else if ( board[ 2 ][ 0 ] != ' ' && board[ 2 ][ 0 ] ==
board[ 1 ][ 1 ] && board[ 2 ][ 0 ] == board[ 0 ][ 2 ] )
{
return WIN;
}
// check for win in rows
for ( int a = 0; a < 3; ++a )
if ( board[ a ][ 0 ] != ' ' && board[ a ][ 0 ] ==
board[ a ][ 1 ] && board[ a ][ 0 ] == board[ a ][ 2 ] )
{
return WIN;
}
// check for win in columns
for ( int a = 0; a < 3; ++a )
if ( board[ 0 ][ a ] != ' ' && board[ 0 ][ a ] ==
board[ 1 ][ a ] && board[ 0 ][ a ] == board[ 2 ][ a ] )
{
return WIN;
}
}// end to check for winner
bool TicTacToe::IsScratch()
{
// check for a completed game
for ( int r = 0; r < 3; ++r )
cout <<endl;
cout <<endl;
cout << "****** Welcome to Tic-Tac-Toe ******"<<endl;
cout << "********************************************************************"<<endl;
cout << "** **"<<endl;
cout << "** Instructions - You will be asked for how many players: **"<<endl;
cout << "** **"<<endl;
cout << "** Entering a 0 - two computer players **"<<endl;
cout << "** **"<<endl;
cout << "** Entering a 1 - human vs computer **"<<endl;
cout << "** **"<<endl;
cout << "** Entering a 2 - two human players **"<<endl;
cout << "** **"<<endl;
cout << "** Make your move known by entering coordinates, 0 - 2. **"<<endl;
cout << " "<<endl;
cout <<endl;
cout << "How many players will be playing? ";
cin >> NumberOfPlayers;
srand( (unsigned)time (0));
Selection = 1 + rand()% 2; //Assigning which player is X and O
if (Selection == 1)
{
Player = 'X';
}
else if (Selection == 2)
{
Player = 'O';
}
if (NumberOfPlayers == 0) //two computer players
{
cout<< "No Human Players, Computer vs Computer! " << endl;
cout<< endl;
cout << Player << "Will go first" << endl;
};
// do
// {
myttt.PrintBoard();
cout <<Player <<"'s turn" << endl;
//do
//{
x = 0 + rand() % 2;
y = 0 + rand() % 2;
cout <<Player << " selected " << x << y << endl;
myttt.OccupySquare(x,y, Player);
if (myttt.OccupySquare(x, y, Player) == false)
{
cout << "Error! Invalid Selection!" << endl;
}
Winner = myttt.CheckForWinner();
MyOccupySquare = myttt.OccupySquare(x,y,Player);
// }while (!MyOccupySquare );
board [ x ][ y] = symbol;
myttt.PrintBoard();
XoStatus = Winner;
if (XoStatus == WIN)
{
cout << "Player " << static_cast< char > ( symbol ) << " wins!\n";
return true;
} //end if
else if (XoStatus == SCRATCH )
{
cout << "Game is a Scratch.\n";
return true;
}// end else if
else
return false;
//}while (!MyOccupySquare );
// }while (1 == 1); //end of Computer Players
if (NumberOfPlayers == 2) //two human players
{
cout<< endl;
cout<< "Human vs Human" << endl;
cout<< endl;
}