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

C++. This program must have 4 files named : Board.hpp , Board.cpp , TicTacToe.hp

ID: 3866741 • Letter: C

Question

C++. This program must have 4 files named : Board.hpp, Board.cpp, TicTacToe.hpp and TicTacToe.cpp

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). There should also be a method called print, which just prints out the current board to the screen.

Write a class called TicTacToe 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 player's 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 TicTacToe.cpp) that asks the user which player should go first, creates a new TicTacToe object and starts the game.  

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 . .

1 . . .

2 . . .

Player O: please enter your move.

1 2

0 1 2

0 x . .

1 . . o

2 . . .

Player X: please enter your move.

1 2

That square is already taken.

0 1 2

0 x . .

1 . . o

2 . . .

Player X: please enter your move.

Explanation / Answer

Brd.cpp

#include "Brd.hpp"
#include <iostream>

using namespace std;
Brd::Brd()
{
for (int uu = 0; uu < 3; ++uu)
{
for (int qq = 0; qq < 3; ++qq)
{
brd[uu][qq] = '.';
}
}
  
playingedTurn = 0;
}
bool Brd::mkMve(int io, int oi, char chance)
{
  
if (chance == 'io')
{
if (brd[io][oi] == '.' || brd[io][oi] != 'o')
{
brd[io][oi] = 'io';
return true;
}
}
else if (chance == 'o')
{
if (brd[io][oi] == '.' || brd[io][oi]!= 'io')
{
brd[io][oi] = 'o';
return true;
}
}
else
{
return false;
}
  
return 0;
}
int Brd::currently()
{
int playingedTurn = 0;
  
for (int uu = 0; uu < 3; uu++)
{
// Checks if there is winner
if ( (brd[uu][0] == brd[uu][1] && brd[uu][1] == brd[uu][2]) ||
(brd[0][uu] == brd[1][uu] && brd[1][uu] == brd[2][uu]) ||
(brd[0][0] == brd[1][1] && brd[1][1] == brd[2][2]) ||
(brd[0][2] == brd[1][1] && brd[1][1] == brd[2][0]))
{
if ( (brd[uu][0] == 'io' && brd[uu][1] == 'io' && brd[uu][2] == 'io') ||
(brd[0][uu] == 'io' && brd[1][uu] == 'io' && brd[2][uu] == 'io') ||
(brd[0][0] == 'io' && brd[1][1] == 'io' && brd[2][2] == 'io') ||
(brd[0][2] == 'io' && brd[1][1] == 'io' && brd[2][0] == 'io'))
{
return XWIN;
}
else if ( (brd[uu][0] == 'o' && brd[uu][1] == 'o' && brd[uu][2] == 'o') ||
(brd[0][uu] == 'o' && brd[1][uu] == 'o' && brd[2][uu] == 'o') ||
(brd[0][0] == 'o' && brd[1][1] == 'o' && brd[2][2] == 'o') ||
(brd[0][2] == 'o' && brd[1][1] == 'o' && brd[2][0] == 'o'))
{
return OWIN;
}
}
}
if (playingedTurn == 9)
{
return PRINTBRD;
}
else
{
return INCOMPLETE;
}
  
return 0;
}

void Brd::displayinging()
{
cout << endl << " " << "0" << " " << "1" << " " << "2" << endl;
cout << "0" << " " << brd[0][0] << " " << brd[0][1] << " " << brd[0][2] << endl;
cout << "1" << " " << brd[1][0] << " " << brd[1][1] << " " << brd[1][2] << endl;
cout << "2" << " " << brd[2][0] << " " << brd[2][1] << " " << brd[2][2] << endl;
}

Brd.hpp

#ifndef Brd_h
#define Brd_h

enum GameState {XWIN, OWIN, PRINTBRD, INCOMPLETE};

class Brd
{
private:
  
public:
char brd[3][3];
  
Brd();
  
bool mkMve(int, int, char);
  
int currently();
  
void print();
  
char chance;
  
int playingedTurn;
};

TTT.cpp

#include "Brd.hpp"
#include "TTT.hpp"
#include <iostream>

using namespace std;

TTT::TTT(char firstTurns)
{
chance = firstTurns;
}
void TTT::startGame()
{
int io, oi;
char ipMv, firstTurns;
int currently = INCOMPLETE;
while(ipMv == true && currently == INCOMPLETE)
{
playingingBoards.print();
  
cout << "It is playinger " << chance << "'s chance." << endl;
cout << "Please make enter the coordinates of your move." << endl;
cin >> io >> oi;
  
ipMv = playingingBoards.mkMve(io, oi, chance);
  
if (ipMv == true)
{
playingingBoards.playingedTurn++;
  
if (chance == 'io')
{
chance = 'o';
}
else if (chance == 'o')
{
chance = 'io';
}
}
  
if (ipMv == false)
{
cout << "That space is taken." << endl;
cin >> io >> oi;
}
}
  
if (ipMv == true)
{
currently = playingingBoards.currently();
  
if (currently == 0)
{
playingingBoards.print();
cout << "***** io wins!!! *****" << endl;
}
else if (currently == 1)
{
playingingBoards.print();
cout << "***** o wins!!! *****" << endl;
}
else if (currently == 2)
{
playingingBoards.print();
cout << "***** The game is a draw! *****" << endl;
}
}
}
int main()
{
int io, oi;
char firstTurns, ipMv;
  
Brd playingingBoards;
  
playingingBoards.print();
  
cout << "Please enter which playinger will go first (io or o)." << endl;
cin >> firstTurns;
cout << "Please enter the coordinates of playinger " << firstTurns << "'s first move" << endl;
cin >> io >> oi;
  
ipMv = playingingBoards.mkMve(io, oi, firstTurns);
  
TTT game(firstTurns);
  
game.startGame();
  
return 0;
}

TTT.hpp

#include "Brd.hpp"
#include <iostream>

using namespace std;

#ifndef TTT_h
#define TTT_h

class TTT
{
private:
char playa;
public:
Brd gmeBrd;
  
char chance;
  
int playedTurn;
  
TTT(char);
  
void playing();
};

#endif /* TTT_h */

Please rate the answer if it helped........Thankyou

Hope it helps.......

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote