-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.py
220 lines (187 loc) · 5.37 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
"""
Tic Tac Toe Player
"""
import math
import copy
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.
"""
finalplayer = 0
for row in board:
for column in row:
if column == EMPTY:
i=0
elif column == X:
i=1
elif column == O:
i=-1
finalplayer = finalplayer + i
if finalplayer > 0 :
return O
elif finalplayer < 0 :
return X
else :
return X
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
possibleActions = set()
for i in range(0, len(board)):
for j in range(0, len(board[0])):
if board[i][j] == EMPTY:
possibleActions.add((i, j))
return possibleActions
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
x = action[0]
y = action[1]
user = player(board)
trialboard = [row[:] for row in board] # Deep copy to avoid modifying original board
# Check if action coordinates are within bounds
if 0 <= x < len(trialboard) and 0 <= y < len(trialboard[0]):
trialboard[x][y] = user
return trialboard
def winner(board) :
def diagonalSums(board):
principal = 0
secondary = 0
for i in range(3):
if board[i][i] == 1:
principal += 1
elif board[i][i] == -1:
principal -= 1
if board[i][2 - i] == 1:
secondary += 1
elif board[i][2 - i] == -1:
secondary -= 1
if principal == 3 or secondary == 3:
return 3
elif principal == -3 or secondary == -3:
return -3
return 0
def columnSums(board):
column_sums = [0] * len(board[0])
for row in board:
for i in range(len(row)):
column_sums[i] += row[i]
columnSum1 = column_sums[0]
columnSum2 = column_sums[1]
columnSum3 = column_sums[2]
if columnSum1 == 3 or columnSum2 == 3 or columnSum3 == 3:
return 3
elif columnSum1 == -3 or columnSum2 == -3 or columnSum3 == -3:
return -3
else:
return 0
def rowSums(board):
row_sums = [sum(row) for row in board]
rowSum1 = row_sums[0]
rowSum2 = row_sums[1]
rowSum3 = row_sums[2]
if rowSum1 == 3 or rowSum2 == 3 or rowSum3 == 3:
return 3
elif rowSum1 == -3 or rowSum2 == -3 or rowSum3 == -3:
return -3
else:
return 0
trialboard = [[0] * 3 for _ in range(3)]
for i in range(3):
for j in range(3):
if board[i][j] == X:
trialboard[i][j] = 1
elif board[i][j] == O:
trialboard[i][j] = -1
if diagonalSums(trialboard) == 3 or columnSums(trialboard) == 3 or rowSums(trialboard) == 3:
return X
elif diagonalSums(trialboard) == -3 or columnSums(trialboard) == -3 or rowSums(trialboard) == -3:
return O
else:
return None
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
if winner(board) is not None:
return True
else:
for row in board:
for cell in row:
if cell == 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
raise NotImplementedError
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
if terminal(board):
return None
else:
user = player(board)
if user == X:
value, move = max_value(board)
else:
value, move = min_value(board)
return move
def max_value(board):
actionSet = actions(board)
valueSet = []
moveSet = []
for action in actionSet:
trialboard = result(board, action)
if terminal(trialboard):
return utility(trialboard), action
else:
value, _ = min_value(trialboard)
valueSet.append(value)
moveSet.append(action)
maxValue = valueSet[0]
move = moveSet[0]
for idx in range(1, len(valueSet)):
if valueSet[idx] > maxValue:
maxValue = valueSet[idx]
move = moveSet[idx]
return maxValue, move
def min_value(board):
actionSet = actions(board)
valueSet = []
moveSet = []
for action in actionSet:
trialboard = result(board, action)
if terminal(trialboard):
return utility(trialboard), action
else:
value, _ = max_value(trialboard)
valueSet.append(value)
moveSet.append(action)
minValue = valueSet[0]
move = moveSet[0]
for idx in range(1, len(valueSet)):
if valueSet[idx] < minValue:
minValue = valueSet[idx]
move = moveSet[idx]
return minValue, move