Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*******************C++ CODE IS WORKING - HELP FIX GAME BOARD - WHEN MAIN FILE IS USED X and O's ARE OFF-CENTER TO THE GAME BOARD -

*******************C++ CODE IS WORKING - HELP FIX GAME BOARD - WHEN MAIN FILE IS USED X and O's ARE OFF-CENTER TO THE GAME BOARD - PROVIDE COMMENTS WITH FIX*********************** SIMPLY FIX BOARD SO THAT X's AND O'S LINE UP CORRECTLY IN THE OUTPUT***********

Please help with writing the code for the following. I'm using C++ and vim. Please include all 4 files required for the project, and only those 4: Board.hpp, Board.cpp, T3Reader.hpp, and T3Reader.cpp. Please write plenty of comments to explain the code. Please also write a main function to accompany these files. 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 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) until someone has won or it's a draw (as indicated by gameState). 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 been won or the board is full. Otherwise it should return true.

Here's an example of the format for the text file:

0 1

2 1

3 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.

Board.hpp =========

#ifndef Board_hpp #define Board_hpp

enum GameStatus { X_WON, O_WON, DRAW, UNFINISHED}; class Board { char grid[3][3]; public: Board(); bool makeMove(char player, int r, int c); GameStatus gameState(); void print(); };

#endif /* Board_hpp */

Board.cpp =========

#include "Board.hpp" #include #include using namespace std; Board::Board() { for(int i = 0 ;i < 3; i++) for(int j = 0; j < 3; j++) grid[i][j] =' '; } bool Board::makeMove(char player, int row, int col) { if(grid[row][col] == ' ') { grid[row][col] = tolower(player); return true; } else return false; } GameStatus Board::gameState() { char symbol=' ';

//top horizontal line if(grid [0][0] != ' ' && grid[0][0] == grid[0][1]&& grid[0][1] == grid[0][2]) symbol = grid[0][0]; //middle horizontal line else if(grid [1][0] != ' ' && grid[1][0] == grid[1][1]&& grid[1][1] == grid[1][2]) symbol = grid[1][0]; //bottom horizontal line else if(grid [2][0] != ' ' && grid[2][0] == grid[2][1]&& grid[2][1] == grid[2][2]) symbol = grid[2][0]; //left vertical line else if(grid [0][0] != ' ' && grid[0][0] == grid[1][0]&& grid[1][0] == grid[2][0]) symbol = grid[0][0]; //middle vertical line else if(grid [0][1] != ' ' && grid[0][1] == grid[1][1]&& grid[1][1] == grid[2][1]) symbol = grid[0][1]; //right verticle line else if(grid [0][2] != ' ' && grid[0][2] == grid[1][2]&& grid[1][2] == grid[2][2]) symbol = grid[0][2]; //diagonal starting at left top else if(grid [0][0] != ' ' && grid[0][0] == grid[1][1]&& grid[1][1] == grid[2][2]) symbol = grid[0][0]; //diagonal starting at right top else if(grid [0][2] != ' ' && grid[0][2] == grid[1][1]&& grid[1][1] == grid[2][0]) symbol = grid[0][2]; else { for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) if(grid[i][j] == ' ') { return UNFINISHED; } return DRAW; }

if(symbol == 'x') return X_WON; else return O_WON; } void Board::print() { cout << endl; //print the column numbers in 1st line cout << " 0 1 2" << endl; for(int i = 0; i < 3; i++) { cout << i << " "; for(int j = 0; j < 3; j++) cout << grid[i][j] << " "; cout << endl; } cout << endl; }

T3Reader.hpp =========

#ifndef T3Reader_hpp #define T3Reader_hpp #include "Board.hpp" #include using namespace std; class T3Reader { Board board; char currPlayer; public: T3Reader(char first); bool readGameFile(string filename); };

#endif /* T3Reader_hpp */

T3Reader.cpp =========

#include "T3Reader.hpp" #include #include #include

using namespace std; T3Reader::T3Reader(char first) { currPlayer = tolower(first); } bool T3Reader::readGameFile(string filename) { ifstream ifs(filename.c_str()); if(!ifs.is_open()) { cout << "Error opening input file " << filename<< endl; return false; } int row, col; while(ifs >> row >> col) { cout << "current player : " << currPlayer << " move: " << row << " " << col << endl; if(board.gameState() != UNFINISHED) //game is over and more data coming from file { cout << "Game is over and there are more moves in file "<< endl; ifs.close(); return false; } if(!board.makeMove(currPlayer, row, col)) { cout << "Board position is already taken " << row<< " " << col << endl; ifs.close(); return false; } else { if(currPlayer == 'x') currPlayer = 'o'; else currPlayer = 'x'; } board.print(); } ifs.close(); board.print(); if(board.gameState() == UNFINISHED) { cout << "Game is not over. There were fewer moves in the file" << endl; return false; } if(board.gameState() == X_WON) cout << "x won!" << endl; else if(board.gameState() == O_WON) cout << "o won!" << endl; else cout << "Its a draw" << endl; return true; }

main.cpp ========= #include "T3Reader.hpp" #include int main() { char firstPlayer; string filename; cout << "Enter the first player (x or o): "; cin >> firstPlayer; cout << "Enter the file name containing game moves: "; cin >> filename; T3Reader reader(firstPlayer); reader.readGameFile(filename);

}

input file: game.txt ================== 0 1 2 1 1 1 2 0 1 2 2 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Samsung Galaxy S23 Ultra Comprehensive User Manual

Authors: Leo Scott

1st Edition

B0BVPBJK5Q, 979-8377286455

More Books

Students also viewed these Databases questions

Question

What could be done to decrease property taxes in our state?

Answered: 1 week ago

Question

love of humour, often as a device to lighten the occasion;

Answered: 1 week ago

Question

orderliness, patience and seeing a task through;

Answered: 1 week ago

Question

well defined status and roles (class distinctions);

Answered: 1 week ago