Game Development Reference
In-Depth Information
15 - Reversi
We do not always want to go with the first move in the possibleMoves list, because
that would make our AI predictable by the player. But it is random, because on line 220 we
shuffled the possibleMoves list. Even though our code always chooses the first of these
tied moves, is random which of the moves will be first in the list because the order is
random. This ensures that the AI will not be predictable when there is more than one best
move.
Printing the Scores to the Screen
239. def showPoints(playerTile, computerTile):
240. # Prints out the current score.
241. scores = getScoreOfBoard(mainBoard)
242. print('You have %s points. The computer has %s
points.' % (scores[playerTile], scores[computerTile]))
showPoints() simply calls the getScoreOfBoard() function and then prints out
the player's score and the computer's score. Remember that getScoreOfBoard()
returns a dictionary with the keys 'X' and 'O' and values of the scores for the X and O
players.
That's all the functions we define for our Reversi game. The code starting on line 246
will implement the actual game and make calls to these functions when they are needed.
The Start of the Game
246. print('Welcome to Reversi!')
247.
248. while True:
249. # Reset the board and game.
250. mainBoard = getNewBoard()
251. resetBoard(mainBoard)
252. playerTile, computerTile = enterPlayerTile()
253. showHints = False
254. turn = whoGoesFirst()
255. print('The ' + turn + ' will go first.')
The while loop on line 248 is the main game loop. The program will loop back to line
248 each time we want to start a new game. First we get a new game board data structure
by calling getNewBoard() and set the starting tiles by calling resetBoard() .
mainBoard is the main game board data structure we will use for this program. The call
to enterPlayerTile() will let the player type in whether they want to be 'X' or 'O' ,
which is then stored in playerTile and computerTile .
showHints is a Boolean value that determines if hints mode is on or off. We originally
set it to off by setting showHints to False .
Search WWH ::




Custom Search