Code:
void TickTacToe::Display()
{
for(int j=0; j<3; j++) //nested for loops to display board
{
for(int i=0; i<3; i++)
{
if (board[j][i] == firstP)
cout << "\tX";
else if (board[j][i] == secP)
cout << "\tO";
else
cout << "\t" << board[j][i];
if (i % 3 == 0) //checking for the end of the row
cout << endl;
}
}
MakeMove();
}
Function that calls it-
Code:
void TickTacToe::MakeMove()
{
Display();
if (CurPlayer == firstP)
cout << "\n\n\nIt's player one's turn.\n";
else
cout << "\n\n\nIt's player two's turn.\n";
cout << "\n\n\nSelect a location: ";
cin >> location;
if (CheckLocation() == 1) //if CheckLocation() returns a non-zero value
MakeMove(); //restart function
switch(location)
{
case 1: board[0][0] = CurPlayer; break; //CurPlayer claims the selected position
case 2: board[0][1] = CurPlayer; break;
case 3: board[0][2] = CurPlayer; break;
case 4: board[1][0] = CurPlayer; break;
case 5: board[1][1] = CurPlayer; break;
case 6: board[1][2] = CurPlayer; break;
case 7: board[2][0] = CurPlayer; break;
case 8: board[2][1] = CurPlayer; break;
case 9: board[2][2] = CurPlayer; break;
}
CheckWin();
if (CurPlayer == secP) //switch current player
CurPlayer = firstP;
else
CurPlayer = secP;
system("cls");
Display();
}
Oh, and since I posted MakeMove() could I use
Code:
if (!CheckLocation())
MakeMove();
Instead of
Code:
if (CheckLocation() == 1)
MakeMove();