In: Computer Science
Modify the attached TicTacToeStart.cpp
Your job is to complete three functions init, printBoard, and gameOver
#include <iostream>
using namespace std;
char gameBoard[3][3];
void init(){
/*
using a double loop to initialize all gameBoard element as 'E'
*/
}
void printBoard()
{
/*using a double loop to print out gameboard
*/
}
bool gameOver()
{
/*if any row or column, or diagonal are all X or O, return true*/
return false;
}
int main()
{
char again;
do{
bool done=false;
bool xTurn=true;
int count=0;
init();
while(!done){
printBoard();
if(xTurn)
cout<<"It's X's turn"<<endl;
else
cout<<"It is O's turn"<<endl;
int row, col;
do{
cout<<"Please enter two integer to specify an empty spot:";
cin>>row>>col;
}while(gameBoard[row-1][col-1]!='E');
if(xTurn)
gameBoard[row-1][col-1]='X';
else
gameBoard[row-1][col-1]='O';
count++;
if(gameOver()){
done=true;
if(xTurn)
cout<<"X wins!"<<endl;
else
cout<<"O wins!"<<endl;
}else if(count==9){
done=true;
cout<<"It's a tie!"<<endl;
}
xTurn=!xTurn;
}
cout<<"Play again? Enter y to continue:";
cin>>again;
}while(again=='y' || again=='Y');
return 0;
}
In this code, intialized the size parameter method , Also taken a variable gameBoard which can be passed in all those 3 functions to make them work .
Here is the code :->
#include <iostream>
using namespace std;
#define side 3 //Defined the tic toe size as 3
void init(char gameBoard[][side]){
/*
using a double loop to initialize all gameBoard element as 'E'
*/
for (int i=0; i<side; i++)
{
for (int j=0; j<side; j++)
gameBoard[i][j] = 'E';
}
return;
}
void printBoard(char gameBoard[][side])
{
/*using a double loop to print out gameboard
*/
for (int i=0; i<side; i++)
{
for (int j=0; j<side; j++) {
printf("%c\t",gameBoard[i][j]);
}
printf("\n");
}
}
bool gameOver(char gameBoard[][side])
{
/*if any row or column, or diagonal are all X or O, return true*/
// Checking Row elements
for (int i=0; i<side; i++)
{
if (gameBoard[i][0] == gameBoard[i][1] &&
gameBoard[i][1] == gameBoard[i][2] &&
gameBoard[i][0] != 'E')
return true;
}
// Checking column elements
for (int i=0; i<side; i++)
{
if (gameBoard[0][i] == gameBoard[1][i] &&
gameBoard[1][i] == gameBoard[2][i] &&
gameBoard[0][i] != 'E')
return true;
}
// Checking Diagnol elements
if (gameBoard[0][0] == gameBoard[1][1] &&
gameBoard[1][1] == gameBoard[2][2] &&
gameBoard[0][0] != 'E')
return true;
// Checking 2nd way Diagnol elements
if (gameBoard[0][2] == gameBoard[1][1] &&
gameBoard[1][1] == gameBoard[2][0] &&
gameBoard[0][2] != 'E')
return true;
return false;
}
int main()
{
char again;
do
{
bool done=false;
bool xTurn=true;
int count=0;
char gameBoard[side][side]; // Assigned the gameBoard array variable
init(gameBoard); // passing the parameter for intializating E in every cell
while(!done){
printBoard(gameBoard); // passing the parameter for printing board
if(xTurn)
cout<<"It's X's turn"<<endl;
else
cout<<"It is O's turn"<<endl;
int row, col;
do
{
cout<<"Please enter two integer to specify an empty spot:";
cin>>row>>col;
}while(gameBoard[row-1][col-1]!='E');
if(xTurn)
gameBoard[row-1][col-1]='X';
else
gameBoard[row-1][col-1]='O';
count++;
if(gameOver(gameBoard)){ // passing the parameter to check
done=true;
if(xTurn)
cout<<"X wins!"<<endl;
else
cout<<"O wins!"<<endl;
}
else if(count==9)
{
done=true;
cout<<"It's a tie!"<<endl;
}
xTurn=!xTurn;
}
cout<<"Play again? Enter y to continue:";
cin>>again;
}while(again=='y' || again=='Y');
return 0;
}
Hope it helps !!! If any issue , Do Comment.