Game Development Reference
In-Depth Information
numbers beginning at 1 instead of 0. So when we convert the strings in
move[0]
and
move[1]
to integers, we also subtract
1
.
Even if the player typed in a correct move, we still need to check that the move is
allowed by the rules of Reversi. We do this by calling
isValidMove()
, passing the
game board data structure, the player's tile, and the XY coordinates of the move. If
isValidMove()
returns
False
, then we execute the
continue
statement so that the
flow of execution goes back to the beginning of the
while
loop and asks the player for the
move again.
If
isValidMove()
does not return
False
, then we know the player typed in a valid
move and we should break out of the
while
loop.
207. else:
208. print('That is not a valid move. Type the x
digit (1-8), then the y digit (1-8).')
209. print('For example, 81 will be the top-right
corner.')
If the
if
statement's condition on line 200 was
False
, then the player did not type in a
valid move. We should display a message instructing them how to type in moves that our
Reversi program can understand. Afterwards, the execution moves back to the
while
statement on line 192 because line 209 is not only the last line in the else-block, but also the
last line in the while-block.
211. return [x, y]
Finally,
getPlayerMove()
returns a two-item list with the XY coordinates of the
player's valid move.
Getting the Computer's Move
214. def getComputerMove(board, computerTile):
215. # Given a board and the computer's tile, determine
where to
216. # move and return that move as a [x, y] list.
217. possibleMoves = getValidMoves(board, computerTile)
getComputerMove()
and is where our Reversi AI is implemented. The
getValidMoves()
function is very helpful for our AI. Normally we use the results from
getValidMoves()
for hints move. Hints mode will print
'.'
period characters on the
board to show the player all the potential moves they can make. But if we call
getValidMoves()
with the computer AI's tile (in computerTile), we can get all the