Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a game of Othello on C++ where two human players compete in a text-based version of the game. Your game will keep track of

Create a game of Othello on C++ where two human players compete in a text-based version of the game. Your game will keep track of the current player, allow that player to select a move, then apply that move to the game board and output the resulting board. The game ends when both players have to "pass" their turn in succession, signaling that neither can select a valid move and thus the game is over.

Game Rules:(Reversi rules) 1. The game is an 8x8 grid of squares. Each square can have a single pieve that is either black of white on that square, or it can be empty. The rows of the grid are numbered 0 though 7, as are the columns. Thus the upper left corner is position (0,0, and the upper right is position (0,7)

2. At the start of the game, the center four squares are already taken by two black and two white pieces.

3. On his/her turn, a player must input a square coordinate for their move, in the format (r,c). Alternatively, the user may choose to pass by entering a move of (-1,-1).

4. If the board is full or the player cannot make a valid move, they must pass.

5. The game ends when both players pass in succession.

6. Black is the first player to move.

7. When the game is over, you will output the winner by counting the value of the board. For each black piece, the value goes up by 1; for each white piece, it goes down by 1. Thus a value of 0 indicates a tie, a positive value indicates a black win, and a negative indicates a white win. Output the winner, even if the board is not completely filled.

Game will consist of 3 files: main.cpp, OthelloGame.cpp, and OthelloGame.h.

Functions: PrintBoard: this function takes the game board array as a parameter and prints it to the screen. First, print a header row of the numbers 0 through 7 witth spaces inbetween, showing the indices of each column. Then print one row of the board at a time: start with the row index, then print a period if the space is empty; a B if the black player has a piece there; a W for a white piece. Example for the initial board setup: - 0 1 2 3 4 5 6 7 0 . . . . . . . . 1 . . . . . . . . 2 . . . . . . . . 3 . . . W B . . . 5 . . . B W . . . 6 . . . . . . . . 7 . . . . . . . . GetMove: this function uses cin to read a line of input from the user representing their move, in the format (row, col); then parses the line and returns a row and column from the function. Since the function needs to return two pieces of information, it will need to take reference parameters from the main, then use those to store the information read from cin. This function should not output anything; the main is responsible for asking the user to type in a move.

InBounds: This function takes a row and column, and returns true if the position specified is "in bounds" of the board: that is, if both are at least 0 and less than the size of the board.

IsValidMove: this function takes the game board, and integer values for a row and column that the player would like to move to. This function ONLY CHECKS that the destination is in-bounds(use InBound) and does not have a piece already at the location; OR alternatively, if the move is a pass (both row and column are -1). This function does not check to make sure the player will actually surround enemy pieces with the move.

ApplyMove: This is the hardest function and contains the bulk of the game logic. Given a game board array, a row, a column, and the current player, this function applies the move requested by the current player by turning the requested square to the player's color, and then searches all eight directions from the square looking to see if there is a run of the opponent's pieces to flip according to the game rules. Because you can't count on the user moving to a specific game location, you must use loops to iterate through all 8 directions and continue moving in each direction looking for a run of enemy pieces. You can assume that the given move is a valid selection for the current player. You do not need to validate moves. GetValue: Given the board, this function returns an integer for the value of the board as defined above. Your main may only call this function once.

OthelloGame.h:

const int BOARD_SIZE = 8;

// A typedef creates an alias for a type (another name we can use). // std::array, BOARD_SIZE> is a lot to type, so // we make an alias "OthelloMatrix" and can use that instead. typedef std::array, BOARD_SIZE> OthelloMatrix;

// Print the board to the screen. void PrintBoard(const OthelloMatrix &board);

// Returns true if row and col are in the bounds of the board. bool InBounds(int row, int col);

// Returns true if the row and column are in bounds of the board and the square // is empty, or if the row and column represent a "pass" move. Does not // make sure the move actually causes pieces to flip. bool IsValidMove(const OthelloMatrix &board, int row, int col);

// Uses cin to read a row and column from the user into the two pointers. void GetMove(int &row, int &col);

// Takes a board, a row and column, and a player, and applies that player's move // to that position, flipping pieces according to the game rules. void ApplyMove(OthelloMatrix &board, int row, int col, char currentPlayer);

// Returns the sum of the black pieces minus the sum of the white pieces. int GetValue(const OthelloMatrix &board);

Yes this is a lot of work. Any help would be appreciated. Really wish to get help with ApplyMove function.

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

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions

Question

4. Describe the factors that influence self-disclosure

Answered: 1 week ago

Question

1. Explain key aspects of interpersonal relationships

Answered: 1 week ago