forked from Henry-the-junior/tictactoe_teaching_resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe_ep3_ans.py
43 lines (34 loc) · 1.14 KB
/
tictactoe_ep3_ans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
theBoard = [' '] * 10
def drawBoard(board):
# This function prints out the board that it was passed.
# "board" is a list of 10 strings representing the board (ignore index 0)
print(' | |')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
print(' | |')
drawBoard(theBoard)
#棋下去的時候,要注意什麼?輸入值要注意什麼?
def isSpaceFree(board, move):
# Return true if the passed move is free on the passed board.
return board[move] == ' '
def getXMove(board):
# Let the player type in his move.
move = ' '
while move not in '1 2 3 4 5 6 7 8 9'.split() or not isSpaceFree(board, int(move)):
print('What is your next move? (1-9)')
move = input()
move = int(move)
board[move] = 'X'
getXMove(theBoard)
drawBoard(theBoard)
getXMove(theBoard)
drawBoard(theBoard)
getXMove(theBoard)
drawBoard(theBoard)