-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathai.py
More file actions
226 lines (188 loc) · 5.68 KB
/
ai.py
File metadata and controls
226 lines (188 loc) · 5.68 KB
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
"""
Tic Tac Toe Player
"""
import copy
import math
# class IllegalActionError(Exception):
# def __init__(self, value):
# self.value = value
# def __str__(self):
# return(repr(self.value))
X = "X"
O = "O"
EMPTY = None
def initial_state():
"""
Returns starting state of the board.
"""
return [[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def player(board):
"""
Returns player who has the next turn on a board.
"""
count_x = count_o = 0
for row in board:
for cell in row:
if cell == X:
count_x += 1
elif cell == O:
count_o += 1
return (X, O)[count_x > count_o]
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
if (terminal(board)):
return utility(board)
actions = set()
i = j = 0
for row in board:
j = 0
for cell in row:
if cell == EMPTY:
actions.add((i, j))
j += 1
i += 1
return actions
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
legalActions = actions(board) # not as in lawsuits
if action not in legalActions:
raise Exception("Illegal Action on board")
turn = player(board)
result = copy.deepcopy(board)
result[action[0]][action[1]] = turn
return result
def winner(board):
"""
Returns the winner of the game, if there is one.
"""
if (utility(board) == -1):
return O
if (utility(board) == 1):
return X
else:
return None
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
empty_cell = False
count_x = count_o = count_xd = count_od = count_xdi = count_odi = count_xdig = count_odig = 0
for i in range(3):
for j in range(3):
if board[i][j] == X:
count_x += 1
elif board[i][j] == O:
count_o += 1
else:
empty_cell = True
if board[j][i] == X:
count_xd += 1
elif board[j][i] == O:
count_od += 1
if (count_o == 3 or count_x == 3 or count_od == 3 or count_xd == 3):
return True
count_o = count_x = 0
count_od = count_xd = 0
if board[2-i][i] == X:
count_xdig += 1
elif board[2-i][i] == O:
count_odig += 1
if board[i][i] == X:
count_xdi += 1
elif board[i][i] == O:
count_odi += 1
if (count_odi == 3 or count_xdi == 3 or count_odig == 3 or count_xdig == 3):
return True
if (empty_cell == False):
return True
return False
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
count_x = count_o = count_xd = count_od = count_xdi = count_odi = count_xdig = count_odig = 0
for i in range(3):
for j in range(3):
if board[i][j] == X:
count_x += 1
elif board[i][j] == O:
count_o += 1
if board[j][i] == X:
count_xd += 1
elif board[j][i] == O:
count_od += 1
if (count_o == 3 or count_od == 3):
return -1
elif (count_x == 3 or count_xd == 3):
return 1
count_o = count_x = 0
count_od = count_xd = 0
if board[2-i][i] == X:
count_xdig += 1
elif board[2-i][i] == O:
count_odig += 1
if board[i][i] == X:
count_xdi += 1
elif board[i][i] == O:
count_odi += 1
if (count_odi == 3 or count_odig == 3):
return -1
elif (count_xdi == 3 or count_xdig == 3):
return 1
return 0
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
if terminal(board): # check if game over
return None
if board == initial_state():
return (1, 1) # this is the optimal move so ion wanna let it think too much
turn = player(board) # set whose turn it is
optimalAction = None
legalActions = actions(board)
if turn == X: # check whose turn it is
v = -math.inf # kinda like dijkstras algo here
for action in legalActions:
# searches for best outcome for that specific action RECURSIVELY
max = min_value(result(board, action))
if (max > v): # if that outcome better or same compared to current chosen action
v = max # ...replace with new max value
optimalAction = action # and new action
elif turn == O:
v = math.inf
for action in legalActions:
min = max_value(result(board, action))
if (min < v):
v = min
optimalAction = action
return optimalAction
def max_value(board):
"""
minmax helper function
"""
if terminal(board): # check if game over
return utility(board) # if yes return winner value
v = -math.inf
legalActions = actions(board)
for action in legalActions:
# for each action check best outcome in the eyes of the opponent recursively
v = max(v, min_value(result(board, action)))
return v
def min_value(board):
"""
minmax helper function
"""
if terminal(board):
return utility(board)
v = math.inf
legalActions = actions(board)
for action in legalActions:
v = min(v, max_value(result(board, action)))
return v