Game Development Reference
In-Depth Information
Printing the Board on the Screen
5. def drawBoard(board):
6. # This function prints out the board that it was
passed.
7.
8. # "board" is a list of 10 strings representing the
board (ignore index 0)
9. print(' | |')
10. print(' ' + board[7] + ' | ' + board[8] + ' | ' +
board[9])
11. print(' | |')
12. print('-----------')
13. print(' | |')
14. print(' ' + board[4] + ' | ' + board[5] + ' | ' +
board[6])
15. print(' | |')
16. print('-----------')
17. print(' | |')
18. print(' ' + board[1] + ' | ' + board[2] + ' | ' +
board[3])
19. print(' | |')
This function will print out the game board, marked as directed by the board parameter.
Remember that our board is represented as a list of ten strings, where the string at index 1
is the mark on space 1 on the Tic Tac Toe board. (And remember that we ignore the string
at index 0 , because the spaces are labeled with numbers 1 to 9.) Many of our functions will
work by passing the board as a list of ten strings to our functions. Be sure to get the spacing
right in the strings that are printed, otherwise the board will look funny when it is printed
on the screen.
Just as an example, here are some values that the board parameter could have (on the
left) and what the drawBoard() function would print out:
Table 10-1: Examples of values of board and output from
drawBoard(board) calls.
board data structure
drawBoard(board) output
| |
X | | O
| |
-----------
| |
X | O |
| |
-----------
| |
| |
[' ', ' ', ' ', ' ', 'X',
'O', ' ', 'X', ' ', 'O']
Search WWH ::




Custom Search