forked from Henry-the-junior/tictactoe_teaching_resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe_ep5_ans.py
99 lines (89 loc) · 3.3 KB
/
tictactoe_ep5_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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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'
def whoGoesFirst():
who = ''
while not (who == 'O' or who == 'X'):
who = input('Who goes first?')
return who
def isWinner(board, turn):
# Given a board and a player's letter, this function returns True if that player has won.
return ((board[7] == turn and board[8] == turn and board[9] == turn) or # across the top
(board[4] == turn and board[5] == turn and board[6] == turn) or # across the middle
(board[1] == turn and board[2] == turn and board[3] == turn) or # across the bottom
(board[7] == turn and board[4] == turn and board[1] == turn) or # down the left side
(board[8] == turn and board[5] == turn and board[2] == turn) or # down the middle
(board[9] == turn and board[6] == turn and board[3] == turn) or # down the right side
(board[7] == turn and board[5] == turn and board[3] == turn) or # diagonal
(board[9] == turn and board[5] == turn and board[1] == turn)) # diagonal
def isBoardFull(board):
# Return True if every space on the board has been taken. Otherwise return False.
for i in range(1, 10):
if isSpaceFree(board, i):
return False
return True
print('Welcome to Tic Tac Toe!')
# Reset the board
theBoard = [' '] * 10
turn = whoGoesFirst()
print('The ' + turn + ' will go first.')
gameIsPlaying = True
while gameIsPlaying:
if turn == 'X':
# X's turn.
drawBoard(theBoard)
getXMove(theBoard)
if isWinner(theBoard, turn):
drawBoard(theBoard)
print('Hooray!' + turn + ' have won the game!')
gameIsPlaying = False
else:
if isBoardFull(theBoard):
drawBoard(theBoard)
print('The game is a tie!')
break
else:
turn = 'O'
else:
# O's turn.
drawBoard(theBoard)
getOMove(theBoard)
if isWinner(theBoard, turn):
drawBoard(theBoard)
print('Hooray!' + turn + ' have won the game!')
gameIsPlaying = False
else:
if isBoardFull(theBoard):
drawBoard(theBoard)
print('The game is a tie!')
break
else:
turn = 'X'