Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USE ONLY THE CODE GIVEN. DO NOT MAKE YOUR OWN CODE AND ONLY EDIT BOARD.CC You will be implementing a simple version of Tic Tac

USE ONLY THE CODE GIVEN. DO NOT MAKE YOUR OWN CODE AND ONLY EDIT "BOARD.CC"

You will be implementing a simple version of Tic Tac Toe in C++. All the files you need are included in this tgz. In particular, you should only edit the file board.cc (the other files should not be changed). There are comments detailing what the methods should do read those carefully to ensure you handle all the possible cases.

board.cc

#include "board.h" #include // std::exit and EXIT_FAILURE macro #include // std::cout, std::cin, std::cerr, std::endl

using namespace std;

// Constructor // size: the size of the grid (i.e. size x size) // 1. Verify that size is >= 3 and <= 10. If not, print an error message to // cerr and call exit(EXIT_FAILURE) to terminate the program. // 2. Dynamically allocate the 2D array named grid_. If there are any // allocation errors (e.g. bad_alloc exception), print an error // message to cerr and call exit(EXIT_FAILURE) to terminate the program. // 3. Initialize all the values within the 2D array to be ' ' (a space). Board::Board(int size) { // Your code goes here }

// Destructor // 1. De-allocate the 2D array Board::~Board() { // Your code goes here }

// Prints the current board to the screen // The board should have numbers to indicate the rows and columns, and the // current values at each position should be displayed (i.e. blank, X, or O). // This should look something like this (for size=3): // 0 1 2 // +---+---+---+ // 0| X | | O | // +---+---+---+ // 1| O | | X | // +---+---+---+ // 2| | X | O | // +---+---+---+ void Board::Display() { // Your code goes here }

// Marks the player in the given row and column and returns true on success. // If the position is invalid or already occupied, print an error to cerr and // return false. bool Board::Mark(Player player, int row, int column) { // Your code goes here }

// Returns the winner if they have won, or indicates if the game is still in // progress or ended in a stalemate (see enum class Winner for details). Winner Board::CheckWinner() { // Your code goes here }

board.h

#ifndef BOARD_H #define BOARD_H

enum class Player { X, O }; enum class Winner { STILL_PLAYING, X, O, STALEMATE };

class Board { public: // Constructor // size: the size of the grid (i.e. size x size) Board(int size);

// Destructor ~Board();

// Prints the current board to the screen void Display();

// Marks the player in the given row and column and returns true on success. // If the position is invalid or already occupied, print an error to cerr // and return false. bool Mark(Player player, int row, int column);

// Returns the winner if they have won, or indicates if the game is still in // progress or ended in a stalemate (see enum class Winner for details). Winner CheckWinner();

private: int size_; // The size of the grid (i.e. size x size) char** grid_; // The 2D grid for the board };

#endif // BOARD_H

tictactoe.cc

#include "board.h"

#include #include

using namespace std;

int main() { int size; cout << "What size board? "; cin >> size; while (cin.fail()) { cin.clear(); cin.ignore(numeric_limits::max(), ' '); cout << "Invalid data. What size board? "; cin >> size; } cout << endl;

// Create the board Board board(size);

// Display the initial empty board board.Display();

Winner winner; Player player = Player::X; do { int row, column; cout << "Enter a row and column (space separated): "; cin >> row >> column; if (cin.fail()) { cin.clear(); cin.ignore(numeric_limits::max(), ' '); cerr << "Not a valid number. Please try again." << endl; continue; } cout << endl; if (board.Mark(player, row, column)) { // If the action was successful, switch the current player player = (player == Player::X ? Player::O : Player::X); } board.Display(); } while ((winner = board.CheckWinner()) == Winner::STILL_PLAYING);

switch (winner) { case Winner::X: cout << "Player X won!" << endl; break; case Winner::O: cout << "Player O won!" << endl; break; case Winner::STALEMATE: cout << "Stalemate!" << endl; break; default: break; }

return 0; }

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

Step: 3

blur-text-image

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

Linked Data A Geographic Perspective

Authors: Glen Hart, Catherine Dolbear

1st Edition

1000218910, 9781000218916

More Books

Students also viewed these Databases questions