Game Development Reference
In-Depth Information
see a parameter named board, that parameter variable is meant to be this list of lists
board
data structure.
Importing Other Modules
1. # Reversi
2.
3. import random
4. import sys
We import the
random
module for its
randint()
and
choice()
functions and the
sys
module for its
exit()
function.
Drawing the Board Data Structure on the Screen
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)
The
drawBoard()
function will print out the current game
board
based on the data
structure in board. Notice that each square of the board looks like this:
+---+
| |
| X |
| |
+---+
Since we are going to print the string with the horizontal line (and plus signs at the
intersections) over and over again, we will store that in a constant variable named
HLINE
.
There are also lines above and below the very center of X or O tile that are nothing but '|'
characters (called "pipe" characters) with three spaces in between. We will store this string
in a constant named
VLINE
.
Line 11 is the first
print()
function call executed, and it prints out the labels for the
X-axis along the top of the board. Line 12 prints the top horizontal line of the board.
13. for y in range(8):