首页 > > 详细

讲解留学生Python语言、Python设计辅导留学生、讲解留学生Python语言

Background
The modern board game, Othello, is a commercially produced version of a game that
originated in the 19th century called 'Reversi'. It's an interesting 2-player game of strategy
played on an 8x8 grid in which each player places tokens on the board in order to cover
more grid cells than their opponent. Each token is a two-sided disk with white on one side
and black on the other. The basic premise of the game is that as each player plays a token
of his/her color (white or black), they 'flip' over all the intermediate tokens of their
opponent. The game ends when all the grid cells have been covered with tokens, or no
allowable move can be made.
Read the following Wikipedia description of the game:
www.wikipedia.org/wiki/Reversi
The following website has an interactive version that you can play. You should try it to
make sure you are comfortable with how the game is played:
www.othelloonline.org
Program Requirements
Using Turtle graphics, write a well-structured Python program named othello.py
that will play an interactive game of Othello. Your program will pit a human player
against the computer.
Your program must do the following:
• Include the academic integrity pledge in the program source code (details below)
• Use turtle graphics to implement the graphical interface (set the turtle speed to 0 in
order to speed things up).
• The graphical display must include numeric row and column number "headers" to
assist the human player in determining what to enter.
• Maintain the board state using a 2-dimensional list (matrix) with 8 rows of 8
columns. Each board location must indicate if it has a black token, a white token or
is unoccupied. The upper-left cell is board location [0][0], and the lower right cell
is board location [7][7].
• The "human" player will play black and the computer will play white. The human
player will always go first. Your program must include a loop that will alternate
between soliciting the human player's move (row, column) and making the next
"computer" play. The game continues until the entire board is occupied or until
either player can no longer make a valid move. The game can also be terminated
prematurely by entering a null-string as input.
• A message should appear when the game ends indicating which player is the
winner, along with the final score.
• Avoid using global variables (global constants are perfectly fine). You will need to
pass the "board" matrix as an argument to the various functions that need it. 

• Solicit input using the turtle .textinput()function. The input should consist of
the row and column for the next (human) play.
• Include a pure function: selectNextPlay(board) that will be called to get
the computer's next play. You can be as creative as you wish, however it is
acceptable to simply make a random choice from any of the remaining valid
moves.
• Include a pure Boolean function: isValidMove(board,row,col,color)
that will return True if the specified row,column is a "valid" move for the given
color and False otherwise. Note that for a move to be valid, it must satisfy three
conditions:
1. It must be unoccupied
2. At least one of its 8 neighbors must be occupied by an opponent's token
(opposite of color)
3. At least one of the straight lines starting from row,col and continuing
horizontally, vertically or diagonally in any direction must start with an
opponent's token and end with a token matching the color argument.
• Include a pure function: getValidMoves(board,color) that will scan
every cell of the board and return a list of (row,column) tuples that are valid plays
for the indicated token color.
• You can find descriptions of the .textinput()and.write()turtle functions
(along with all the Turtle graphics module functions) here: docs.python.org/3/
library/turtle.html .

Constraints:
• You may use any built-in Python object class methods (string, list, etc.)
• You may use imported functions and class methods from the turtle,
random and math modules only
Suggestions:
• You will find this project much easier if you employ the principles of "top-down,
functional decomposition", as discussed in lecture, and implement your program as
a collection of short, simple functions.
• The graphical display is fairly uncomplicated if you set the world coordinates to
match the board (leaving some room for the border) and use the turtle.stamp()
method. You simply change the turtle shape to "square" and "stamp" it at each grid
cell location. Then change the turtle shape to "circle" and "stamp" each token when
it is played (in the appropriate color)
• You will find it useful to construct (and use!) a "helper" function that will convert
"row, column" board coordinates to "x, y" turtle coordinates!
• The most complicated aspect of this program is determining the lines of opponent
tokens to "flip" when a move is made (this is also needed to determine if a
proposed move is "valid"). One way to do this is to subtract the corresponding row
and column coordinates of the first two cells in a "line" (horizontal, vertical,
diagonal) to obtain a row and column "delta value". This delta value can be used in
an iteration loop to identify all the cells in that line.
• One of the challenges in this problem is dealing with the grid border. For example,
the cell at [0][0] does not have 8 neighbors, it has 3. You might find it useful to
construct a helper function that returns a list of neighbors, given a cell's row and
column. Another useful helper function would return a Boolean indicating if a
particular row, column pair is "inGrid". 

Example Program Output


 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!