MUST BE IN 4 FILES - Board.hpp , Board.cpp , TicTacToe.hpp and TicTacToe.cpp Wri
ID: 3673046 • Letter: M
Question
MUST BE IN 4 FILES - 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. 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.
The files must be named: Board.hpp, Board.cpp, TicTacToe.hpp and TicTacToe.cpp
Explanation / Answer
working c++ codes
board.hpp:
#include "board.hpp"
class Game
{
private:
Board board;
public:
Game(); // Constructor
~Game(); // Destructor
void Run();
};
board.cpp
#include <stdio.h>
#include <string.h>
#include "board.hpp"
const int DEFAULT_ROWS = 3;
const int DEFAULT_COLS = 3;
/**
* @brief: Makes a standard 3x3 tic-tac-toe board
*/
Board::Board()
{
cols = DEFAULT_COLS;
rows = DEFAULT_ROWS;
/* Create a 3 x 3 2d array */
board = new int*[DEFAULT_ROWS];
for (int i = 0; i < DEFAULT_ROWS; i++)
{
board[i] = new int[DEFAULT_COLS];
}
/* Initializes all the spaces as unoccupied */
for (int i = 0; i < DEFAULT_ROWS; i++)
{
for (int j = 0; i < DEFAULT_COLS; i++)
{
board[i][j] = UNOCCUPIED;
}
}
}
Board::Board(int board_rows, int board_cols)
{
cols = board_rows;
rows = board_cols;
/* Creates a rows x cols 2d array */
board = new int*[rows];
for (int i = 0; i < rows; i++)
{
board[i] = new int[cols];
}
/* Initializes all the spaces as unoccupied */
for (int i = 0; i < rows; i++)
{
for (int j = 0; i < cols; i++)
{
board[i][j] = UNOCCUPIED;
}
}
}
/**
* @brief: Destructor that deletes the two dimensional board array.
*/
Board::~Board()
{
for (int i = 0; i < 3; i++)
{
delete[] board[i];
}
delete[] board;
}
int Board::Query(int row, int col)
{
return board[row][col];
}
void Board::Place(int row, int col, char type)
{
if (type == 'X')
{
board[row][col] = X_OCC;
}
else if (type == 'O')
{
board[row][col] = O_OCC;
}
}
int Board::CheckWin()
{
// States of top row
int top_left = board[0][0];
int top_mid = board[0][1];
int top_right = board[0][2];
// States of middle row
int mid_left = board[1][0];
int mid_mid = board[1][1];
int mid_right = board[1][2];
// States of bottom row
int bottom_left = board[2][0];
int bottom_mid = board[2][1];
int bottom_right = board[2][2];
if ((top_left == X_OCC && top_mid == X_OCC && top_right == X_OCC)
|| (mid_left == X_OCC && mid_mid == X_OCC && mid_right == X_OCC)
|| (bottom_left == X_OCC && bottom_mid == X_OCC
&& bottom_right == X_OCC)
|| (top_left == X_OCC && mid_left == X_OCC && bottom_left == X_OCC)
|| (top_mid == X_OCC && mid_mid == X_OCC && bottom_mid == X_OCC)
|| (top_right == X_OCC && mid_right == X_OCC
&& bottom_right == X_OCC)
|| (top_left == X_OCC && mid_mid == X_OCC && bottom_right == X_OCC)
|| (top_right == X_OCC && mid_mid == X_OCC && bottom_left == X_OCC))
{
// Check if player "X" has won
return 1; // Player 1 is X
}
else if ((top_left == O_OCC && top_mid == O_OCC && top_right == O_OCC)
|| (mid_left == O_OCC && mid_mid == O_OCC && mid_right == O_OCC)
|| (bottom_left == O_OCC && bottom_mid == O_OCC
&& bottom_right == O_OCC)
|| (top_left == O_OCC && mid_left == O_OCC && bottom_left == O_OCC)
|| (top_mid == O_OCC && mid_mid == O_OCC && bottom_mid == O_OCC)
|| (top_right == O_OCC && mid_right == O_OCC
&& bottom_right == O_OCC)
|| (top_left == O_OCC && mid_mid == O_OCC && bottom_right == O_OCC)
|| (top_right == O_OCC && mid_mid == O_OCC && bottom_left == O_OCC))
{
// Check if player "Y" has won
return 2; //Player 2 is O
}
return 0; // Nobody has won
}
int Board::CheckDraw()
{
// Check if board is full
int full_board = 1;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (board[i][j] == UNOCCUPIED)
{
full_board = 0;
}
}
}
if (full_board && !CheckWin())
{
return 1;
}
return 0;
}
void Board::Print(int row, int col)
{
printf("%d ", board[row][col]);
}
tictactoe.cpp
#include <stdio.h>
#include "tictactoe.hpp"
int main(int argc, char ** argv)
{
Game game;
game.Run();
return 0;
}
tictactoe.hpp
#include "board.hpp"
class Game
{
private:
Board board;
public:
Game(); // Constructor
~Game(); // Destructor
void Run();
};
#include "board.hpp"
class Game
{
private:
Board board;
public:
Game(); // Constructor
~Game(); // Destructor
void Run();
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.