Game Development Reference
In-Depth Information
O | | X
| |
The computer has beaten you! You lose.
Do you want to play again? (yes or no)
no
Source Code of Tic Tac Toe
In a new file editor window, type in this source code and save it as tictactoe.py . Then run
the game by pressing F5. You do not need to type in this program before reading this
chapter. You can also download the source code by visiting the website at the URL
http://inventwithpython.com/chapter10 and following the instructions on the webpage.
tictactoe.py
This code can be downloaded from http://inventwithpython.com/tictactoe.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. # Tic Tac Toe
2.
3. import random
4.
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(' | |')
20.
21. def inputPlayerLetter():
22. # Let's the player type which letter they want to be.
23. # Returns a list with the player's letter as the
first item, and the computer's letter as the second.
24. letter = ''
25. while not (letter == 'X' or letter == 'O'):
26. print('Do you want to be X or O?')
Search WWH ::




Custom Search