Game Development Reference
In-Depth Information
Running the Computer's Turn
281. else:
282. # Computer's turn.
283. drawBoard(mainBoard)
284. showPoints(playerTile, computerTile)
285. input('Press Enter to see the computer\'s
move.')
286. x, y = getComputerMove(mainBoard,
computerTile)
287. makeMove(mainBoard, computerTile, x, y)
The first thing we do when it is the computer's turn is call drawBoard() to print out
the board to the player. Why do we do this now? Because either the computer was selected
to make the first move of the game, in which case we should display the original starting
picture of the board to the player before the computer makes its move. Or the player has
gone first, and we want to show what the board looks like after the player has moved but
before the computer has gone.
After printing out the board with drawBoard() , we also want to print out the current
score with a call to showPoints() .
Next we have a call to input() to pause the script while the player can look at the
board. This is much like how we use input() to pause the program in our Jokes chapter.
Instead of using a print() call to print a string before a call to input() , you can pass
the string as a parameter to input() . input() has an optional string parameter. The
string we pass in this call is 'Press Enter to see the computer\'s move.' .
After the player has looked at the board and pressed Enter (any text the player typed is
ignored since we do not assign the return value of input() to anything), we call
getComputerMove() to get the X and Y coordinates of the computer's next move. We
store these coordinates in variables x and y , respectively.
Finally, we pass x and y , along with the game board data structure and the computer's
tile to the makeMove() function to change the game board to reflect the computer's move.
Our call to getComputerMove() got the computer's move, and the call to makeMove
() makes the move on the board.
289. if getValidMoves(mainBoard, playerTile) ==
[]:
290. break
291. else:
292. turn = 'player'
Lines 289 to 292 are very similar to lines 276 to 279. After the computer has made its
Search WWH ::




Custom Search