Here is my assignment.
5. Assignment: Using the code given below develop a solution to the game Tic-Tac-Toe.
a. The game is played between a human user and the computer. The computer’s moves are made randomly. You must get 3-in-a-row to win; getting five of the nine squares is a tie game.
b. Use proper designing strategies. All input and output should be done in a single module (I/O should not be done in strategy modules). Have an introduction screen that explains how to play the game, let the user know the computer’s move at each turn, and have an ending screen announcing the winner.
6. Complete all function stubs and correct the logic of the main() to obtain a working program.
The starting code;
#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()
{
}
//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;
}
}
/*********
repeat the above check for all locations
***********/
}
}
//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";
/*************
// the logic is similar to the computer move except that
// it takes the location from the keyboard
****************/
}
//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;
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);
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);
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!=0));
}
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!=0));
}
}
I took this code did all the hard work below;
//Michelle Stokes CSI130 Lab 13
//Code from hand out Lab14 TTT
#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 << "This game is played between a human user and the computer. \n" << "The computers moves are made randomly. \n" << "You must get 3-in-a-row to win; getting five of the nine squares is a tie game. \n";
cout << "You player will be lower case 'x'. You will enter the number of the space you would like to play. \n";
}
//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;
}
}
else if (i==2)
{
if ((B[0][1]!='x')&&(B[0][1]!='o'))
{ B[0][1]='o'; //if not taken, then move
turn=1;
}
}
else if (i==3)
{
if ((B[0][2]!='x')&&(B[0][2]!='o'))
{ B[0][2]='o'; //if not taken, then move
turn=1;
}
}
else if (i==4)
{
if ((B[1][0]!='x')&&(B[1][0]!='o'))
{ B[1][0]='o'; //if not taken, then move
turn=1;
}
}
else if (i==5)
{
if ((B[1][1]!='x')&&(B[1][1]!='o'))
{ B[1][1]='o'; //if not taken, then move
turn=1;
}
}
else if (i==6)
{
if ((B[1][2]!='x')&&(B[1][2]!='o'))
{ B[1][2]='o'; //if not taken, then move
turn=1;
}
}
else if (i==7)
{
if ((B[2][0]!='x')&&(B[2][0]!='o'))
{ B[2][0]='o'; //if not taken, then move
turn=1;
}
}
else if (i==8)
{
if ((B[2][1]!='x')&&(B[2][1]!='o'))
{ B[2][1]='o'; //if not taken, then move
turn=1;
}
}
else 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";
//while (turn==0) //Make sure the move is ok
{
cout << "Please enter the number of the space you would like to move to: ";
cin >> i;
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;
}
}
else if (i==2)
{
if ((B[0][1]!='x')&&(B[0][1]!='o'))
{ B[0][1]='x'; //if not taken, then move
turn=1;
}
}
else if (i==3)
{
if ((B[0][2]!='x')&&(B[0][2]!='o'))
{ B[0][2]='x'; //if not taken, then move
turn=1;
}
}
else if (i==4)
{
if ((B[1][0]!='x')&&(B[1][0]!='o'))
{ B[1][0]='x'; //if not taken, then move
turn=1;
}
}
else if (i==5)
{
if ((B[1][1]!='x')&&(B[1][1]!='o'))
{ B[1][1]='x'; //if not taken, then move
turn=1;
}
}
else if (i==6)
{
if ((B[1][2]!='x')&&(B[1][2]!='o'))
{ B[1][2]='x'; //if not taken, then move
turn=1;
}
}
else if (i==7)
{
if ((B[2][0]!='x')&&(B[2][0]!='o'))
{ B[2][0]='x'; //if not taken, then move
turn=1;
}
}
else if (i==8)
{
if ((B[2][1]!='x')&&(B[2][1]!='o'))
{ B[2][1]='x'; //if not taken, then move
turn=1;
}
}
else if (i==9)
{
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;
if((B[0][0] == B[0][1] && B[0][1] == B[0][2]))
{
if (B[0][0]=='x')
win=1;
else if (B[0][0]=='o')
win=2;
if (B[0][1]=='x')
win=1;
else if (B[0][1]=='o')
win=2;
if (B[0][2]=='x')
win=1;
else if (B[0][2]=='o')
win=2;
}
if ((B[1][0] == B[1][1] && B[1][1] == B[1][2]))
{
if (B[1][0]=='x')
win=1;
else if (B[1][0]=='o')
win=2;
if (B[1][1]=='x')
win=1;
else if (B[1][1]=='o')
win=2;
if (B[1][2]=='x')
win=1;
else if (B[1][2]=='o')
win=2;
}
if ((B[2][0] == B[2][1] && B[2][1] == B[2][2]))
{
if (B[2][0]=='x')
win=1;
else if (B[2][0]=='o')
win=2;
if (B[2][1]=='x')
win=1;
else if (B[2][1]=='o')
win=2;
if (B[2][2]=='x')
win=1;
else if (B[2][2]=='o')
win=2;
}
if ((B[0][0] == B[1][1] && B[1][1] == B[2][2]))
{
if (B[0][0]=='x')
win=1;
else if (B[0][0]=='o')
win=2;
if (B[1][1]=='x')
win=1;
else if (B[1][1]=='o')
win=2;
if (B[2][2]=='x')
win=1;
else if (B[2][2]=='o')
win=2;
}
if ((B[0][2] == B[1][1] && B[1][1] == B[2][0]))
{
if (B[0][2]=='x')
win=1;
else if (B[0][2]=='o')
win=2;
if (B[1][1]=='x')
win=1;
else if (B[1][1]=='o')
win=2;
if (B[2][0]=='x')
win=1;
else if (B[2][0]=='o')
win=2;
}
if ((B[0][0] == B[1][0] && B[1][0] == B[2][0]))
{
if (B[0][0]=='x')
win=1;
else if (B[0][0]=='o')
win=2;
if (B[1][0]=='x')
win=1;
else if (B[1][0]=='o')
win=2;
if (B[2][0]=='x')
win=1;
else if (B[2][0]=='o')
win=2;
}
if ((B[0][1] == B[1][1] && B[1][1] == B[2][1]))
{
if (B[0][1]=='x')
win=1;
else if (B[0][1]=='o')
win=2;
if (B[1][1]=='x')
win=1;
else if (B[1][1]=='o')
win=2;
if (B[2][1]=='x')
win=1;
else if (B[2][1]=='o')
win=2;
}
if ((B[0][2] == B[1][2] && B[1][2] == B[2][2]))
{
if (B[0][2]=='x')
win=1;
else if (B[0][2]=='o')
win=2;
if (B[1][2]=='x')
win=1;
else if (B[1][2]=='o')
win=2;
if (B[2][2]=='x')
win=1;
else if (B[2][2]=='o')
win=2;
}
cout<<"\n IN CHECK WIN "<<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 turn = "<<t<<endl;
HumanMove(Board);
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);
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!=0));
}
else
{
do{ cout<<"\n turn = "<<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!=0));
}
}
While still in the lab I was able to get the program going. However I can't get it to stop after someone wins, nor can I get the tie to work (might be related).
Please help! The program has to be in this format as he gave us the guide line and told us something in main was broken.... :)