Game Development Reference
In-Depth Information
15 - Reversi
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)
Printing each row of spaces on the board is fairly repetitive, so we can use a loop here. We
will loop eight times, once for each row. Line 15 prints the label for the Y-axis on the left
side of the board, and has a comma at the end of it to prevent a new line. This is so we can
have another loop (which again loops eight times, once for each space) print out each space
(along with the 'X' , 'O' , or ' ' character for that space depending on what is stored in
board.
The print() function call inside the inner loop also has a comma at the end of it,
meaning a space character is printed instead of a newline character. This produces the
second space in the pipe-space-tile-space string that we print out, over and over for eight
times. That will produce a single line on the screen that looks like '| X | X | X | X
| X | X | X | X ' (that is, if each of the board[x][y] values were 'X' ). After the
inner loop is done, the print() function call on line 18 prints out the final '|' character
along with a newline (since it does not end with a comma).
(The print() call forces us to always print a newline character or a space at the end of
everything we print. If we do not want this last character, then we can always use the
sys.stdout.write() function, which has a single string parameter that it prints out. Be
sure to import sys first before calling this function.)
The code inside the outer for loop that begins on line 13 prints out an entire row of the
board like this:
| | | | | | | | |
| X | X | X | X | X | X | X | X |
| | | | | | | | |
+---+---+---+---+---+---+---+---+
When printed out eight times, it forms the entire board (of course, some of the spaces on
the board will have 'O' or ' ' instead of 'X' .:
| | | | | | | | |
| X | X | X | X | X | X | X | X |
| | | | | | | | |
+---+---+---+---+---+---+---+---+
| | | | | | | | |
| X | X | X | X | X | X | X | X |
| | | | | | | | |
+---+---+---+---+---+---+---+---+
Search WWH ::




Custom Search