Starting from:
$29.99

$23.99

Homework # 1 Solution

You will write a C++ program that will play the classical game reversi. The game of reversi is played by two players (computer and user) on a two dimentional board (2D array) with rectangular cells. Each cell is either computer, user, or  empty. The game starts with the following board for an 8x8 game

 

abcdefgh

1........

2........

3........

4...xo...

5...ox...

6........

7........

8........

 

 

where . represents the empty cells, X repesents the computer cells and O represents the user cells. The players take turns the play the game. For the above board, the user can play positions at 4c, 3d, 5f, or

6e. For example, if the user plays to postions 4c then the board becomes

 

abcdefgh

1........

2........

3........

4..ooo...

5...ox...

6........

7........

8........

 

The computer cell at 4d becomes a user cell because it is between two user cells. The computer now can play positions at 3c, 3e, or 5c. The game continues this way until all cells are filled and the player with the most cells wins the game. For the detailed rules of the game see https://en.wikipedia.org/wiki/Reversi. There are many online reversi games, chose one of them and play against the computer to learn the game rules and develop some game strategies. https://www.mathsisfun.com/games/reversi.html is a good example.

 

 

Your program will do the following

1.   When your program starts, you will ask the user the board size, the board size can be 4x4,

6x6, 8x8, 10x10, …, 20x20. You should check the validity of the input.

2.   You will display the initial board where at the center there are two diagonal X and O cells as shown above.

3.   You ask the user to make a move, get the user move from the keyboard and draw the new board. Use positions such as 4C, 5B etc. If the move is not legal, then ask for another move.

4.   The computer makes a new move and draws the board again. You should print what move the computer chose on the screen. The move should be legal and try to make it “smart”. In other words, chose the legal move that wins the largest number of cells instead of other moves.

5.   The game continues until all the board is filled or players have no legal moves to make.

6.   The final result of the game should be printed on the screen showing who won and the number of cells for each player.

 

Notes:

        Do not use any functions from the standard C library (like printf)

        Your program should have only functions and no classes.

        Do not forget to indent your code and provide comments.

        Check the validity of the user input.

    Test your programs very carefully at least with 10 different runs. For some runs, let the computer win and for others the user should win. There should be cases for illegal moves and your program should handle them appropriately.

        You should submit your work to the moodle page.

More products