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

C++ Write a class called Board that represents a tic-tac-toe board. It should ha

ID: 3866648 • Letter: C

Question

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

The files must be named: Board.hpp, Board.cpp, TicTacToe.hpp and TicTacToe.cpp

Explanation / Answer

Given below is the code and output for hte question. Please don't forget to rate the answer if it helped. Thank you.

Board.hpp


#ifndef Board_hpp
#define Board_hpp

enum Status {X_WON, O_WON, DRAW, UNFINISHED};

class Board
{
char board[3][3];
Status status;
void updateStatus(char player);
public:
Board();
bool makeMove(int row, int col, char player);
Status gameStatus();
void print();
};
#endif /* Board_hpp */

Board.cpp

#include "Board.hpp"
#include <iostream>
using namespace std;
Board::Board()
{
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
board[i][j] = '.';
  
status = UNFINISHED;
}

bool Board::makeMove(int row, int col, char player)
{
if(gameStatus() == UNFINISHED && board[row][col] == '.') //if the game is not over and the spot is empty
{
board[row][col] = player;
updateStatus(player);
return true;
}
else
return false;
}
void Board::updateStatus(char player)
{
  
bool won = false;
//only when top left , or center of board, or bottom corner is occupied by the player is there a possiblity to win.
//so first check for these locations
if(board[0][0] == player) //top corner corner occupied by player
{
if((board[0][0] == board[0][1] && board[0][1] == board[0][2]) || //top horizontal line
(board[0][0] == board[1][0] && board[1][0] == board[2][0])) //left vertical line
won = true;
  
}
else if(board[1][1] == player)//center occupied
{
if((board[1][0] == board[1][1] && board[1][1] == board[1][2]) || //middle horizontal line
(board[0][1] == board[1][1] && board[1][1] == board[2][1]) ||//middle verticle
(board[0][0] == board[1][1] && board[1][1] == board[2][2]) || //diagonal line starting at left top
(board[2][0] == board[1][1] && board[1][1] == board[0][2])) //diagonal line stating at left bottom
won = true;

}
else if(board[2][2] == player) //bottom right corner occupied
{
if((board[0][2] == board[1][2] && board[1][2] == board[2][2]) || //right verticle line
(board[2][0] == board[2][1] && board[2][1] == board[2][2]) ) //bottom horizontal
won = true;
}
  
if(won)
{
if(player == 'X')
status = X_WON;
else
status = O_WON;
}
else
{
//check if game is over and its draw
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(board[i][j] == '.') //game not over
{
status = UNFINISHED;
return;
}
}
}
  
status = DRAW;
  
}
}
Status Board::gameStatus()
{
return status;
}
void Board::print()
{
cout << " 0 1 2" << endl;
for(int i = 0; i < 3; i++)
{
cout << i << " ";
for(int j = 0; j < 3; j++)
cout << board[i][j] << " ";
cout << endl;
}
cout << endl;
}

TicTacToe.hpp


#ifndef TicTacToe_hpp
#define TicTacToe_hpp
#include "Board.hpp"
class TicTacToe
{
Board board;
char currentPlayer;
void askMove();
public:
TicTacToe(char first);
void play();
void declareStatus();
};


#endif /* TicTacToe_hpp */

TicTacToe,cpp

#include "TicTacToe.hpp"
#include <iostream>
using namespace std;

TicTacToe::TicTacToe(char first)
{
if(first == 'X' || first == 'x')
currentPlayer = 'X';
else
currentPlayer = 'O';
}
void TicTacToe::askMove()
{
int row, col;
do
{
board.print();
cout << "Player " << currentPlayer << ": Enter your move." << endl;
cin >> row >> col;
if(row < 0 || row > 2 || col < 0 || col > 2)
cout << "Invalid position " << endl;
else
{
if(!board.makeMove(row, col, currentPlayer))
cout << "That square is already taken. " << endl;
else
break;
}
}while(true);
}

void TicTacToe::play()
{
while(board.gameStatus() == UNFINISHED)
{
askMove();
//switch players
if(currentPlayer == 'X')
currentPlayer = 'O';
else
currentPlayer = 'X';
  
}
  
declareStatus();
  
}
void TicTacToe::declareStatus()
{
board.print();
if(board.gameStatus() == X_WON)
cout << "X Won!!" << endl;
else if(board.gameStatus() == O_WON)
cout << "O Won!!" << endl;
else
cout << "It's a draw!!" << endl;
}

int main()
{
TicTacToe tictac('X');
tictac.play();
}

output

0 1 2
0 . . .
1 . . .
2 . . .

Player X: Enter your move.
1 1
0 1 2
0 . . .
1 . X .
2 . . .

Player O: Enter your move.
9 0
Invalid position
0 1 2
0 . . .
1 . X .
2 . . .

Player O: Enter your move.
2 0
0 1 2
0 . . .
1 . X .
2 O . .

Player X: Enter your move.
1 2
0 1 2
0 . . .
1 . X X
2 O . .

Player O: Enter your move.
0 2
0 1 2
0 . . O
1 . X X
2 O . .

Player X: Enter your move.
1 0
0 1 2
0 . . O
1 X X X
2 O . .

X Won!!
Program ended with exit code: 0

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