Game Development Reference
In-Depth Information
The turn variable is a string will either have the string value 'player' or
'computer' , and will keep track of whose turn it is. We set turn to the return value of
whoGoesFirst() , which randomly chooses who will go first. We then print out who
goes first to the player on line 255.
Running the Player's Turn
257. while True:
258. if turn == 'player':
259. # Player's turn.
260. if showHints:
261. validMovesBoard = getBoardWithValidMoves
(mainBoard, playerTile)
262. drawBoard(validMovesBoard)
263. else:
264. drawBoard(mainBoard)
265. showPoints(playerTile, computerTile)
The while loop that starts on line 257 will keep looping each time the player or
computer takes a turn. We will break out of this loop when the current game is over.
Line 258 has an if statement whose body has the code that runs if it is the player's turn.
(The else-block that starts on line 282 has the code for the computer's turn.) The first thing
we want to do is display the board to the player. If hints mode is on (which it is if
showHints is True ), then we want to get a board data structure that has '.' period
characters on every space the player could go.
Our getBoardWithValidMoves() function does that, all we have to do is pass the
game board data structure and it will return a copy that also contains '.' period characters.
We then pass this board to the drawBoard() function.
If hints mode is off, then we just pass mainBoard to drawBoard() .
After printing out the game board to the player, we also want to print out the current
score by calling showPoints() .
266. move = getPlayerMove(mainBoard, playerTile)
Next we let the player type in their move. getPlayerMove() handles this, and its
return value is a two-item list of the X and Y coordinate of the player's move.
getPlayerMove() makes sure that the move the player typed in is a valid move, so we
don't have to worry about it here.
Search WWH ::




Custom Search