Game Development Reference
In-Depth Information
15 - Reversi
move, we check if there exist any possible moves the human player can make. If
getValidMoves() returns an empty list, then there are no possible moves. That means
the game is over, and we should break out of the while loop that we are in.
Otherwise, there is at least one possible move the player should make, so we should set
turn to 'player' . There is no more code in the while-block after line 292, so execution
loops back to the while statement on line 257.
Drawing Everything on the Screen
294. # Display the final score.
295. drawBoard(mainBoard)
296. scores = getScoreOfBoard(mainBoard)
297. print('X scored %s points. O scored %s points.' %
(scores['X'], scores['O']))
298. if scores[playerTile] > scores[computerTile]:
299. print('You beat the computer by %s points!
Congratulations!' % (scores[playerTile] - scores
[computerTile]))
300. elif scores[playerTile] < scores[computerTile]:
301. print('You lost. The computer beat you by %s
points.' % (scores[computerTile] - scores[playerTile]))
302. else:
303. print('The game was a tie!')
Line 294 is the first line beyond the while-block that started on line 257. This code is
executed when we have broken out of that while loop, either on line 290 or 277. (The
while statement's condition on line 257 is simply the value True , so we can only exit the
loop through break statements.)
At this point, the game is over. We should print out the board and scores, and determine
who won the game. getScoreOfBoard() will return a dictionary with keys 'X' and
'O' and values of both players' scores. By checking if the player's score is greater than,
less than, or equal to the computer's score, we can know if the player won, if the player lost,
or if the player and computer tied.
Subtracting one score from the other is an easy way to see by how much one player won
over the other. Our print() calls on lines 299 and 301 use string interpolation to put the
result of this subtraction into the string that is printed.
Ask the Player to Play Again
305. if not playAgain():
306. break
Search WWH ::




Custom Search