C++. The files must be named: Board.hpp, Board.cpp, TicTacToe.hpp and TicTacToe.
ID: 3866872 • Letter: C
Question
C++.
The files must be 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. 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
Board.hpp
#ifndef _Board_h
#define _Board_h
// enum returns the game state in the game
enum game_state_enum {X_WON, O_WON, DRAW, UNFINISHED};
class Board
{
public:
char board_array[3][3]; // 3x3 array store the locations of the players' moves
Board(); // default constructor that initializes the 3x3 array to being empty
bool makeMove(int, int, char); // takes the x and y coordinates of the move which player's turn it is as parameters
int gameState(); //takes no parameters and returns game_state_enum
void print(); //prints out the current board to the screen.
char turn; //store which player turn
int total_turns; //stores total number of turns of player
};
#endif
Board.cpp
#include "Board.hpp"
#include <iostream>
using namespace std;
// default constructor that initializes the 3x3 array to being empty
Board::Board()
{
int i,j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
board_array[i][j] = '.';
}
}
total_turns = 0;
}
//takes the x, y coordinates and turn as parameter and
//checks if the coordinates entered by the user are valid or not
//value by the function id return in type of true or false(boolean)
bool Board::makeMove(int x, int y, char whos_turn)
{
if (whos_turn == 'x' || whos_turn == 'X')// if this is Player X's turn
{
if (board_array[x][y] == '.' || board_array[x][y] != 'o') // checks if the X choice available
{
board_array[x][y] = 'x'; //if true assign the position to player
return true; // and return true
}
}
else if (whos_turn == 'o' || whos_turn == 'O') // if this is Player O's turn
{
if (board_array[x][y] == '.' || board_array[x][y]!= 'x') // checks if the O choice available
{
board_array[x][y] = 'o'; //if true assign the position to player
return true; // and return true
}
}
else //else return false if space is already ocuupied or not available
return false;
return 0;
}
//checks on the 4 possible states of the game:
//x wins, o wins,draw,or unfinished.
int Board::gameState()
{
int total_turns = 0;
for (int i = 0; i < 3; i++) //loop to check the game state
{
// to check if there is a winner
if ( (board_array[i][0] == board_array[i][1] && board_array[i][1] == board_array[i][2]) ||
(board_array[0][i] == board_array[1][i] && board_array[1][i] == board_array[2][i]) ||
(board_array[0][0] == board_array[1][1] && board_array[1][1] == board_array[2][2]) ||
(board_array[0][2] == board_array[1][1] && board_array[1][1] == board_array[2][0]))
{
//checking if X is winner
if ( (board_array[i][0] == 'x' && board_array[i][1] == 'x' && board_array[i][2] == 'x') ||
(board_array[0][i] == 'x' && board_array[1][i] == 'x' && board_array[2][i] == 'x') ||
(board_array[0][0] == 'x' && board_array[1][1] == 'x' && board_array[2][2] == 'x') ||
(board_array[0][2] == 'x' && board_array[1][1] == 'x' && board_array[2][0] == 'x'))
return X_WON; //if X is winner return
//checking if X is winner
else if ( (board_array[i][0] == 'o' && board_array[i][1] == 'o' && board_array[i][2] == 'o') ||
(board_array[0][i] == 'o' && board_array[1][i] == 'o' && board_array[2][i] == 'o') ||
(board_array[0][0] == 'o' && board_array[1][1] == 'o' && board_array[2][2] == 'o') ||
(board_array[0][2] == 'o' && board_array[1][1] == 'o' && board_array[2][0] == 'o'))
return O_WON; //if X is winner return
}
}
if (total_turns == 9) // if all moves are made by both players
return DRAW; //return game state as draw
else //if nothing statisfy then
return UNFINISHED; //return game state as unfinished
return 0;
}
//prints out the current board to the screen.
void Board::print()
{
cout << endl << " " << "0" << " " << "1" << " " << "2" << endl;
cout << "0" << " " << board_array[0][0] << " " << board_array[0][1] << " " << board_array[0][2] << endl;
cout << "1" << " " << board_array[1][0] << " " << board_array[1][1] << " " << board_array[1][2] << endl;
cout << "2" << " " << board_array[2][0] << " " << board_array[2][1] << " " << board_array[2][2] << endl;
}
TicTacToe.hpp
#include "Board.hpp"
#include <iostream>
using namespace std;
#ifndef _TicTacToe_h
#define _TicTacToe_h
class TicTacToe
{
private:
char whos_turn; //store which player turn
public:
Board gameBoard; //object of Board class
TicTacToe(char); //TicTacToe constructor which takes whos_turn as input
void play(); //game play function
};
#endif
TicTacToe.cpp
#include "Board.hpp"
#include "TicTacToe.hpp"
#include <iostream>
using namespace std;
//constructor that stores value of whos_turn to the input value entered by user
TicTacToe::TicTacToe(char input_turn)
{
whos_turn = input_turn;
}
//play method keep looping, asking the correct player for their move
//and sends 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.
void TicTacToe::play()
{
int x, y, game_state;
char input_move;
while(gameBoard.gameState() == UNFINISHED) //continue loop when game state is unfinished
{
gameBoard.print(); //print the game board
cout << "Player " << whos_turn << ": ";
cout << "please enter your move." << endl;
cin >> x >> y;
input_move = gameBoard.makeMove(x, y, whos_turn); //makeMove will return if input move is valid or not
if (input_move == true) //if input move is valid
{
gameBoard.total_turns++; //increase the number of turns of the player
if (whos_turn == 'x') //if it was X player turn
whos_turn = 'o'; //update the whos_turn to O player turn
else if (whos_turn == 'o') //if it was X player turn
whos_turn = 'x'; //update the whos_turn to O player turn
}
if (input_move == false) //if input move is invalid
cout << "That space is taken." << endl; //print error message
}
game_state = gameBoard.gameState(); //get the game_state
if (game_state == 0) // if X player is winner
{
gameBoard.print(); //print the game board
cout << "***** x wins!!! *****" << endl; //print the winner
}
else if (game_state == 1) // if O player is winner
{
gameBoard.print(); //print the game board
cout << "***** o wins!!! *****" << endl; //print the winner
}
else if (game_state == 2) //if game state is draw
{
gameBoard.print(); //print the game board
cout << "***** The game is a draw! *****" << endl; //print thr draw message
}
}
//asks the user which player should go first, creates a new TicTacToe object and starts the game.
int main()
{
char input_turn;
Board gameBoard;
gameBoard.print(); // Print the game board
cout << "Please enter which player will go first (x or o)." << endl;
cin >> input_turn;
TicTacToe game(input_turn);
game.play();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.