Game Development Reference
In-Depth Information
15 - Reversi
before reading this chapter. And you can also download the program by going to this
topic's website at the URL, http://inventwithpython.com/chapter15 and following the
instructions online.
As with our other programs, we will first create several functions to carry out Reversi-
related tasks that the main section of our program will call. Roughly the first 250 lines of
code are for these helper functions, and the last 50 lines of code implement the Reversi
game itself.
reversi.py
This code can be downloaded from http://inventwithpython.com/reversi.py
If you get errors after typing this code in, compare it to the topic's code with the online
diff tool at http://inventwithpython.com/diff or email the author at
al@inventwithpython.com
1. # Reversi
2.
3. import random
4. import sys
5.
6. def drawBoard(board):
7. # This function prints out the board that it was
passed. Returns None.
8. HLINE = ' +---+---+---+---+---+---+---+---+'
9. VLINE = ' | | | | | | | | |'
10.
11. print(' 1 2 3 4 5 6 7 8')
12. print(HLINE)
13. for y in range(8):
14. print(VLINE)
15. print(y+1, end=' ')
16. for x in range(8):
17. print('| %s' % (board[x][y]), end=' ')
18. print('|')
19. print(VLINE)
20. print(HLINE)
21.
22.
23. def resetBoard(board):
24. # Blanks out the board it is passed, except for the
original starting position.
25. for x in range(8):
26. for y in range(8):
27. board[x][y] = ' '
28.
29. # Starting pieces:
30. board[3][3] = 'X'
31. board[3][4] = 'O'
32. board[4][3] = 'O'
33. board[4][4] = 'X'
34.
35.
36. def getNewBoard():
Search WWH ::




Custom Search