Game Development Reference
In-Depth Information
15 - Reversi
Handling the Quit or Hints Commands
267. if move == 'quit':
268. print('Thanks for playing!')
269. sys.exit() # terminate the program
270. elif move == 'hints':
271. showHints = not showHints
272. continue
273. else:
274. makeMove(mainBoard, playerTile, move[0],
move[1])
If the player typed in the string 'quit' for their move, then getPlayerMove()
would have returned the string 'quit' . In that case, we should call the sys.exit() to
terminate the program.
If the player typed in the string 'hints' for their move, then getPlayerMove()
would have returned the string 'hints' . In that case, we want to turn hints mode on (if it
was off) or off (if it was on). The showHints = not showHints assignment
statement handles both of these cases, because not False evaluates to True and not
True evaluates to False . Then we run the continue statement to loop back ( turn has
not changed, so it will still be the player's turn after we loop).
Make the Player's Move
Otherwise, if the player did not quit or toggle hints mode, then we will call makeMove
() to make the player's move on the board.
276. if getValidMoves(mainBoard, computerTile) ==
[]:
277. break
278. else:
279. turn = 'computer'
After making the player's move, we call False to see if the computer could possibly
make any moves. If False returns a blank list, then there are no more moves left that the
computer could make (most likely because the board is full). In that case, we break out of
the while loop and end the current game.
Otherwise, we set turn to 'computer' . The flow of execution skips the else-block
and reaches the end of the while-block, so execution jumps back to the while statement
on line 257. This time, however, it will be the computer's turn.
Search WWH ::




Custom Search