-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.py
262 lines (180 loc) · 5.41 KB
/
tictactoe.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"""
Tic Tac Toe Player
"""
from ctypes import util
import math
import copy
from this import d
from tkinter import E
from numpy import moveaxis
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 = 0
# for i in range(len(board)):
# for j in range(len(board)):
# if board[i][j] == X:
# count += 1
# if count == 5:
# return None
# if count == 0:
# return X
# elif count % 2 == 1:
# return X
# else:
# return O
countx = 0
counto = 0
for row in range(len(board)):
for col in range(len(board)):
if board[row][col] == X:
countx += 1
elif board[row][col] == O:
counto += 1
if not terminal(board) and countx == counto:
return X
elif countx > counto:
return O
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
action = set()
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == EMPTY:
action.add((i, j))
return action
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
if action not in actions(board):
raise NameError("Invalid move")
elif terminal(board):
raise NameError("Game is over")
newboard = copy.deepcopy(board)
newboard[action[0]][action[1]] = player(board)
return newboard
def winner(board):
"""
Returns the winner of the game, if there is one.
"""
for row in range(len(board)):
if board[row][0] == board[row][1] == board[row][2] == X:
return X
elif board[row][0] == board[row][1] == board[row][2] == O:
return O
for col in range(len(board)):
if board[0][col] == board[1][col] == board[2][col] == X:
return X
elif board[0][col] == board[1][col] == board[2][col] == O:
return O
if board[0][0] == board[1][1] == board[2][2] == X or board[0][2] == board[1][1] == board[2][0] == X:
return X
if board[0][2] == board[1][1] == board[2][0] == O or board[0][0] == board[1][1] == board[2][2] == O:
return O
return None
def isTie(board):
"""
Returns True if game is over, False otherwise.
"""
if terminal(board) == True and utility(board) == 0:
return True
return False
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
if winner(board) != None:
return True
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == EMPTY:
return False
return True
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
if winner(board) == X:
return 1
elif winner(board) == O:
return -1
else:
return 0
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
# if terminal(board) == True and utility(board) == 0:
# return None
# for action in actions(board):
# if utility(result(board,action)) == 1 and player(result(board,action)) == X:
# return action
# elif utility(result(board,action)) == -1 and player(result(board,action)) == O:
# return action
# elif utility(result(board,action)) == 0:
# return minimax(result(board,action))
# if terminal(board):
# return None
# if player(board) == X:
# arr = []
# for action in actions(board):
# arr.append([min_val(result(board,action)), action])
# return sorted(arr, key=lambda x: x[0], reverse=True)[0][1]
# elif player(board) == O:
# arr = []
# for action in actions(board):
# arr.append([max_val(result(board,action)), action])
# return sorted(arr, key=lambda x: x[0])[0][1]
if board == [[EMPTY]*3]*3:
return (0,0)
if player(board) == X:
v = float("-inf")
slected_action = None
for action in actions(board):
minValRes = min_val(result(board,action))
if minValRes > v:
v = minValRes
slected_action = action
elif player(board) == O:
v= float("inf")
selected_action = None
for action in actions(board):
maxValRes = max_val(result(board,action))
if maxValRes < v:
v = maxValRes
slected_action = action
return slected_action
def max_val(board):
"""
Returns the maximum value of the current board
"""
if terminal(board) == True:
return utility(board)
v = float("-inf")
for action in actions(board):
v = max(v, min_val(result(board,action)))
return v
def min_val(board):
"""
Returns the minimum value of the current board
"""
if terminal(board) == True:
return utility(board)
v = float("inf")
for action in actions(board):
v = min(v, max_val(result(board,action)))
return v