-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
208 lines (156 loc) · 4.64 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
"""
Tic Tac Toe Player
"""
import math
import copy
import random
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.
"""
counter = {"X":0, "O":0}
for i in board:
for j in i:
if j is not None:
counter[j] +=1
return X if counter['X'] == counter['O'] else O
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
possible_moves = set()
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] == EMPTY:
possible_moves.add((i, j))
return possible_moves
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
result_board = copy.deepcopy(board)
if action is not None and result_board[action[0]][action[1]] == EMPTY:
next_player = player(board)
result_board[action[0]][action[1]] = next_player
return result_board
else:
raise ImpossibleMoveError
def winner(board):
"""
Returns the winner of the game, if there is one.
"""
win_states = win_indexes(len(board))
for indexes in win_states:
if all(board[r][c] == X for r, c in indexes):
return X
if all(board[r][c] == O for r, c in indexes):
return O
return None
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
if winner(board) is not None:
return True
for i in board:
if i.count(EMPTY) > 0:
return False
# no more moves
return True
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
winner_player = winner(board)
if winner_player is None:
return 0
return player_goal(winner_player)
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
# no more moves
if terminal(board):
return None
# get the current player
cur_player = player(board)
# best found option for max player (init opposite val)
alpha = -2
# best found option for min player (init opposite val)
beta = 2
possible_actions = actions(board)
if cur_player == X:
# first move - return rand move
if len(possible_actions) == 9:
return get_random_move(len(board) -1)
# max player
(b, best_move) = max_value(board, alpha, beta)
else:
# min player
(b, best_move) = min_value(board, alpha, beta)
return best_move
def min_value(board, alpha, beta):
best_move = None
# no more moves
if terminal(board):
return (utility(board), best_move)
best_min_val = 2
possible_actions = actions(board)
for action in possible_actions:
next_board = result(board, action)
(res_val, res_move) = max_value(next_board, alpha, beta)
if best_min_val > res_val:
best_min_val = res_val
best_move = action
#the alpha beta prunning part:
if best_min_val <= alpha:
return (best_min_val, best_move)
if best_min_val < beta:
beta = best_min_val
return (best_min_val, best_move)
def max_value(board, alpha, beta):
best_move = None
# no more moves
if terminal(board):
return (utility(board), best_move)
best_max_val = -2
possible_actions = actions(board)
for action in possible_actions:
next_board = result(board, action)
(res_val, res_move) = min_value(next_board, alpha, beta)
if best_max_val < res_val:
best_max_val = res_val
best_move = action
#the alpha beta prunning part:
if best_max_val >= beta:
return (best_max_val, best_move)
if best_max_val > alpha:
alpha = best_max_val
return (best_max_val, best_move)
def win_indexes(n):
# Rows
for r in range(n):
yield [(r, c) for c in range(n)]
# Columns
for c in range(n):
yield [(r, c) for r in range(n)]
# Diagonal top left to bottom right
yield [(i, i) for i in range(n)]
# Diagonal top right to bottom left
yield [(i, n - 1 - i) for i in range(n)]
def player_goal(player):
return 1 if player == X else -1
def get_random_move(max):
i = random.randrange(max)
j = random.randrange(max)
return (i, j)