Game Development Reference
In-Depth Information
15 - Reversi
85. board[xstart][ystart] = ' ' # restore the empty space
86. if len(tilesToFlip) == 0: # If no tiles were flipped,
this is not a valid move.
87. return False
88. return tilesToFlip
We perform this check in all eight directions, and afterwards the tilesToFlip list
will contain the XY coordinates all of our opponent's tiles that would be flipped if the
player moved on xstart , ystart . Remember, the isValidMove() function is only
checking to see if the original move was valid, it does not actually change the data structure
of the game board.
If none of the eight directions ended up flipping at least one of the opponent's tiles, then
tilesToFlip would be an empty list and this move would not be valid. In that case,
isValidMove() should return False . Otherwise, we should return tilesToFlip .
Checking for Valid Coordinates
91. def isOnBoard(x, y):
92. # Returns True if the coordinates are located on the
board.
93. return x >= 0 and x <= 7 and y >= 0 and y <=7
isOnBoard() is a function called from isValidMove() , and is just shorthand for
the rather complicated Boolean expression that returns True if both x and y are in
between 0 and 7 . This function lets us make sure that the coordinates are actually on the
game board.
Getting a List with All Valid Moves
96. def getBoardWithValidMoves(board, tile):
97. # Returns a new board with . marking the valid moves
the given player can make.
98. dupeBoard = getBoardCopy(board)
99.
100. for x, y in getValidMoves(dupeBoard, tile):
101. dupeBoard[x][y] = '.'
102. return dupeBoard
getBoardWithValidMoves() is used to return a game board data structure that has
'.' characters for all valid moves on the board. This is used by the hints mode to display
to the player a board with all possible moves marked on it.
Notice that this function creates a duplicate game board data structure instead of
Search WWH ::




Custom Search