-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
192 lines (173 loc) · 4.94 KB
/
utils.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import random
def main():
players = ['x', 'o']
board = {1: ' ',
2: ' ',
3: ' ',
4: ' ',
5: ' ',
6: ' ',
7: ' ',
8: ' ',
9: ' '}
print('Welcome to 2 player Tic Tac Toe! \nOne player is x, and one player is o.\n')
if choosePlay():
bot = players[0]
human = players[1]
print(f"The computer is playing as {bot}.")
for i in board:
if i % 2 == 0:
humanMove(human, board)
cur = human
else:
computerMove(board, bot, human)
cur = bot
check = isWinner(board)
if check:
print(f'Player {cur.upper()} wins!')
break
else:
check = isDraw(board)
if check:
print("It's a draw!")
break
else:
turns = random.randint(0,1)
print(f'{players[turns]} will go first.')
printBoard(board)
for i in board:
current = players[turns%2]
humanMove(current, board)
check = isWinner(board)
if check:
print(f'Player {current.upper()} wins!')
break
else:
check = isDraw(board)
if check:
print("It's a draw!")
break
turns += 1
def choosePlay():
"""takes yes or no user input, returns true if bot play, false if 2 player"""
choice = input("If you would like to play against the computer, type \"yes\". Otherwise, type \"no\".\n").lower()
while choice != "yes" and choice != "no":
choice = input("That was not a yes or no. Try again!")
if choice == "yes":
return True
else:
return False
def printBoard(board):
horiz = '-------------'
print(horiz)
print(f'| {board[7]} | {board[8]} | {board[9]} |')
print(horiz)
print(f'| {board[4]} | {board[5]} | {board[6]} |')
print(horiz)
print(f'| {board[1]} | {board[2]} | {board[3]} |')
print(horiz)
def isWinner(b):
"""b: dict board
returns true if current is winner, false if not, to continue."""
if b[1] == b[2] == b[3] and b[1] != ' ':
return True
elif b[4] == b[5] == b[6] and b[4] != ' ':
return True
elif b[7] == b[8] == b[9] and b[7] != ' ':
return True
elif b[1] == b[4] == b[7] and b[1] != ' ':
return True
elif b[2] == b[5] == b[8] and b[2] != ' ':
return True
elif b[3] == b[6] == b[9] and b[3] != ' ':
return True
elif b[1] == b[5] == b[9] and b[1] != ' ':
return True
elif b[3] == b[5] == b[7] and b[3] != ' ':
return True
else:
return False
def whichWon(b, mark):
if b[1] == b[2] == b[3] and b[1] == mark:
return True
elif b[4] == b[5] == b[6] and b[4] == mark:
return True
elif b[7] == b[8] == b[9] and b[7] == mark:
return True
elif b[1] == b[4] == b[7] and b[1] == mark:
return True
elif b[2] == b[5] == b[8] and b[2] == mark:
return True
elif b[3] == b[6] == b[9] and b[3] == mark:
return True
elif b[1] == b[5] == b[9] and b[1] == mark:
return True
elif b[3] == b[5] == b[7] and b[3] == mark:
return True
else:
return False
def isDraw(b):
"""b: dict board"""
for i in b:
if b[i] == ' ':
return False
return True
def humanMove(current, board):
"""current: string letter x or o"""
pick = int(input(f"Player {current.upper()}, choose your position from numbers 1-9.\n"))
if pick not in range(1, 10):
while pick not in range(1, 10):
pick = int(input("That was not a number 1-9. Try again!"))
elif board[pick] != ' ':
while board[pick] != ' ':
pick = int(input("This spot is already filled, choose a different spot!"))
board[pick] = current
printBoard(board)
def computerMove(board, bot, human):
idealscore = -1000
idealmove = 0
for key in board:
if board[key] == ' ':
board[key] = bot
score = minimax(board, False, bot, human)
board[key] = ' '
if score > idealscore:
idealscore = score
idealmove = key
board[idealmove] = bot
print("Computer move:")
printBoard(board)
def minimax(board, isMaximizing, bot, human):
"""board: dict board
isMaximizing: boolean"""
# terminal states - reports that either the bot won (good+++) or the human won (bad---)
if whichWon(board, bot):
return 100
elif whichWon(board, human):
return -100
elif isDraw(board):
return 0
# for each possible position, find the best possible score again
# until the terminal state is reached (winning) - take turns by alternating true and false
# computer player
if isMaximizing:
idealscore = -1000
for key in board:
if board[key] == ' ':
board[key] = bot
score = minimax(board, False, bot, human)
board[key] = ' '
if score > idealscore:
idealscore = score
return idealscore
# enemy bot
else:
idealscore = 1000
for key in board:
if board[key] == ' ':
board[key] = human
score = minimax(board, True, bot, human)
board[key] = ' '
if score < idealscore:
idealscore = score
return idealscore