-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
225 lines (170 loc) · 4.57 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
"""
Tic Tac Toe Player
"""
import math
from random import randint
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.
"""
x_count = 0
o_count = 0
#Count no of X and no of O in the board
for row in board:
x_count += row.count(X)
o_count += row.count(O)
#Find the turn
if x_count > o_count:
return O
else:
return X
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
action = []
#Find empty cells in the board
for i,row in enumerate(board):
for j,col in enumerate(row):
if not board[i][j]:
action.append((i,j))
return action
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
new_board = []
x,y = action
turn = player(board)
#Fill on certain cell and return the resulting board
for i,row in enumerate(board):
tmp = []
for j,col in enumerate(row):
if x == i and y == j:
if col:
raise Exception("Invalid Move")
tmp.append(turn)
else:
tmp.append(col)
new_board.append(tmp)
return new_board
def winner(board):
"""
Returns the winner of the game, if there is one.
"""
inverted_board = []
#Invert the board
for i,row in enumerate(board):
tmp = []
for j,col in enumerate(row):
tmp.append(board[j][i])
inverted_board.append(tmp)
#Check if three rows are same or not
for row in board:
if row.count("X") == 3:
return X
elif row.count("O") == 3:
return O
#Agian for the inverted board
for row in inverted_board:
if row.count("X") == 3:
return X
elif row.count("O") == 3:
return O
#Check diagonal
if board[0][0] == board[1][1] == board[2][2]:
return board[1][1]
elif board[0][2] == board[1][1] == board[2][0]:
return board[1][1]
#No-winner
return None
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
if winner(board):
return True
#Check if any cell is empty
for row in board:
for col in row:
if not col:
return False
return True
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
won = winner(board)
#1,0,-1
if won == X:
return 1
elif won == O:
return -1
return 0
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
if terminal(board):
return None
#The first move
elif board == initial_state():
return [(0,0),(0,2),(1,1),(2,0),(2,2)][randint(0,4)]
#Whose turn
turn = player(board)
#If AI is X
if turn == X:
moves = max_value(board)
return moves[0]
#If AI is Y
else:
moves = min_value(board)
return moves[0]
def max_value(board):
"""
IF AI is X, find it's optimal move.
"""
max_results = []
#If game-over, return utility of the board
if terminal(board):
return (1,utility(board))
#Find all possible moves(recursion)
for action in actions(board):
#Get the value of the previous state
value = min_value(result(board,action))
#Alpha-Beta Pruning
if value == 1:
return ((action), value)
max_results.append((action, value[1]))
#Return the best action
max_results.sort(key=lambda a:a[1], reverse=True)
return max_results[0]
def min_value(board):
"""
IF AI is Y, find it's optimal move.
"""
min_results = []
#If game-over, return utility of the board
if terminal(board):
return (1,utility(board))
#Find all possible moves(recursion)
for action in actions(board):
#Get the value of the previous state
value = max_value(result(board,action))
#Alpha-Beta Pruning
if value == -1:
return ((action), value)
min_results.append((action,value[1]))
#Return the best action
min_results.sort(key=lambda a:a[1])
return min_results[0]