Menu

(Solved) : Reversi Game Help Student Write Functions Reversih Ifndef Reversihpp Define Reversihpp Inc Q44106824 . . .

REVERSI GAME HELP WITH STUDENT TO WRITE FUNCTIONS

We will change a few rules from those listed on the above site. 1. The game ends when the current player does not have a legaC/C++ Background 1. Enumerations: enumerations in C/C++ provide an easy method of declaring named integer constants. We will

reversi.h

#ifndef REVERSI_HPP#define REVERSI_HPP#include iostream#include vector/*******************************************************//* Add destructors, copy constructors, and assignment *//* operators to any class that requires one. *//*******************************************************//** * Square class models a location in Reversi * that can either be white, black, or free * using an enumerated type. */struct Square { /** * Enumeration values act as named integer constants. * So Square::FREE acts as an integral constant 0, * Square::WHITE acts as an integral constant 1, and * Square::BLACK acts as an integral constant 2. */ enum SquareValue { FREE = 0, WHITE, BLACK }; // Data member SquareValue value_; /** * Constructor using default arguments to allow * no initial value that will default to FREE */ Square(SquareValue value = FREE) : value_(value) { } /*——————- STUDENT TO WRITE —————–*/ /** * Assigns the internal SquareValue enumeration * to the provided input argument. * Note: The input is a SquareValue not a full Square */ Square& operator=(SquareValue value); /*——————- STUDENT TO WRITE —————–*/ /** * Comparison operators to compare a Square * to a SquareValue enumeration value (i.e. * Square::FREE, Square::WHITE, Square::BLACK) * Note: We are comparing a SquareValue not a full Square */ bool operator==(SquareValue value) const; bool operator!=(SquareValue value) const; /** * Flip the current SquareValue from WHITE to BLACK or * BLACK to WHITE */ void flip();};/*——————- STUDENT TO WRITE —————–*//** * ostream operator for a Square object that outputs * ‘-‘ for FREE, ‘B’ for BLACK, and ‘W’ for WHITE. * Note: Since data members in the Square struct are public * we do not need to make this a friend function. */std::ostream& operator<<(std::ostream& out, const Square& square);/** * Global helper to flip BLACK to WHITE and vice versa */Square::SquareValue opposite_color(Square::SquareValue value);/** * Board class models the 2D Reversi board * as an array of array of Squares. Uses * letters starting from ‘a’ to identify rows * and integers starting from 1 to identify * columns. */class Board {public: /*——————- STUDENT TO WRITE —————–*/ /** * Initializing constructor */ Board(size_t s); /** Access private size as read-only. */ size_t dimension() const { return dimension_; } /** * To allow access to an internal square, we overload * the operator(). * Given a board, b, this allows square access as `b(‘a’,1);` * Will throw std::out_of_range if the row or column are * out of bounds. */ Square& operator()(char row, size_t column); Square const& operator()(char row, size_t column) const; /** * Checks if the square value at the specified row and col are legal * locations and either opposite or the same as the square value * specified by `turn`. */ bool is_legal_and_opposite_color(char row, size_t column, Square::SquareValue turn) const; bool is_legal_and_same_color(char row, size_t column, Square::SquareValue turn) const; /** * Outputs the board */ std::ostream& print(std::ostream& out) const; /*——————- STUDENT TO WRITE —————–*/ /** * Outputs the board * Should be a simple wrapper for Board::print(). */ friend std::ostream& operator<<(std::ostream& out, const Board& board);private: // Our actual board representation size_t dimension_; // Dimension Square** squares_; // 2D Square array /** * Private helper to convert ‘a’ to 0, ‘b’ to 1, etc. */ size_t row_to_index(char row) const { return (size_t)(row – ‘a’); } /** * Private helper to check if a move can be made. */ bool is_valid_location(char row, size_t column) const;};/** * Stores a board configuration (deep copy) and the current player’s turn. */struct Checkpoint { Board board_; Square::SquareValue turn_; /// Constructor Checkpoint(const Board& b, Square::SquareValue t);};/** * Reversi game class */class Reversi {public: /*——————- STUDENT TO WRITE —————–*/ /** * Constructs the board of a given side length (dimension) * Should set the middle 2×2 elements of the board to: * BW * WB */ Reversi(size_t size); /*——————- STUDENT TO WRITE —————–*/ /** * Play the entire game. * Uses private helper functions to modularize the code. */ void play();private: /** * Prints the board and prompt for the next input/turn. */ void prompt() const; /** * Prints the Win/Loss/Tie message using wcnt (white count) * and bcnt (black count) */ void win_loss_tie_message(size_t white_count, size_t black_count); /** * Returns true if no legal move exists for the current player * given by current data member `turn_` */ bool is_game_over() const; /** * Determines whether player given by `turn` can legally place at the * specified row and column. Will throw std::out_of_range if the * row or column are out of bounds. */ bool is_legal_choice(char row, size_t column, Square::SquareValue turn) const; /*——————- STUDENT TO WRITE —————–*/ /** * Makes a checkpoint of the current board and player turn * and saves it in the history vector */ void save_checkpoint(); /*——————- STUDENT TO WRITE —————–*/ /** * Overwrites the current board and player turn with the * latest saved checkpoint. If no checkpoint is available * simply return. */ void undo(); /* You may add other private helper functions */private: // You do not need to add additional data members, but // may add them, provided they don’t take the place of // or circumvent the purpose of the data members below. /// Current board state Board board_; /// Current player’s ID (WHITE or BLACK) Square::SquareValue turn_; /// Saved checkpoints std::vector history_;};#endif

reversi.cpp

#include vector#include stdexcept#include “reversi.h”using namespace std;void Square::flip(){ switch (value_) { case WHITE: value_ = BLACK; break; case BLACK: value_ = WHITE; break; default: break; }}Square::SquareValue opposite_color(Square::SquareValue value){ switch (value) { case Square::WHITE: return Square::BLACK; case Square::BLACK: return Square::WHITE; default: throw invalid_argument(“Illegal square value”); }}Square& Board::operator()(char row, size_t column){ if (!is_valid_location(row, column)) { throw out_of_range(“Bad row index”); } size_t row_index = row_to_index(row); return squares_[row_index][column – 1];}Square const& Board::operator()(char row, size_t column) const{ if (!is_valid_location(row, column)) { throw out_of_range(“Bad row index”); } size_t row_index = row_to_index(row); return squares_[row_index][column – 1];}bool Board::is_legal_and_opposite_color( char row, size_t column, Square::SquareValue turn) const{ if (is_valid_location(row, column)) { size_t row_index = row_to_index(row); return squares_[row_index][column – 1] != Square::FREE && squares_[row_index][column – 1] != turn; } return false;}bool Board::is_legal_and_same_color( char row, size_t column, Square::SquareValue turn) const{ if (is_valid_location(row, column)) { size_t row_index = row_to_index(row); return squares_[row_index][column – 1] == turn; } return false;}bool Board::is_valid_location(char row, size_t column) const{ size_t row_index = row_to_index(row); return row_index < dimension_ && column – 1 < dimension_;}Checkpoint::Checkpoint(const Board& b, Square::SquareValue turn) : board_(b), turn_(turn){}ostream& Board::print(ostream& out) const{ out << ” “; for (size_t i = 1; i <= dimension_; i++) { if (i < 10) { out << ” “; } else { out << (i / 10); } } out << endl; out << ” “; for (size_t i = 1; i <= dimension_; i++) { out << (i % 10); } out << endl; for (size_t i = 0; i < dimension_; i++) { out << (char)(‘a’ + i) << ‘:’; for (size_t k = 0; k < dimension_; k++) { out << squares_[i][k]; } out << endl; } return out;}void Reversi::prompt() const{ cout << board_ << endl; cout << (turn_ == Square::BLACK ? “B” : “W”); cout << ” – Enter ‘p r/c’, ‘q’, ‘c’, ‘u’:” << endl;}void Reversi::win_loss_tie_message(size_t white_count, size_t black_count){ cout << board_ << endl; if (white_count > black_count) { cout << “W wins” << endl; } else if (white_count < black_count) { cout << “B wins” << endl; } else { cout << “Tie” << endl; } cout << “W=” << white_count << “/B=” << black_count << endl;}bool Reversi::is_legal_choice(char row, size_t column, Square::SquareValue turn) const{ // Vectors for each cardinal direction const size_t direction_count = 8; const int direction_row[] = {-1, -1, 0, +1, +1, +1, 0, -1}; const int direction_column[] = { 0, -1, -1, -1, 0, +1, +1, +1}; // Make sure location is free if (board_(row, column) != Square::FREE) { return false; } // Now check in each directions for (size_t d = 0; d < direction_count; d++) { // Where we’re checking char cursor_row = row + direction_row[d]; size_t cursor_column = column + direction_column[d]; // Move towards the direction while we’re on the opposite color bool found_opposite = false; while (board_.is_legal_and_opposite_color(cursor_row, cursor_column, turn_)) { found_opposite = true; cursor_row += direction_row[d]; cursor_column += direction_column[d]; } // Check if the next thing after is our color bool found_same = board_.is_legal_and_same_color(cursor_row, cursor_column, turn_); // If this direction is valid, the move is valid, so short circuit and return if (found_opposite && found_same) { return true; } } return false;}bool Reversi::is_game_over() const{ for (unsigned char row = ‘a’; row < ‘a’ + board_.dimension(); row++) { for (size_t column = 1; column <= board_.dimension(); column++) { if (is_legal_choice(row, column, turn_)) { return false; } } } return true;} We will change a few rules from those listed on the above site. 1. The game ends when the current player does not have a legal move (rather than ending the game only when BOTH players do not have a legal move). The winner is still the one with the most pieces matching their color. 2. We will support boards of EVEN-number sizes from 4×4 to 26×26 (and not just 8×8). The user will specify the dimension of the board at the command lines (e.g. ./test-reversi 4 should create and play a 4×4 version of the game). o Any provided dimension that is ODD or outside the range of 4 to 26 should cause Invalid size to be printed and the program should exit by returning 1 from maino o If no command line argument is provided, default to a 4×4 game board. Additional features and game play: 1. Without a GUI, we will require simple console I/O. Thus, we will output the current board state before accepting each players selection on their turn. The logic to output the board is already provided by Board::print (though we will want to support the operator<< which is left for you to add). The board will be displayed using B and w for black and white, respectively On each turn, the player can choose between 4 options: p = play a piece, c = save a checkpoint, u = undo to the previous checkpoint, or a = quit. Thus, the user will input either • p lay by specifying the coordinates of the location to place their piece. The coordinate system will use lower-case letters a, b,… for rows and numbers 1, 2, … for columns. Some whitespace will always separate the p command and the coordinate (e.g. b). The coordinate shall not have whitespace separating the single character row coordinate and the integer column coordinate. You do NOT need to check for errors in this format but may assume correct input format is always used. If an out-of-range row or column are entered, an exception should be thrown and the program will crash/fail. This is allowed. If a valid row/column are entered but that location is not a legal location (where at least 1 of the opponent’s pieces is captured), simply ignore the command and repeat the process of displaying the board and prompt • checkpoint the current game state by saving the current board and whose turn it is. Multiple checkpoints may be generated and must be stored. • undo the current game state by restoring the game state to the last saved checkpoint (note that we said last checkpoint not oldest checkpoint). After being restored, the checkpoint should be removed from the stored checkpoints (since we just consumed it with the undo operation). If u is entered and no checkpoint is available, simply ignore the command. • quit (or any other character beside P, C, u) should cause the program to immediately and correctly display the end of game information (using the provided win_loss_tie_msg(int went, int bent) function), clean up any resources, and exit the program. • We belive the above instructions fully enumerate error cases that you need to handle. If you do have questions, post on the class Q&A site. 1. The game should quit automatically when the current player has no legal location. It should NOT even prompt the player at this point but simply display the end of game information (using the provided win_loss_tie_msg(int went, int bont) function), clean up any resources, and exit the program. 2. Unlike the linked online game, the first turn should be taken by the BLACK piece. 3. You need to provide the code that actually does the appropriate flipping of pieces (updated of the Board) each time a new pieces is played. 4. You do NOT need to catch exceptions that we throw. C/C++ Background 1. Enumerations: enumerations in C/C++ provide an easy method of declaring named integer constants. We will use an enumerated value for the state of a Square as FREE=0, WHITE=1, or BLACK=2. For more information, search online or start at this site. Additional Requirements: 1. You may not allocate arrays larger than needed (i.e. you cannot just use a 26×26 array for all games) but must allocate arrays that match the size specified by the user. 2. You may NOT use std::vector<t> for storing the Board. You MAY use it to to store the checkpoints. 3. You may not allocate variable size arrays ON THE STACK. Recall, that in C/C++, arrays whose size depends on a runtime variable should be allocated on the heap. 4. You may not alter the general class structure provide or any of the given member function prototypes but may add additional global or private member functions. The only public member functions you may add are destructors, copy constructors, and/or assignment operators. 5. You may not alter any provided code given. If that code calls a specific function or uses an operator that is not defined, you must define that function. 6. std::stringstream is allowed. In addition, you MAY use functions from <cmath> and <cstdlib> but nothing from <algorithm> except std:: max(and std::min() 7. Read the comments/documentation in the provided skeleton code. They acts as REQUIREMENTS just as much as these instructions and will lead to deductions if not heeded. Show transcribed image text We will change a few rules from those listed on the above site. 1. The game ends when the current player does not have a legal move (rather than ending the game only when BOTH players do not have a legal move). The winner is still the one with the most pieces matching their color. 2. We will support boards of EVEN-number sizes from 4×4 to 26×26 (and not just 8×8). The user will specify the dimension of the board at the command lines (e.g. ./test-reversi 4 should create and play a 4×4 version of the game). o Any provided dimension that is ODD or outside the range of 4 to 26 should cause Invalid size to be printed and the program should exit by returning 1 from maino o If no command line argument is provided, default to a 4×4 game board. Additional features and game play: 1. Without a GUI, we will require simple console I/O. Thus, we will output the current board state before accepting each players selection on their turn. The logic to output the board is already provided by Board::print (though we will want to support the operator

Expert Answer


Answer to REVERSI GAME HELP WITH STUDENT TO WRITE FUNCTIONS reversi.h #ifndef REVERSI_HPP #define REVERSI_HPP #include iostream #i…

OR