Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with writing the code for the following. I\'m using C++ and vim. Ple

ID: 3868525 • Letter: P

Question

Please help with writing the code for the following. I'm using C++ and vim. Please include all 4 files required for the project, and only those 4: Board.hpp, Board.cpp, TicTacToe.hpp, and TicTacToe.cpp. Please write plenty of comments to explain the code. Thanks!

Write a class called Board that represents a tic-tac-toe board. It should have a 3x3 array as a data member, which will store the locations of the players' moves. It should have a default constructor that initializes the 3x3 array to being empty. It should have a method called makeMove that takes the x and y coordinates of the move (see the example below) and which player's turn it is as parameters. If that location is unoccupied, makeMove should record the move and return true. If that location is already occupied, makeMove should just return false. There should be a method called gameState that takes no parameters and returns one of the four following values: X_WON, O_WON, DRAW, or UNFINISHED - use an enum for this, not strings (the enum definition should go in Board.hpp, before the Board class definition). There should also be a method called printwhich just prints out the current board to the screen. Write a class called Tic Tac Toe that allows two people to play a game. This class will have a field for a Board object and a field to keep track of which players turn it is. It should have a constructor that takes a char parameter that specifies whether 'X' or 'o' should have the first move. It should have a method called play that starts the game. The play method should keep looping, asking the correct player for their move and sending it to the board (with makeMove) until someone has won or it's a draw (as indicated by gameState), and then declare what the outcome was. Write a main method (in Tic Tac Toe.cpp) that asks the user which player should go first, creates a new Tic Tac Toe object and starts the game. For this assignment only, you will not comment out your main method. Input validation: If someone tries to take an occupied square, tell them that square is already occupied and ask for a different move Here's an example portion of a game (already in progress) 0 1 2 0 x Player o: please enter your move.

Explanation / Answer

#include <iostream>
#include <conio.h>
#include <windows.h>

/* This cpp program is supposed to run a simple tic-tac-toe game
which will keep scores, and allow you to choose from best 2
out of 3 or best 3 out of 5. I ran this program on code::blocks
a month ago and it worked, but I made slight changes, and
havn't built the program since then. If it doesn't work,
I would appreciate some help. It has only been roughly 2
months since I started learning cpp at my high school, and a month when I
finished this program, so there will be errors and bugs.
Anyways, thanks for checking my program out, and I'm working on more simple games like this one :D */


char square[10]={'0','1','2','3','4','5','6','7','8','9'};
void gameBoard();
int checkWinner();
int scoreA=0;
int scoreB=0;
int z;
bool overallwin(int, int, int);
int main() // main function
{
int i;
int j;
int choice;
for(j=48; j < 58; j++) // creates values for the tic-tac-toe board
{
square[j-48]=j;
}
bool shutoff = false; // if the game will continue or not
std::cout<<"do you want best out of 3 or 5?:";
std::cin>>z;
int mark;
int player;
while(shutoff == false) // pretty much until player decides that s/he wants to quit
{
for(j=48; j < 58; j++)
{
square[j-48]=j;
}
player=1,i,choice;
system ("cls");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(z==3) // if player's choice was best out of 3
{


do
{ gameBoard();
player=(player%2)? 1:2; /* the loop will allow this game to switch between player one and two */
std::cout<<"Player"<<player<<", make your choice: ";
std::cin>>choice;
mark=(player==1)? 'X':'O';
  
//////////////////////////////////////////////////////////////////////////////////////////
/////////////// These next few lines will pretty much allow the //////////////////////
/////////////// corresponding square to be marked if it is not taken//////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
  
if (choice==1 && square[1]=='1')
{
square [1]=mark;
}
else if(choice == 2 && square[2] == '2')
{
square[2]=mark;
}
else if(choice ==3 && square[3] == '3')
{
square [3] =mark;
}
else if(choice == 4 && square [4] == '4')
{
square [4]=mark;
}
else if (choice == 5 && square [5] == '5')
{
square[5]=mark;
}

else if(choice == 6 && square [6] == '6')
{
square[6]=mark;
}
else if (choice== 7 && square [7] == '7')
{
square[7]=mark;
}
else if (choice== 8 && square [8] == '8')
{
square[8]=mark;
}
else if(choice == 9 && square[9] == '9')
{
square[9]=mark;
}
else
{
std::cout<<"Dude, invalid move!!!";
player--;
getch();
}
player++;
i=checkWinner();
}
  
//game condition
  
while(i==0);
{
gameBoard();
}

if(i==1)
{
std::cout<<"Winner is, Player:1";
scoreA++;
getch();
system("cls");
for(j=48; j < 58; j++)
{
square[j-48]=j;
}
gameBoard();

}
else if (i==2)
{
std::cout<<"Winner is, Player:2";
scoreB++;
getch();
system("cls");
for(j=48; j < 58; j++)
{
square[j-48]=j;
}
gameBoard();


}

else
{
std::cout<<"It's a draw!";
getch();
system("cls");
for(j=48; j < 58; j++)
{
square[j-48]=j;
}
gameBoard();

}
shutoff = overallwin(scoreA, scoreB, z);

}
  
  
//same as the if (z==3), but only best out of 5

else if(z==5)
{
do
{ gameBoard();
player=(player%2)? 1:2;
std::cout<<"Player"<<player<<", make your choice: ";
std::cin>>choice;
mark=(player==1)? 'X':'O';
if (choice==1 && square[1]=='1')
{
square [1]=mark;
}
else if(choice == 2 && square[2] == '2')
{
square[2]=mark;
}
else if(choice ==3 && square[3] == '3')
{
square [3] =mark;
}
else if(choice == 4 && square [4] == '4')
{
square [4]=mark;
}
else if (choice == 5 && square [5] == '5')
{
square[5]=mark;
}
else if(choice == 6 && square [6] == '6')
{
square[6]=mark;
}
else if (choice== 7 && square [7] == '7')
{
square[7]=mark;
}
else if (choice== 8 && square [8] == '8')
{
square[8]=mark;
}
else if(choice == 9 && square[9] == '9')
{
square[9]=mark;
}
else
{
std::cout<<"Dude, invalid move!!!";
player--;
getch();
}
player++;
i=checkWinner();
}

while(i==0);
{
gameBoard();
}

if(i==1)
{
std::cout<<"Winner is, Player:1";
scoreA++;
getch();
system("cls");
for(j=48; j < 58; j++)
{
square[j-48]=j;
}
gameBoard();
}
else if (i==2)
{
std::cout<<"Winner is, Player:2";
scoreB++;
getch();
system("cls");
for(j=48; j < 58; j++)
{
square[j-48]=j;
}
gameBoard();
}

else
{
std::cout<<"It's a draw!";
getch();
system("cls");
for(j=48; j < 58; j++)
{
square[j-48]=j;
}
gameBoard();
}
shutoff = overallwin(scoreA, scoreB, z);

}
  
// if player decides to enter a different number
//There will be a bug if you enter a letter
else if(z!=3,5)
{
std::cout<<"Enter a 3 or a 5"<<std::endl;
std::cout<<"Press any key to go back";
getch();
system("cls");
main();
}

}

return 0;
}

bool overallwin(int scoreA, int scoreB, int z) // function for the overall winner of the game
{
if(z==3)
{
if (scoreB==2)
{
std::cout<<"overall winner is player2";
getch();
system ("cls");
std::cout<<"Thanks For Playing!!!!";
getch();
return true;
}
else if (scoreA==2)
{
std::cout<<"overall winner is player1";
getch();
system ("cls");
std::cout<<"Thanks For Playing!!!!";
getch();
return true;
}
return false;

}

//////////////////////////////////////////

if(z==5)
{
if (scoreB==3)
{
std::cout<<"overall winner is player2";
getch();
system("cls");
std::cout<<"Thanks for Playing!!!!";
getch();
return true;
}
else if (scoreA==3)
{
std::cout<<"overall winner is player1";
getch();
system("cls");
std::cout<<"Thanks for Playing!!!!";
getch();
return true;
}
return false;

}

}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// ////////////////////////////////////
///////////////////////////////below is the function to check the ////////////////////////////////////
/////////////////////////////// winner of the game ////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int checkWinner()
{

if(square[1] == square[2] && square[2] == square[3] && square[1]=='X')
{
return 1;
}
else if(square[4] == square [5] && square [5] == square [6] && square[4]=='X')
{
return 1;
}
else if (square [7] == square [8] && square [8] == square[9] && square[7]=='X')
{
return 1;
}
else if(square [1] == square [4] && square [4] == square [7] && square [1]=='X')
{
return 1;
}
else if(square [2]== square[5] && square [5] == square [8] && square[2]=='X')
{
return 1;
}
else if(square [3] == square [6] && square [6] == square [9] && square[3]=='X')
{
return 1;
}
else if(square [1] == square [5] && square [5] == square [9] && square[1]=='X')
{
return 1;
}
else if(square[3] == square [5] && square [5] == square [7] && square[3]=='X')
{
return 1;
}

else if(square[1] == square[2] && square[2] == square[3] && square[1]=='O')
{
return 2;
}
else if(square[4] == square [5] && square [5] == square [6] && square[4]=='O')
{
return 2;
}
else if (square [7] == square [8] && square [8] == square[9] && square[7]=='O')
{
return 2;
}
else if(square [1] == square [4] && square [4] == square [7] && square[1]=='O')
{
return 2;
}
else if(square [2]== square[5] && square [5] == square [8] && square[2]=='O')
{
return 2;
}
else if(square [3] == square [6] && square [6] == square [9] && square[3]=='O')
{
return 2;
}
else if(square [1] == square [5] && square [5] == square [9] && square[1]=='O')
{
return 2;
}
else if(square[3] == square [5] && square [5] == square [7] && square[3]=='O')
{
return 2;
}
else if(square[1]!='1' && square [2]!= '2' && square [3]!= '3' && square[4]!= '4' && square[5]!= '5' && square[6]!= '6' && square[7]!= '7' && square[8]!= '8' && square[9]!= '9')
{
return 9000;
}
else
{
return 0;
}

}

//This function draws the gameboard


void gameBoard()
{ system("cls");
std::cout<<"Tic Tac Toe, Best out of "<< z <<std::endl;
std::cout<<"Programmed by Shaka Kanenobu"<<std::endl;
std::cout<<"Player 1-X: Player 2-O"<<std::endl;
std::cout<<std::endl;
std::cout<<"score:"<<scoreA<<" : "<<scoreB<<std::endl;
std::cout<<" | | "<<std::endl;
std::cout<<" "<<square[1]<<" "<<square[2]<<" "<<square[3]<<" "<<std::endl;
std::cout<<"____|____|____"<<std::endl;
std::cout<<" | | "<<std::endl;
std::cout<<" "<<square[4]<<" "<<square[5]<<" "<<square[6]<<" "<<std::endl;
std::cout<<"____|____|____"<<std::endl;
std::cout<<" | | "<<std::endl;
std::cout<<" "<<square[7]<<" "<<square[8]<<" "<<square[9]<<" "<<std::endl;
std::cout<<" | | "<<std::endl;
}