Question
Write a class called Board that represents a tic-tac-toe board. It should have a 3x3 array of chars ('x', 'o', or a space, where a
Write a class called Board that represents a tic-tac-toe board. It should have a 3x3 array of chars ('x', 'o', or a space, where a space would represent an empty square) 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 (each element set to a space character). It should have a method called makeMove that takes two ints and a char (either 'x' or 'o') as parameters, representing the x and y coordinates of the move (see the example below) and which player's turn it is. 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 class, not inside it). [Optional: write a method called print, which just prints out the current board to the screen - this is not required, but will very likely be useful for debugging.] Write a class called T3Reader that uses the Board class to re-run a game of TicTacToe from moves that it reads from a text file. 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 readGameFile that takes a string parameter that gives the name of the game file. The readGameFile method should keep looping, reading a move from the file, and sending it to the board (with makeMove). The readGameFile method should return false if any of the moves is for a square that was already occupied, or if there are still additional moves in the file after the game has finished. Otherwise it should return true. Here's an example of the format for the text file: 0 1 2 1 2 0 1 2 and so on. Which coordinate is the row and which is the column doesn't matter as long as you're consistent. The files must be named: Board.cpp, Board.hpp, T3Reader.cpp, T3Reader.hpp. *Need help figuring out the gameState function specifically in the Board.cpp file
I've got the following program:
// Board.hpp
#ifndef BOARD_HPP_
#define BOARD_HPP_
# include
using namespace std;
// enum storing the status of the game
enum Status{
X_WON,
O_WON,
DRAW,
UNFINISHED
};
// class Board storing the board which is 3X3 array
class Board {
private:
char board[3][3];
public:
// default constructor - initializes empty board
Board();
// makeMove which checks if move if valid and if valid makes that move
bool makeMove(int row,int col,char user);
// checks the status of the game
Status gameState();
// print the board
void printBoard();
};
#endif /* BOARD_HPP_ */
// end of Board.hpp
// Board.cpp
# include "Board.hpp"
Board::Board()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
board[i][j]=' ';
}
bool Board ::makeMove(int row,int col,char user)
{
if(row<0 || row>2 || col<0 || col>2)
{
cout<<"Row1 "< return false; } if(board[row][col] != ' '){ return false; } board[row][col]=user; return true; } Status Board:: gameState() { bool gameFinished = true; // check if there is a winner row-wise or column-wise for(int i=0;i<3;i++) { if((board[i][0] == board[i][1]) && (board[i][0] == board[i][2]) && (board[i][0] == 'X')) return X_WON; if((board[i][0] == board[i][1]) && (board[i][0] == board[i][2]) && (board[i][0] == 'O')) return O_WON; if((board[0][i] == board[1][i]) && (board[0][i] == board[2][i]) && (board[0][i] == 'X')) return X_WON; if((board[0][i] == board[1][i]) && (board[0][i] == board[2][i]) && (board[0][i] == 'O')) return O_WON; } // check if there is a winner diagonal-wise if((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[0][0] == 'X')) return X_WON; if((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[0][0] == 'O')) return O_WON; if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] == 'X')) return X_WON; if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0]) && (board[1][1] == 'O')) return O_WON; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(board[i][j] != 'X' && board[i][j] !='O') { gameFinished = false; break; } } } if(!gameFinished) return UNFINISHED; return DRAW; } void Board:: printBoard() { for(int i=0;i<3;i++) { cout< for(int j=0;j<3;j++){ if(j==2) cout< else cout< } if(i!=2) cout<<" ------------"; } cout<<" "; } // end of Board.cpp // T3Reader.hpp #ifndef T3READER_HPP_ #define T3READER_HPP_ # include #include "Board.hpp" class T3Reader{ private: Board board; // board object char playerTurn; // indicates player's turn public: // default constructor T3Reader(); // parameterized constructor T3Reader(char playerTurn); // read the moves from the file and play the game bool readGameFile(string filename); }; #endif /* T3READER_HPP_ */ // end of T3Reader.hpp // T3Reader.cpp # include "T3Reader.hpp" T3Reader::T3Reader() { playerTurn='X'; } T3Reader::T3Reader(char playerTurn) { T3Reader::playerTurn = playerTurn; } bool T3Reader:: readGameFile(string filename) { ifstream inFile(filename.c_str()); int row, col; // if file can be opened if(inFile.is_open()) { // read till the end of file while(!inFile.eof()) { board.printBoard(); cout<<" Player "< inFile>>row>>col; bool move = board.makeMove(row,col,playerTurn); if(!move){ inFile.close(); return false; } if(playerTurn == 'X') playerTurn = 'O'; else playerTurn = 'X'; if(board.gameState() == X_WON || board.gameState() == O_WON || board.gameState() == DRAW) { if(!inFile.eof()){ inFile.close(); return false; } board.printBoard(); if(board.gameState() == X_WON) cout<<" Player X won"; else if(board.gameState() == O_WON) cout<<" Player O won"; else cout<<" Its a draw"; inFile.close(); return true; } } }else{ cout<<" Unable to open file "< return false; } } // end of T3Reader.cpp // main.cpp # include "Board.hpp" # include "T3Reader.hpp" # include using namespace std; // class to play the game of tic tac toe int main() { T3Reader gamePlay('X'); string filename; cout<<" Enter the file name : "; cin>>filename; bool result = gamePlay.readGameFile(filename); if(!result) cout<<" Invalid moves in file"; } // end of main.cpp I keep getting this problem: Debug Info: Your instructor has chosen to give you the following information to help you debug your code and improve your submission. Input of the test case Your code's output The correct output of the test case #include "Board.cpp" T3Reader game('x'); ASSERT_TRUE(game.readGameFile("gameFile"));
[==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from MimirTest [ RUN ] MimirTest.cppUnitTest | | ------------ | | ------------ | | Player x turn : x | | ------------ | | ------------ | | Player X turn : x | | ------------ X | | ------------ | | Player O turn : x |O | ------------ X | | ------------ | | Player X turn : x |O | ------------ X |X | ------------ | | Player O turn : x |O |O ------------ X |X | ------------ | | Player X turn :./tests.cpp:18: Failure Value of: game.readGameFile("gameFile") Actual: false Expected: true [ FAILED ] MimirTest.cppUnitTest (1 ms) [----------] 1 test from MimirTest (1 ms total) [----------] Global test environment tear-down [==========] 1 test from 1 test case ran. (1 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] MimirTest.cppUnitTest 1 FAILED TEST
None
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started