-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtictactoe_ep4-1.py
60 lines (53 loc) · 1.59 KB
/
tictactoe_ep4-1.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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(' | |')
def isSpaceFree(board, move):
# Return true if the passed move is free on the passed board.
return board[move] == ' '
def getXMove(board):
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'
def getOMove(board):
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] = 'O'
theBoard = [' '] * 10
getXMove(theBoard)
drawBoard(theBoard)
getOMove(theBoard)
drawBoard(theBoard)
getXMove(theBoard)
drawBoard(theBoard)
getOMove(theBoard)
drawBoard(theBoard)
getXMove(theBoard)
drawBoard(theBoard)
getOMove(theBoard)
drawBoard(theBoard)
getXMove(theBoard)
drawBoard(theBoard)
getOMove(theBoard)
drawBoard(theBoard)
getXMove(theBoard)
drawBoard(theBoard)
#這樣寫很醜...
#而且思考看看,這樣寫還有什麼問題
#我這次想嘗試加入,決定由O還是X開始