Tic Tac Toe Problem

Ok so a follow on to my last post still a newb at this so please bare with me, I can get the game to play but declaring a winner does not shut the game down it keeps letting me play moves till I hit 9 moves any suggestions?

>
#include <iostream>
#include <ctime>
using namespace std;

void Intro()
{
cout<<"Welcome to the Tic Tac Toe Game"<<endl;
cout<<"The rules are easy pick one of the"<<endl;
cout<<"9 numbers to play your spot"<<endl;
cout<<"The first player to get three"<<endl;
cout<<"X's or O's in a row wins"<<endl;

}

void ComputerMove(char B[][3])
{ int i=0, turn=0;
cout<<"\n IN COMPUTER MOVE \n";
while (turn==0) //Make sure the move is ok
{
i=rand()%9+1; //randomize a move
if (i==1) // make sure the first location is not taken
{
if ((B[0][0]!='x')&&(B[0][0]!='o'))
{ B[0][0]='O'; //if not taken, then move
turn=1;
}
}
if (i==2)
{
if ((B[0][1]!='x')&&(B[0][1]!='o'))
{ B[0][1]='o'; //if not taken, then move
turn=1;
}
}
if (i==3)
{
if ((B[0][2]!='x')&&(B[0][2]!='o'))
{ B[0][2]='O'; //if not taken, then move
turn=1;
}
}
if (i==4)
{
if ((B[1][0]!='x')&&(B[1][0]!='o'))
{ B[1][0]='O'; //if not taken, then move
turn=1;
}
}
if (i==5)
{
if ((B[1][1]!='x')&&(B[1][1]!='o'))
{ B[1][1]='O'; //if not taken, then move
turn=1;
}
}
if (i==6)
{
if ((B[1][2]!='x')&&(B[1][2]!='o'))
{ B[1][2]='O'; //if not taken, then move
turn=1;
}
}
if (i==7)
{
if ((B[2][0]!='x')&&(B[2][0]!='o'))
{ B[2][0]='O'; //if not taken, then move
turn=1;
}
}
if (i==8)
{
if ((B[2][1]!='x')&&(B[2][1]!='o'))
{ B[2][1]='O'; //if not taken, then move
turn=1;
}
}
if (i==9)
{
if ((B[2][2]!='x')&&(B[2][2]!='o'))
{ B[2][2]='O'; //if not taken, then move
turn=1;
}
}
}
}

//Purpose: allows the user to make a move
//Input: B[][] a 2 dimensional array that represents the board
//Output: B[][], the board that has been updated with the human's move
void HumanMove(char B[][3])
{int i=0, turn=0;
cout<<"\n IN HUMAN MOVE \n";
cin>>i;
while (turn==0) //Make sure the move is ok
{
if (i==1) // make sure the first location is not taken
{
if ((B[0][0]!='x')&&(B[0][0]!='o'))
{ B[0][0]='x'; //if not taken, then move
turn=1;
}
}
if (i==2)
{
if ((B[0][1]!='x')&&(B[0][1]!='o'))
{ B[0][1]='x'; //if not taken, then move
turn=1;
}
}
if (i==3)
{
if ((B[0][2]!='x')&&(B[0][2]!='o'))
{ B[0][2]='x'; //if not taken, then move
turn=1;
}
}
if (i==4)
{
if ((B[1][0]!='x')&&(B[1][0]!='o'))
{ B[1][0]='x'; //if not taken, then move
turn=1;
}
}
if (i==5)
{
if ((B[1][1]!='x')&&(B[1][1]!='o'))
{ B[1][1]='x'; //if not taken, then move
turn=1;
}
}
if (i==6)
{
if ((B[1][2]!='x')&&(B[1][2]!='o'))
{ B[1][2]='x'; //if not taken, then move
turn=1;
}
}
if (i==7)
{
if ((B[2][0]!='x')&&(B[2][0]!='o'))
{ B[2][0]='x'; //if not taken, then move
turn=1;
}
}
if (i==8)
{
if ((B[2][1]!='x')&&(B[2][1]!='o'))
{ B[2][1]='x'; //if not taken, then move
turn=1;
}
}
if (i==9)
{
if ((B[2][2]!='x')&&(B[2][2]!='o'))
{ B[2][2]='x'; //if not taken, then move
turn=1;
}
}
}
}
void PrintBoard(char B[][3])
{ cout<<"\n "<<B[0][0]<<"| "<<B[0][1]<<"| "<<B[0][2];
cout<<"\n_________________";
cout<<"\n "<<B[1][0]<<"| "<<B[1][1]<<"| "<<B[1][2];
cout<<"\n_________________";
cout<<"\n "<<B[2][0]<<"| "<<B[2][1]<<"| "<<B[2][2];
cout<<"\n\n";
}
//Purpose: Check for a winner
//Input: B[][] a 2 dimensional array that represents the board
//Output: win and integer indicating winner status.
// 0=no winner, 1= human winner, 2= computer winner
int CheckWin(char B[][3])
{ int win = 0;
if ((B[0][0] == B[0][1]) && (B[0][1] == B[0][2]))
{
if (B[0][0]=='x')
win=1;
else
win=2;
}
if ((B[1][0] == B[1][1]) && (B[1][1] == B[1][2]))
{
if (B[1][0]=='x')
win=1;
else
win=2;
}
if ((B[2][0] == B[2][1]) && (B[2][1] == B[2][2]))
{
if (B[2][0]=='x')
win=1;
else
win=2;
}
if ((B[0][0] == B[1][0]) && (B[1][0] == B[2][0]))
{
if (B[0][0]=='x')
win=1;
else
win=2;
}
if ((B[0][1] == B[1][1]) && (B[1][1] == B[2][1]))
{
if (B[0][1]=='x')
win=1;
else
win=2;
}
if ((B[0][2] == B[1][2]) && (B[1][2] == B[2][2]))
{
if (B[0][2]=='x')
win=1;
else
win=2;
}
if ((B[0][0] == B[1][1]) && (B[1][1] == B[2][2]))
{
if (B[0][2]=='x')
win=1;
else
win=2;
}
if ((B[2][0] == B[1][1]) && (B[1][1] == B[0][2]))
{
if (B[0][0]=='x')
win=1;
else
win=2;
}
else
win=0;
cout<<"\n Game over "<<win<<endl;

return (win);
}
void main ()
{int choice=0, done=0, t=0, w=0;
char Board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
srand(time(0));
Intro();
PrintBoard(Board);
cout<<"Do you want to go first? Enter 1 for yes, 2 for no";
cin>>choice;
if (choice==1) //if player wishes to go first
{
do{ cout<<"\n t = "<<t<<endl;
HumanMove(Board);
t=t+1;
w=CheckWin(Board);
if (w==1)
cout<<"Youwin!";
else if (w==2)
cout<<"You lose!";
else if (t==9&&w==0)
cout<<"Tie Game";
PrintBoard(Board);
if((w == 1 || w == 2) || (t == 9 && w == 0))
break;
if(t<9 && w==0)
{
ComputerMove(Board);
t=t+1;
w=CheckWin(Board);
}
if (w==1)
cout<<"You Win!";
else if (w==2)
cout<<"You lose!";
else if (t==9&&w==0)
cout<<"Tie Game";
PrintBoard(Board);
if((w == 1 || w == 2) || (t == 9 && w == 0))
break;
}while((t!=9)||(w!=0));
}
else
{
do{ cout<<"\n t = "<<t<<endl;
ComputerMove(Board);
t=t+1;
w=CheckWin(Board);
if(w==1)
cout<<"You Win!";
else if (w==2)
cout<<"You lose!";
else if (t==9&&w==0)
cout<<"Tie Game";
PrintBoard(Board);
if((w == 1 || w == 2) || (t == 9 && w == 0))
break;
if(t<9 && w==0)
{
HumanMove(Board);
t=t+1;
w=CheckWin(Board);
}
if (w==1)
cout<<"You Win!";
else if (w==2)
cout<<"You lose!";
else if (t==9&&w==0)
cout<<"Tie Game";
PrintBoard(Board);
if((w == 1 || w == 2) || (t == 9 && w == 0))
break;
}while((t!=9)||(w!=0));
}

}
<
closed account (Dy7SLyTq)
New to this how where do they go

 ```[code] #include // all the other lines [/code] ```

It will look like:

 ``12`` ``````#include // all the other lines ``````
Last edited on
[/code] goes below your code, [code] goes above it
Ok so it should look like this

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312`` ``````#include #include using namespace std; void Intro() { cout<<"Welcome to the Tic Tac Toe Game"<>i; while (turn==0) //Make sure the move is ok { if (i==1) // make sure the first location is not taken { if ((B[0][0]!='x')&&(B[0][0]!='o')) { B[0][0]='x'; //if not taken, then move turn=1; } } if (i==2) { if ((B[0][1]!='x')&&(B[0][1]!='o')) { B[0][1]='x'; //if not taken, then move turn=1; } } if (i==3) { if ((B[0][2]!='x')&&(B[0][2]!='o')) { B[0][2]='x'; //if not taken, then move turn=1; } } if (i==4) { if ((B[1][0]!='x')&&(B[1][0]!='o')) { B[1][0]='x'; //if not taken, then move turn=1; } } if (i==5) { if ((B[1][1]!='x')&&(B[1][1]!='o')) { B[1][1]='x'; //if not taken, then move turn=1; } } if (i==6) { if ((B[1][2]!='x')&&(B[1][2]!='o')) { B[1][2]='x'; //if not taken, then move turn=1; } } if (i==7) { if ((B[2][0]!='x')&&(B[2][0]!='o')) { B[2][0]='x'; //if not taken, then move turn=1; } } if (i==8) { if ((B[2][1]!='x')&&(B[2][1]!='o')) { B[2][1]='x'; //if not taken, then move turn=1; } } if (i==9) { if ((B[2][2]!='x')&&(B[2][2]!='o')) { B[2][2]='x'; //if not taken, then move turn=1; } } } } void PrintBoard(char B[][3]) { cout<<"\n "<>choice; if (choice==1) //if player wishes to go first { do{ cout<<"\n t = "<

OK thnx for the help
closed account (Dy7SLyTq)
yes
You need to add a `break;` statement.

After you assign 'w' with the return from the check function.
 ``12345`` ``````If( w == 2 ) { cout << "You win!"; break; }``````

This will exit the do-while loop. Ending your program.
I think the last few lines of function `CheckWin()`
 ``233234235236237238`` `````` else win=0; cout<<"\n Game over "<

should look like this:
 ``233234235236237238`` `````` if (win != 0) cout<<"\n Game over "<

Currently that "else" is usually executed and re-sets win to zero, even after a win.

Also, some of the checks may be wrong, the diagonal 1-5-9 checks for an 'x' in one of the other squares.

 ``219220221222223224225`` `````` if ((B[0][0] == B[1][1]) && (B[1][1] == B[2][2])) { if (B[0][2]=='x') win=1; else win=2; }``````
Last edited on
Topic archived. No new replies allowed.