-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
238 lines (189 loc) · 6.75 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from os import system
import time
from math import inf as infinity
class TicTacToe:
def __init__(self):
self.board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
def drawBoard(self):
print(' ', self.board[0], '|', self.board[1], '|', self.board[2], ' ')
print('----------------')
print(' ', self.board[3], '|', self.board[4], '|', self.board[5], ' ')
print('----------------')
print(' ', self.board[6], '|', self.board[7], '|', self.board[8], ' ')
def drawUpdatedBoard(self, board):
print(' ', board[0], '|', board[1], '|', board[2], ' ')
print('----------------')
print(' ', board[3], '|', board[4], '|', board[5], ' ')
print('----------------')
print(' ', board[6], '|', board[7], '|', board[8], ' ')
def emptySpots(self):
"""Makes a list containing the indexes of all the free/open positions on the board."""
result = []
for i, j in enumerate(self.board):
if j == ' ':
result.append(i)
return result
def winner(self, player):
""" Tells you if the player you are checking for has won the game. For example if player 1 has 3 in a row but
player 2 is checking if he himself won, the function will return False."""
i = 0
while i < 9:
if self.board[i] == self.board[i + 1] == self.board[i + 2] == player:
return True
else:
i += 3
i = 0
while i < 3:
if self.board[i] == self.board[i + 3] == self.board[i + 6] == player:
return True
else:
i += 1
if self.board[0] == self.board[4] == self.board[8] == player:
return True
elif self.board[2] == self.board[4] == self.board[6] == player:
return True
return False
def evaluate(self):
if self.winner('O'):
score = +1
elif self.winner('X'):
score = -1
else:
score = 0
def boardFull(self):
counter = 0
for i in self.board:
if i == ' ':
counter += 1
return not (counter >= 1)
def gameOver(self):
return self.winner('X') or self.winner('O') or self.boardFull()
def clean(self):
system('cls')
def humanTurn(self, player):
depth = len(self.emptySpots())
if depth == 0 or self.gameOver():
return
move = -1
while move < 1 or move > 9:
# clean()
print("Human turn")
self.drawBoard()
move = int(input('Enter a position between 1 and 9: '))
if 9 >= move >= 1:
if self.board[move - 1] == ' ':
move -= 1
if player == 'X':
self.board[move] = 'X'
else:
self.board[move] = 'O'
self.drawBoard()
return
else:
print('This position is not free')
move = -1
time.sleep(1)
else:
print('Bad move')
move = -1
class ArtificialIntelligence:
def __init__(self, board):
self.board = board
self.score = None
def winner(self, player):
""" Tells you if the player you are checking for has won the game. For example if player 1 has 3 in a row but
player 2 is checking if he himself won, the function will return False."""
i = 0
while i < 9:
if self.board[i] == self.board[i + 1] == self.board[i + 2] == player:
return True
else:
i += 3
i = 0
while i < 3:
if self.board[i] == self.board[i + 3] == self.board[i + 6] == player:
return True
else:
i += 1
if self.board[0] == self.board[4] == self.board[8] == player:
return True
elif self.board[2] == self.board[4] == self.board[6] == player:
return True
return False
def boardFull(self):
counter = 0
for i in self.board:
if i == ' ':
counter += 1
return not (counter >= 1)
def emptySpots(self):
"""Makes a list containing the indexes of all the free/open positions on the board."""
result = []
for i, j in enumerate(self.board):
if j == ' ':
result.append(i)
return result
def gameOver(self):
return self.winner('X') or self.winner('O') or self.boardFull()
def evaluate(self):
if self.winner('O'):
self.score = +1
return self.score
elif self.winner('X'):
self.score = -1
return self.score
else:
self.score = 0
return self.score
def minimax(self, board, depth, player):
if player == 'O':
best = [-1, -infinity]
else:
best = [-1, infinity]
if depth == 0 or self.gameOver():
self.score = self.evaluate() # checks who winner is - returns a 1, -1, or 0 for tie
return [-1, self.score]
for spot in self.emptySpots():
# changes the blank spot on the board at index (spot) to X or O depending on the turn
self.board[spot] = player
if player == 'O':
self.score = self.minimax(self.board, depth - 1, 'X')
else:
self.score = self.minimax(self.board, depth - 1, 'O')
self.board[spot] = ' '
self.score[0] = spot
if player == 'O':
if best[1] < self.score[1]:
best = self.score
else:
if best[1] > self.score[1]:
best = self.score
return best
def AImove(self, lengthEmpty, gameOver):
""" lengthEmpty has to be self.emptySpots(self.board) and gameOver is self.gameOver(self.board)"""
depth = len(lengthEmpty)
if depth == 0 or gameOver:
return
copyBoard = []
for i in self.board:
copyBoard.append(i)
print("AI turn")
move = self.minimax(copyBoard, depth, 'O')
print(move)
self.board[move[0]] = 'O'
return self.board
def main():
game = TicTacToe()
game.drawBoard()
while not (game.gameOver()):
game.humanTurn('X')
AI = ArtificialIntelligence(game.board)
move = AI.AImove(game.emptySpots(), game.gameOver())
game.drawUpdatedBoard(move)
if game.winner('X'):
print('Player 1 Wins!')
elif game.winner('O'):
print('Player 2 Wins!')
elif game.boardFull():
print('Its a Tie')
main()