Please help with writing the code for the following. I\'m using C++ and vim. Ple
ID: 3868516 • Letter: P
Question
Please help with writing the code for the following. I'm using C++ and vim. Please include plenty of comments that explain what's going on in the code. Thanks!
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 printwhich just prints out the current board to the screen. Write a class called Tic Tac Toe 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 players 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 Tic Tac Toe.cpp) that asks the user which player should go first, creates a new Tic Tac Toe 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 Player o: please enter your move.Explanation / Answer
player.h
#ifndef PLAYER_H
#dene PLAYER_H
class Board;
class Player
{
public:
Player();
char getValues() const;
void MoveValues(Board& _boardObj) const;
private:
static const int TOTAL_PLAYERS = 2;
static const char PLAYERS[TOTAL_PLAYERS];
static int _curr;
char m_player;
};
#endif
--------------------------------------------------------------------------------------------------
player.cpp
#include "player.h"
#include "board.h"
#include <iostream>
using namespace std;
const char Player::PLAYERS[TOTAL_PLAYERS] = {'X', 'O'};
int Player::_curr = 0;
Player::Player()
{
m_player = PLAYERS[_curr];
_curr = (_curr + 1) % TOTAL_PLAYERS;
}
char Player::getValues() const
{
return m_player;
}
void Player::MoveValues(Board& _boardObj) const
{
int move;
do
{
cout << "Player " << getValues();
cout << ", Enter your move (0-8): ";
cin >> move;
} while (!_boardObj.checkLegalMove(move));
_boardObj.onMoveReceived(getValues(), move);
}
-----------------------------------------------------------------------------------------------------------------
Board.h
#ifndef BOARD_H
#define BOARD_H
class Board
{
public:
Board();
bool IsCompletelyFilled() const;
bool checkLegalMove(int move) const;
bool checkWinner(char player) const;
void Print() const;
void destroy();
void onMoveReceived(char player, int move);
static const int TOTAL_SQUARES = 9;
static const char EMPTYVAL = '.';
private:
static const int TOTAL_COMBOS = 8;
static const int NUM_ROWS = 3;
static const int WINNING_COMBINATIONS[TOTAL_COMBOS]
[NUM_ROWS];
char m_Squares[TOTAL_SQUARES];
};
#endif
--------------------------------------------------------------------------------------------------------------------------
Board.cpp
#include "board.h"
#include <iostream>
using namespace std;
const int Board::WINNING_COMBINATIONS[TOTAL_COMBOS]
[NUM_ROWS] = { {0, 1, 2},{3, 4, 5},{6, 7, 8},{0, 3, 6},{1, 4, 7},{2, 5, 8},{0, 4, 8}, {2, 4, 6} };
Board::Board()
{
destroy();
}
bool Board::IsCompletelyFilled() const
{
bool _full = true;
int i = 0;
while (_full && i < TOTAL_SQUARES)
{
if (m_Squares[i] == EMPTYVAL)
{
_full = false;
}
++i;
}
return _full;
}
bool Board::checkLegalMove(int move) const
{
return (move >= 0 && move < TOTAL_SQUARES && m_Squares[move] == EMPTYVAL);
}
bool Board::checkWinner(char player) const
{
bool winner = false;
int i = 0;
while (!winner && i < TOTAL_COMBOS)
{
int piecesInCombo = 0;
for (int j = 0; j < NUM_ROWS; ++j)
{
if (m_Squares[WINNING_COMBINATIONS[i][j]] == player)
{
++piecesInCombo;
}
}
if (piecesInCombo == NUM_ROWS)
{
winner = true;
}
++i;
}
return winner;
}
void Board::Print() const
{
cout << endl << " " << m_Squares[0] << " | " << m_Squares[1];
cout << " | " << m_Squares[2];
cout << endl << " " << "---------";
cout << endl << " " << m_Squares[3] << " | " << m_Squares[4];
cout << " | " << m_Squares[5];
cout << endl << " " << "---------";
cout << endl << " " << m_Squares[6] << " | " << m_Squares[7];
cout << " | " << m_Squares[8];
cout << endl << endl;
}
void Board::destroy()
{
for (int i=0; i<TOTAL_SQUARES; ++i)
{
m_Squares[i] = EMPTYVAL;
}
}
void Board::onMoveReceived(char player, int move)
{
m_Squares[move] = player;
}
-----------------------------------------------------------------------------------------------------------------------------------
Game.h
#ifndef GAME_H
#define GAME_H
#include "board.h"
#include "player.h"
class Game
{
public:
Game();
bool StillPlaying() const;
bool GameTie() const;
void Instructions() const;
void playerNext();
void PrintWinner() const;
void Play();
private:
static const int TOTAL_PLAYERS = 2;
static const int FIRST = 0;
static const int SECOND = 1;
Board m_Board;
Player m_Players[TOTAL_PLAYERS];
int m_Current;
};
#endif
--------------------------------------------------------------------------------------------------------------------------------------
Game.cpp
#include "game.h"
#include <iostream>
using namespace std;
Game::Game():
m_Current(FIRST)
{}
bool Game::StillPlaying() const
{
return ( !m_Board.IsCompletelyFilled() &&!m_Board.checkWinner(m_Players[FIRST].getValues()) &&
!m_Board.checkWinner(m_Players[SECOND].getValues()) );
}
bool Game::GameTie() const
{
return ( m_Board.IsCompletelyFilled() &&!m_Board.checkWinner(m_Players[FIRST].getValues()) &&
!m_Board.checkWinner(m_Players[SECOND].getValues()) );
}
void Game::Instructions() const
{
cout << " welcome to tic-tac-toe";
cout << endl << endl;
cout << "Enter number (0-8)" << endl;
cout << endl << " " << "0 | 1 | 2";
cout << endl << " " << "---------";
cout << endl << " " << "3 | 4 | 5";
cout << endl << " " << "---------";
cout << endl << " " << "6 | 7 | 8";
cout << endl << endl;
}
void Game::playerNext()
{
m_Current = (m_Current + 1) % TOTAL_PLAYERS;
}
void Game::PrintWinner() const
{
cout << "The game is completed..";
cout << endl;
if (GameTie())
{
cout << "Its tie.";
cout << endl;
}
else
{
cout << "Winner is player";
if (m_Board.checkWinner(m_Players[FIRST].
getValues()))
{
cout << m_Players[FIRST].getValues() << "!";
cout << endl;
}
else
{
cout << m_Players[SECOND].getValues() << "!";
cout << endl;
}
}
}
void Game::Play()
{
m_Current = FIRST;
m_Board.destroy();
while (StillPlaying())
{
m_Board.Print();
m_Players[m_Current].MoveValues(m_Board);
playerNext();
}
m_Board.Print();
PrintWinner();
}
---------------------------------------------------------------------------------------------------------------------------------
main.cpp
#include "game.h"
#include <iostream>
using namespace std;
int main()
{
Game ticTacToe;
ticTacToe.Instructions();
char playAgain;
do
{
ticTacToe.Play();
cout << endl << "want to play again? (y/n): ";
cin >> playAgain;
} while (playAgain != 'n');
return 0;
}