Any help would be greatly appreciated. Does not recognize cat's game also goes into infinate loop when you try to use the same box twice?
I have spent hours trying to figure it out. I know it something simple.
Any ideas?
//Board Display Class - Tic-Tac-Toe Program
#include <iostream>
using namespace std;
char board[3][3];
int checkWinState(int);
bool boardDraw(int, int, int);
void showBoard();
int main()
{
bool boxPlayed;
int player, boxNum;
int wrong;
int turn=1;
int check=0;
int x, y;
board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';
showBoard();
while(check == 0) // checks for a continuation of the game (0 = continue)
{
if(turn % 2 == 0) // determines the player
player = 2; //o
else
player = 1; //x
cout << "Player " << player << " What box do you want? "; // gets box choice
cin >> boxNum;
if(boxNum >=1 && boxNum <= 9) // validates box number
{
switch(boxNum) // converts the boxNum to x and y values for
{ // a two-dimensional array
case 1:
x = 0, y = 0;
break;
case 2:
x = 0, y = 1;
break;
case 3:
x = 0, y = 2;
break;
case 4:
x = 1, y = 0;
break;
case 5:
x = 1, y = 1;
break;
case 6:
x = 1, y = 2;
break;
case 7:
x = 2, y = 0;
break;
case 8:
x = 2, y = 1;
break;
case 9:
x = 2, y = 2;
break;
}
wrong = 0;
boxPlayed = boardDraw(x, y, turn); // seek valid box
if(boxPlayed == false)
check = checkWinState(turn); // looks for a win or draw
while(boxPlayed == true) // play box if condition is true
{
cout << " That box has already been played.\nPlayer " << player << " Choose again: ";
wrong = 1;
}
if(check != 1 && wrong != 1) // makes sure the right player is indicated as winner
{
turn++;
}
}
else
cout << "Enter an empty box number of 1 through 9.\n";
}
if(check == 1) // displays tic-tac-toe or draw
{
cout << "Tic-Tac-Toe! ";
if(turn % 2 == 0)
cout << "O Wins!!\n";
else
cout << "X Wins!!\n";
}
else
cout << "Cat's Game!\n";
system("pause");
return 0;
}
// takes the row and the column as input and if the spot is open
// it will put an x or an o there depending on which player's turn it is.
// returns true if the move was successful and false if it wasn't.
//checks if the spot on the board is open. input is one and two.
bool boardDraw(int one, int two, int t)
{
if(board[one][two] == 'X')
return true;
else if(board[one][two] == 'O')
return true;
else if(t % 2 != 0)
{
board[one][two] = 'X';
showBoard();
return false;
}
else
{
board[one][two] = 'O';
showBoard();
return false;
}
}
//prints out the board
void showBoard()
{
for(int q = 0; q < 3; q++)
{
for(int z = 0; z < 3; z++)
{
if(z == 0)
{ cout << " "; }
cout << board[q][z];
if(z < 2)
{ cout << " | "; }
if(z == 2)
{ cout << endl; }
}
if( q < 2)
{
cout << "-----------------" << endl;
}
}
cout << endl;
}
int checkWinState(int t)
{
char player;
if (t % 2 != 0)
{ player ='X'; }
else
{ player ='O'; }
if (board[0][0] == player && board[0][1] == player && board[0][2] == player ) {
return 1;
} else if (board[1][0] == player && board[1][1] == player && board[1][2] == player) {
return 1;
} else if (board[2][0] == player && board[2][1] == player && board[2][2] == player ) {
return 1;
} else if (board[0][0] == player && board[1][0] == player && board[2][0] == player ) {
return 1;
} else if (board[0][1] == player && board[1][1] == player && board[2][1] == player ) {
return 1;
} else if (board[0][2] == player && board[1][2] == player && board[2][2] == player ) {
return 1;
} else if (board[0][0] == player && board[1][1] == player && board[2][2] == player ) {
return 1;
} else if (board[0][2] == player && board[1][1] == player && board[2][0] == player ) {
return 1;
}
if (t>9)
return 3;
return 0;
}