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;
}

No bugs, just seems to be something I,m overlooking in my structure? Not used to this old DOS compiler Dev c++

It may compile but the logic may be wrong. Debugging is finding out where your logic gets fubared.

BTW dev-cpp is a great IDE and uses a modern up to date compiler.

It's possible I,m not debugging properly. I guess I got used to Visual 2005 way of things. I don't mind using Dev just have to better understand it. I am fairly new to c++

well back on topic,

you say you get a problem if you try going in a square that is already used. So why not create a function checkifEmpty() which checks before if that square is empty.


For a drawn game surely you just count the moves played i.e.

if moved played = 9 and checkwins = 0 then 
   show draw
Endif

?

I thought I covered that with the isTaken function and also for cats game in my checkWinState function but I guess I,m missing something?

By the way this is NOT how I personally would write this program. This is how my instructor wishes it to be done. This way is to drawn out and semi-confusing.

Well I've already told you. Before you fill a square with 'x' or 'o' you need to check if it is occupied.

Where, when the user chooses their square, do you check before if it is occupied?

By using while boxPlayed The program will tell the box is taken but will fall through after it asks player to enter another box?

The checking has to take place before you assign the 'x' or 'o' variable to a square.


Now you tell me, is your validating taking place before or after?

Don't know about you but that looks like it is happening after?

It seems to be validating alright just can't figure out why it keeps looping instead of validating then accepting new input?

By the way Thank you for your patience, perhaps I've stared at this code for too long:(

Sure it validates, but you don't seem to understand. What's the point of validating something after you have already assigned it a to a position.

It's like me saying I'm gonna sell someone alcohol behind the bar, watch them drink it then ask them for ID.

Where's the logic in that?

Get your logic right first before you can move on.

Thanks Thwee for making me take a harder look at my logic, sometimes a person can get frustrated. Especially when learning four languages at once (Java, VB, C++, and PHP. Thanks again.... Gotta go to C++ class (hahahah)

Yeah well I told a lie when I said you were assigning the values before you validated. That isn't strictly true.

But still your logic is backward. Try my way, things should become clearer.

And use flow charts.

This article has been dead for over six months. Start a new discussion instead.