Game Development Reference
In-Depth Information
15 - Reversi
move[1])
275.
276. if getValidMoves(mainBoard, computerTile) ==
[]:
277. break
278. else:
279. turn = 'computer'
280.
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)
288.
289. if getValidMoves(mainBoard, playerTile) == []:
290. break
291. else:
292. turn = 'player'
293.
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!')
304.
305. if not playAgain():
306. break
How the Code Works
The Game Board Data Structure
Before we get into the code, we should talk about the board data structure. This data
structure is a list of lists, just like the one in our previous Sonar game. The list is created so
that board[x][y] will represent the character on space located at position x on the X-axis
(going left/right) and position y on the Y-axis (going up/down). This character can either be
a ' ' space character (to represent a blank space), a '.' period character (to represent a
possible move in hint mode), or an 'X' or 'O' (to represent a player's tile). Whenever you
Search WWH ::




Custom Search