I have been working on a C++ program that is a tic-tac-toe game.
The problem is that after I win or lose, the program does not end.
Here's the code I have written so far (I even wrote comments to make it easier):
#include <iostream>
#include <ctime>
using namespace std;
//Purpose: Prints to the screen the an introduction and
// provides the rules of the game
//Input: None
//Output: None
void Intro()
{
cout<<"Tic-Tac-Toe";
cout<<endl<<endl<<"Introduction: You will play against the computer in this game."; //Note: Tabs have been used
cout<<" To choose a space, type the number that matches\n the spot you want.";
}
//Purpose: allows the computer to make a move
//Input: B[][] a 2 dimensional array that represents the board
//Output: B[][], the board that has been updated with the computer's move
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) // make sure the second location is not taken
{
if ((B[0][1]!='x')&&((B[0][1]!='o')))
{ B[0][1]='o'; //if not taken, then move
turn=1;
}
}
if (i==3) // make sure the third location is not taken
{
if ((B[0][2]!='x')&&((B[0][2]!='o')))
{ B[0][2]='o'; //if not taken, then move
turn=1;
}
}
if (i==4) // make sure the fourth location is not taken
{
if ((B[1][0]!='x')&&(B[1][0]!='o'))
{ B[1][0]='o'; //if not taken, then move
turn=1;
}
}
if (i==5) // make sure the first location is not taken
{
if ((B[1][1]!='x')&&(B[1][1]!='o'))
{ B[1][1]='o'; //if not taken, then move
turn=1;
}
}
if (i==6) //make sure the sixth location is not taken
{
if ((B[1][2]!='x')&&(B[1][2]!='o'))
{ B[1][2]='o'; //if not taken, then move
turn=1;
}
}
if (i==7) //make sure the seventh location is not taken
{
if ((B[2][0]!='x')&&(B[2][0]!='o'))
{ B[2][0]='o'; //if not taken, then move
turn=1;
}
}
if (i==8) //make sure the eighth location is not taken
{
if ((B[2][1]!='x')&&(B[2][1]!='o'))
{ B[2][1]='o'; //if not taken, then move
turn=1;
}
}
if (i==9) //make sure the ninth location is not taken
{
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";
while (turn==0) //Make sure the move is ok
{
cin>>i; //user enters move
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) // make sure the second location is not taken
{
if ((B[0][1]!='x')&&((B[0][1]!='o')))
{ B[0][1]='x'; //if not taken, then move
turn=1;
}
}
if (i==3) // make sure the third location is not taken
{
if ((B[0][2]!='x')&&((B[0][2]!='o')))
{ B[0][2]='x'; //if not taken, then move
turn=1;
}
}
if (i==4) // make sure the fourth location is not taken
{
if ((B[1][0]!='x')&&(B[1][0]!='o'))
{ B[1][0]='x'; //if not taken, then move
turn=1;
}
}
if (i==5) // make sure the first location is not taken
{
if ((B[1][1]!='x')&&(B[1][1]!='o'))
{ B[1][1]='x'; //if not taken, then move
turn=1;
}
}
if (i==6) //make sure the sixth location is not taken
{
if ((B[1][2]!='x')&&(B[1][2]!='o'))
{ B[1][2]='x'; //if not taken, then move
turn=1;
}
}
if (i==7) //make sure the seventh location is not taken
{
if ((B[2][0]!='x')&&(B[2][0]!='o'))
{ B[2][0]='x'; //if not taken, then move
turn=1;
}
}
if (i==8) //make sure the eighth location is not taken
{
if ((B[2][1]!='x')&&(B[2][1]!='o'))
{ B[2][1]='x'; //if not taken, then move
turn=1;
}
}
if (i==9) //make sure the ninth location is not taken
{
if ((B[2][2]!='x')&&(B[2][2]!='o'))
{ B[2][2]='x'; //if not taken, then move
turn=1;
}
}
}
}
//Purpose: Print out the board
//Input: B[][] a 2 dimensional array that represents the board
//Output: None
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;
cout<<"\n IN CHECK WIN "<<win<<endl;
if ((B[0][0]=='x')&&(B[0][1]=='x')&&(B[0][2]=='x')) //Horizontal wins for 'x'.
{ win=1;
}
if ((B[1][0]=='x')&&(B[1][1]=='x')&&(B[1][2]=='x'))
{ win=1;
}
if ((B[2][0]=='x')&&(B[2][1]=='x')&&(B[2][2]=='x'))
{ win=1;
}
if ((B[0][0]=='x')&&(B[1][0]=='x')&&(B[2][0]=='x')) //Vertical wins for 'x'.
{ win=1;
}
if ((B[0][1]=='x')&&(B[1][1]=='x')&&(B[2][1]=='x'))
{ win=1;
}
if ((B[0][2]=='x')&&(B[1][2]=='x')&&(B[2][2]=='x'))
{ win=1;
}
if ((B[0][0]=='x')&&(B[1][1]=='x')&&(B[2][2]=='x')) //Top-Left diagonal win for 'x'.
{ win=1;
}
if ((B[0][2]=='x')&&(B[1][1]=='x')&&(B[2][0]=='x')) //Top-Right diagonal win for 'x'.
{ win=1;
}
if ((B[0][0]=='o')&&(B[0][1]=='o')&&(B[0][2]=='o')) //Horizontal wins for 'o'.
{ win=2;
}
if ((B[1][0]=='o')&&(B[1][1]=='o')&&(B[1][2]=='o'))
{ win=2;
}
if ((B[2][0]=='o')&&(B[2][1]=='o')&&(B[2][2]=='o'))
{ win=2;
}
if ((B[0][0]=='o')&&(B[1][0]=='o')&&(B[2][0]=='o')) //Vertical wins for 'o'.
{ win=2;
}
if ((B[0][1]=='o')&&(B[1][1]=='o')&&(B[2][1]=='o'))
{ win=2;
}
if ((B[0][2]=='o')&&(B[1][2]=='o')&&(B[2][2]=='o'))
{ win=2;
}
if ((B[0][0]=='o')&&(B[1][1]=='o')&&(B[2][2]=='o')) //Top-Left diagonal win for 'o'.
{ win=2;
}
if ((B[0][2]=='o')&&(B[1][1]=='o')&&(B[2][0]=='o')) //Top-Right diagonal win for 'o'.
{ win=2;
}
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\n";
cin>>choice;
if (choice==1) //if player wishes to go first
{
do{ cout<<"\n t = "<<t<<endl;
HumanMove(Board);
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);
ComputerMove(Board);
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);
t=t+1;
}while((t!=9)||((w!=1)||(w!=2)));
}
else
{
do{ cout<<"\n t = "<<t<<endl;
ComputerMove(Board);
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);
HumanMove(Board);
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);
t=t+1;
}while((t!=9)||((w!=1)||(w!=2)));
}
}
Any help will be greatly appreciated.
Thanks!