Question
Code in python 3 Problem : The Reversi Game Reversi is a 2-player game, played on an 8 x 8 board. Players take turns placing
Code in python 3
Problem : The Reversi Game
Reversi is a 2-player game, played on an 8 x 8 board. Players take turns placing their disks on the board with their assigned colour (Black and White). Black is the first player to move. A player may place their disk anywhere on the board, as long as it surrounds a group of the opponents disks (vertically, horizontally, or diagonally) on opposite sides. Any disks that you surround will become yours and will flip over to your colour. The game is over when the player to move has no possible legal move.
Example:
The board must always start off as such
Example: The board must always start off as such 0 1 2 3 4 5 6 7 0 . . . . . . . . 1 . . . . . . . . 2 . . . . . . . . 3 . . . w b . . . 4 . . . b w . . . 5 . . . . . . . . 6 . . . . . . . . 7 . . . . . . . .
BLACK is the first to move. BLACK must place a piece such that at least one straight (vertical, horizontal, or diagonal) line is made with another BLACK piece, with at least one White piece between and no empty spaces allowed. The available moves BLACK can make are denoted with '*':
0 1 2 3 4 5 6 7 0 . . . . . . . . 1 . . . . . . . . 2 . . . * . . . . 3 . . * w b . . . 4 . . . b w * . . 5 . . . . * . . . 6 . . . . . . . . 7 . . . . . . . .
Suppose BLACK makes the move (2,3). Then the board is updated with BLACK capturing the white pieces between the line made
0 1 2 3 4 5 6 7 0 . . . . . . . . 1 . . . . . . . . 2 . . . b . . . . 3 . . . b b . . . 4 . . . b w . . . 5 . . . . . . . . 6 . . . . . . . . 7 . . . . . . . .
Now it is WHITE turn, and they are allowed to make the following moves
0 1 2 3 4 5 6 7 0 . . . . . . . . 1 . . . . . . . . 2 . . * b * . . . 3 . . . b b . . . 4 . . * b w . . . 5 . . . . . . . . 6 . . . . . . . . 7 . . . . . . . .
If WHITE makes the move (4,2), then the board will look like
0 1 2 3 4 5 6 7 0 . . . . . . . . 1 . . . . . . . . 2 . . . b . . . . 3 . . . b b . . . 4 . . w w w . . . 5 . . . . . . . . 6 . . . . . . . . 7 . . . . . . . .
The score for each player is the number of pieces that they own on the board. I suggest that you play a few games to get a better sense of the rules: https://www.coolmathgames.com/0-reversi
Tasks to do
Task 1
Your task will be to design a Reversi class, and use the class to play games. The class will store the state of the game, and have methods to allow the players to interact with the game. We require the following methods to be implemented:
- newGame(): Create the game state so players can play again
- getScore(colour): return the current score for the player with colour 'colour'
- setPlayerColour(colour): set the colour for the human player to the designated colour 'colour', as well as the computer will have the other colour
- displayBoard(): print a visual representation of the board at the current state. The row and column index should be printed on the left and top side to make it easier to check moves. See the above output as an example.
- isPositionValid(position, colour): Check if the input position 'position' is valid for the given player 'colour' to make. A position is defined as valid using the rules above.
- isGameOver(): return true if the game is over, false otherwise. The game is over when the current player cannot make any more legal moves.
- makeMovePlayer(position): Make the move given by 'position' for the human player. This function should also handle the capturing of pieces
- makeMoveNaive(): This function should make a naive (no strategy involved) move for the computer. This can be the first valid move when scanning the board left to right, starting at the top
- makeMoveSmart(): This function should make a smart (strategy involved) move for the computer. This is where you can try and make your game computer as smart as possible! A good starting place can be to make the move which maximizes the computer's score.
Do not forget to test your class in isolation before you write the code of the game itself. Test your class.
The number of required methods may seem like a lot, but many of these methods are quite small, and are designed so that interacting with the class is easier. A skeleton template class has been provided with comments as to what is expected for each method, as well as a text file of sample output and input.
Task 2
In addition, you will be required to create a main() function in a file main.py that will create a Reversi object, and play games. You should get and process the users input, determine if the move is valid, make the move, and display the updated board and score. Don't forget to validate any input from the user.
Also, the game should ask whether you as the user would like to play as white or black. At the end of the game, the user should be asked whether they would like to play again.
Optional Tasks to do (not marked)
Optional (1): Allow the user to input the size of the gameboard to allow different versions of the game to be played. The input should be an even integer 'n' between [4,20] inclusive, where the gameboard will be of size (n,n). The starting board position should keep the 2 starting white and black pieces in the center.
Optional (2): Allow the user to enter an 'h' option (for hint), which will display the gameboard but will highlight the valid moves the player can make denoted by '*'. As an example, the legal moves shown for black are as follows:
Enter move: h 0 1 2 3 4 5 6 7 0 . . . . . . . . 1 . . . . . . . . 2 . . . * . . . . 3 . . * w b . . . 4 . . . b w * . . 5 . . . . * . . . 6 . . . . . . . . 7 . . . . . . . .
Optional (3): Allow the user to enter an 'a' option (for assist), which will display the gameboard but will highlight the valid moves with '*' and the moves with highest potential score the player can make in one round denoted by '#'. As an example, the moves and the highest legal moves shown for black are as follows:
Enter move: a 0 1 2 3 4 5 6 7 0 . . . w . . . . 1 . . # w . . . . 2 . . w w . . . . 3 . . b w b * . . 4 . . . b b b . . 5 . . . . b w * . 6 . . . . . * . . 7 . . . . . . . .
Optional (4): Allow the user to enter a 'u' option to undo moves. If its currently the users turn, by pressing 'u', it should undo the computers move, as well as your previous move, with your turn to move. If only 1 move is undone, then control will be back to the computer and it will simply make its move again. As an added optional, you can ask the user how many moves they wish to undo.
please use the following template
Hints: - self.gameBoard represents the game board - self.playerColour represents the colour of the human player - self.computerColour represents the colour of the computer - the colour arguments are eiter self.WHITE or self.BLACK, which are defined below """ class Reversi: WHITE = "w" BLACK = "b" EMPTY = "." SIZE = 8 def __init__(self): self.newGame() """ Functionality: Create the game state so players can play again Parameters: None """ def newGame(self): self.gameBoard = ... return """ Functionality: Return the score of the player Parameters: colour: The colour of the player to get the score for Use BLACK or 'b' for black, WHITE or 'w' for white """ def getScore(self, colour): return """ Functionality: Set the colour for the human player to the designated colour, as well as the computer will haev the other colour Parameters: colour: The colour of the player the user wants to play as Use BLACK or 'b' for black, WHITE or 'w' for white """ def setPlayerColour(self, colour): self.playerColour = ... self.computerColour = ... """ Functionality: Print out the current board state The index of the rows and columns should be on the left and top. See the sample output for details Parameters: None """ def displayBoard(self): return """ Functionality: Return true if the input position 'position' is valid for the given player 'colour' to make Parameters: position -> A list [i,j] where i is the row and j is the column colour: The colour that is making the move Use BLACK or 'b' for black, WHITE or 'w' for white """ def isPositionValid(self, position, colour): return """ Functionality: Return true if the game is over, false otherwise The game is over when the current player cannot make any more legal moves. Parameters: None Note: Skipping is not allowed """ def isGameOver(self): return """ Functionality: Make the given move for the human player, and capture any pieces If you assume the move is valid, make sure the validity is checked before calling Parameters: position -> A list [i,j] where i is the row and j is the column """ def makeMovePlayer(self, position): return """ Functionality: Make a naive move for the computer This is the first valid move when scanning the board left to right, starting at the top Parameters: None """ def makeMoveNaive(self): return """ Functionality: Make a move for the computer which is the best move available This should be the move that results in the best score for the computer Parameters: None """ def makeMoveSmart(self): return
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started