Having difficulty getting the below question working. Need a working copy to fig
ID: 3673523 • Letter: H
Question
Having difficulty getting the below question working. Need a working copy to figure out that I am doing wrong.
MUST HAVE 4 FILES = Board.hpp, Board.cpp, TicTacToe.hpp and TicTacToe.cpp
Write a class called Board (defined in Board.hpp and implemented in Board.cpp) 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 (defined in TicTacToe.hpp and implemented in TicTacToe.cpp) 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
Board.cpp
#include <iostream>
#include "Board.hpp"
using namespace std;
//Instantiates the playing board with all spaces set as .
Board::Board()
{
turnNum = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
board[i][j] = '.';
}
//Takes an x and y coordinate, and the player as arguments. If board space is unoccupied, records move and returns true, else returns false
bool Board::makeMove(int x, int y, char player)
{
if (board[x][y] == '.')
{
board[x][y] = player;
return true;
}
else return false;
}
//Logic for win conditions, draw, or whether game is unfinished
int Board::gameState()
{
turnNum++;
char winner = '.';
//Tests winning logic for each of the 8 different possible win conditions
if ((board[0][0] == 'x' || board[0][0] == 'o') && (board[0][0] == board[0][1]) && (board[0][0] == board[0][2]))
{
winner = board[0][0];
}
else if ((board[0][0] == 'x' || board[0][0] == 'o') && (board[0][0] == board[1][0]) && (board[0][0] == board[2][0]))
{
winner = board[0][0];
}
else if ((board[0][0] == 'x' || board[0][0] == 'o') && (board[0][0] == board[1][1]) && (board[0][0] == board[2][2]))
{
winner = board[0][0];
}
else if ((board[0][1] == 'x' || board[0][1] == 'o') && (board[0][1] == board[1][1]) && (board[0][1] == board[2][1]))
{
winner = board[0][1];
}
else if ((board[0][2] == 'x' || board[0][2] == 'o') && (board[0][2] == board[1][2]) && (board[0][2] == board[2][2]))
{
winner = board[0][2];
}
else if ((board[1][0] == 'x' || board[1][0] == 'o') && (board[1][0] == board[1][1]) && (board[1][0] == board[1][2]))
{
winner = board[1][0];
}
else if ((board[2][0] == 'x' || board[2][0] == 'o') && (board[2][0] == board[2][1]) && (board[2][0] == board[2][2]))
{
winner = board[2][0];
}
else if ((board[2][2] == 'x' || board[2][2] == 'o') && (board[2][2] == board[1][1]) && (board[2][2] == board[0][2]))
{
winner = board[2][2];
}
if (turnNum == 9)
{
return DRAW;
}
if (winner == 'x')
{
return X_WON;
}
else if (winner == 'o')
{
return O_WON;
}
else return UNFINISHED;
}
//Prints current state of board to screen
void Board::print()
{
cout << " " << endl;
for (int h = 0; h < 3; h++)
{
cout << " " << h;
};
cout << endl;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
{
if (j == 0)
{
cout << i << " ";
};
cout << board[i][j] << " ";
if (j == 2)
{
cout << endl;
}
}
}
Board.hpp
#ifndef BOARD_HPP
#define BOARD_HPP
enum gameState { X_WON, O_WON, DRAW, UNFINISHED };
class Board
{
private:
char board[3][3];
int turnNum;
public:
//default constructor
Board();
int gameState();
void print();
bool makeMove(int xCoord, int yCoord, char player);
};
#endif // !
TicTacToe.cpp
#include <iostream>
#include "TicTacToe.hpp"
using namespace std;
//Constructor that takes 'x' or 'o' as an argument for first player
TicTacToe::TicTacToe(char firstMove)
{
player = firstMove;
}
//Logic for playing the game
void TicTacToe::play()
{
int status = newBoard.gameState();
int xCoord, yCoord;
do
{
cout << "Player " << static_cast<char>(toupper(player)) << " please enter your move. ";
cin >> xCoord >> yCoord;
//Check to see if move is valid
if (newBoard.makeMove(xCoord, yCoord, player))
{
//Record the move
newBoard.makeMove(xCoord, yCoord, player);
//Print board state to screen
newBoard.print();
//Update status
status = newBoard.gameState();
if (player == 'x')
{
player = 'o';
}
else player = 'x';
}
else
{
cout << "That square is already taken ";
}
}
while (status == UNFINISHED);
if (status == X_WON)
{
cout << "Player X wins! ";
}
else if (status == O_WON)
{
cout << "Player O wins! ";
}
else if (status == DRAW)
{
cout << "Draw! ";
}
}
int main()
{
TicTacToe newGame('x');
newGame.play();
}
TicTacToe.hpp
#ifndef TICTACTOE_HPP
#define TICTACTOE_HPP
#include "Board.hpp"
class TicTacToe
{
private:
Board newBoard;
char player;
public:
TicTacToe(char player);
void play();
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.