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

Write a class called Board that represents a tic-tac-toe board. It should have a

ID: 3868375 • Letter: W

Question

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

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

Given below is the code for the question. Output is also shown. Please don't forget to rate the answer if it helped. Thank you vey much.

Board.hpp

#ifndef Board_hpp
#define Board_hpp

enum GameStatus { X_WON, O_WON, DRAW, UNFINISHED};
class Board
{
char grid[3][3];

public:
Board();
bool makeMove(char player, int r, int c);
GameStatus gameState();
void print();
};

#endif /* Board_hpp */

Board.cpp

#include "Board.hpp"
#include <iostream>
#include <cctype>
using namespace std;
Board::Board()
{
for(int i = 0 ;i < 3; i++)
for(int j = 0; j < 3; j++)
grid[i][j] ='.';
}
bool Board::makeMove(char player, int row, int col)
{
if(grid[row][col] == '.')
{
grid[row][col] = tolower(player);
return true;
}
else
return false;
}
GameStatus Board::gameState()
{

char symbol='.';

//top horizontal line
if(grid [0][0] != '.' && grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2])
symbol = grid[0][0];
//middle horizontal line
else if(grid [1][0] != '.' && grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2])
symbol = grid[1][0];
//bottom horizontal line
else if(grid [2][0] != '.' && grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2])
symbol = grid[2][0];
//left vertical line
else if(grid [0][0] != '.' && grid[0][0] == grid[1][0] && grid[1][0] == grid[2][0])
symbol = grid[0][0];
//middle vertical line
else if(grid [0][1] != '.' && grid[0][1] == grid[1][1] && grid[1][1] == grid[2][1])
symbol = grid[0][1];
//right verticle line
else if(grid [0][2] != '.' && grid[0][2] == grid[1][2] && grid[1][2] == grid[2][2])
symbol = grid[0][2];
//diagonal starting at left top
else if(grid [0][0] != '.' && grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2])
symbol = grid[0][0];
//diagonal starting at right top
else if(grid [0][2] != '.' && grid[0][2] == grid[1][1] && grid[1][1] == grid[2][0])
symbol = grid[0][2];
else
{

for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
if(grid[i][j] == '.')
{
return UNFINISHED;
}
return DRAW;
}

if(symbol == 'x')
return X_WON;
else
return O_WON;

}
void Board::print()
{
cout << endl;
//print the column numbers in 1st line
cout << " 0 1 2" << endl;
for(int i = 0; i < 3; i++)
{
cout << i << " ";
for(int j = 0; j < 3; j++)
cout << grid[i][j] << " ";

cout << endl;
}

cout << endl;
}

TicTacToe.hpp

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

};

#endif /* TicTacToe_hpp */

TicTacToe.cpp


#include "TicTacToe.hpp"
#include <cctype>
#include <iostream>
using namespace std;
TicTacToe::TicTacToe(char first)
{
currPlayer = tolower(first);
}
void TicTacToe::play()
{
int row, col;
while(board.gameState() == UNFINISHED)
{
board.print();
cout << "player " << currPlayer << ": please enter your move -> ";
cin >> row >> col;
if(!board.makeMove(currPlayer, row, col))
{
cout << "that square is already taken.";
continue;
}
if(currPlayer == 'x')
currPlayer = 'o';
else
currPlayer = 'x';
}
board.print();
GameStatus state = board.gameState();

if(state == X_WON)
cout << "X Won!" << endl;
else if(state == O_WON)
cout << "O Won!" << endl;
else
cout << "It's a draw!" << endl;
}

int main()
{
char player;
do
{
cout << "Who will play first x or o ? ";
cin >> player;
}while(player != 'x' && player != 'o');

TicTacToe tic(player);
tic.play();

return 0;
}

output

$ g++ Board.cpp TicTacToe.cpp
$ ./a.out
Who will play first x or o ? p
Who will play first x or o ? x

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

player x: please enter your move -> 0 0

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

player o: please enter your move -> 1 1

0 1 2
0 x . .
1 . o .
2 . . .

player x: please enter your move -> 2 0

0 1 2
0 x . .
1 . o .
2 x . .

player o: please enter your move -> 0 1

0 1 2
0 x o .
1 . o .
2 x . .

player x: please enter your move -> 1 0
X Won!


==================

$ ./a.out
Who will play first x or o ? o

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

player o: please enter your move -> 1 1

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

player x: please enter your move -> 2 0

0 1 2
0 . . .
1 . o .
2 x . .

player o: please enter your move -> 1 0

0 1 2
0 . . .
1 o o .
2 x . .

player x: please enter your move -> 1 2

0 1 2
0 . . .
1 o o x
2 x . .

player o: please enter your move -> 0 0

0 1 2
0 o . .
1 o o x
2 x . .

player x: please enter your move -> 0 2

0 1 2
0 o . x
1 o o x
2 x . .

player o: please enter your move -> 2 2
O Won!

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