This repository was archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtictactoe.py
198 lines (150 loc) · 4.89 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
"""
Tic Tac Toe Player
"""
import math
from copy import deepcopy
# Initialize X, O, and EMPTY
X = 'X'
O = 'O'
EMPTY = None
def initial_state():
"""
Returns the initial state of the board, being an EMPTY 2D numpy array.
"""
return [[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def player(board):
"""
Returns the player who has the next turn on a board.
"""
x_counter = 0
o_counter = 0
# Loop through the board and increment the X/O counts if found
for row in board:
for item in row:
if item == X:
x_counter += 1
if item == O:
o_counter += 1
# X is the initial player, therefore prioritized over O
if x_counter <= o_counter:
return X
else:
return O
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
actions = set()
# Loop through all items in the board and retrieve all EMPTY spaces
# This is done by tracking the current item in the loop through enumerate()
for i, row in enumerate(board):
for j, item in enumerate(row):
if item == EMPTY:
actions.add((i, j))
return actions
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
if action not in actions(board):
raise ActionError("The given action was invalid.")
# Unpack 'action', make a deepcopy of the board, get the next player,
# then assign the next player's value to our resulting action
i, j = action
result_board = deepcopy(board)
next_player = player(board)
result_board[i][j] = next_player
return result_board
def winner(board):
"""
Returns the winner of the game. Returns None if the game tied or is still in progress.
"""
wins = [[(0, 0), (0, 1), (0, 2)], # Horizontal Top
[(1, 0), (1, 1), (1, 2)], # Horizontal Middle
[(2, 0), (2, 1), (2, 2)], # Horizontal Bottom
[(0, 0), (1, 1), (2, 2)], # Diagonal
[(0, 2), (1, 1), (2, 0)], # Diagonal
[(0, 0), (1, 0), (2, 0)], # Vertical Left
[(0, 1), (1, 1), (2, 1)], # Vertical Middle
[(0, 2), (1, 2), (2, 2)]] # Vertical Right
for win in wins:
x_counter = 0
o_counter = 0
for i, j in win:
if board[i][j] == X:
x_counter += 1
if board[i][j] == O:
o_counter += 1
if x_counter == 3:
return X
if o_counter == 3:
return O
return None
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
# Checks if any of the board's spots remain EMPTY or if the board has not yet crowned a winner
if not actions(board) or winner(board) is not None:
return True
return False
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
game_winner = winner(board)
if game_winner == X:
return 1
if game_winner == O:
return -1
return 0
def minimax(board):
"""
Returns the optimal action for the next player on the board.
"""
if terminal(board):
return None
# Optimizing the AI to use the most optimal moves at the start
if board[1][1] == EMPTY and player(board) == O:
return 1, 1
if board == initial_state():
return 0, 0
next_player = player(board)
best_value = -2 if next_player == X else 2
# Looping through all possible actions and
# finding the most valuable (optimal) action
for action in actions(board):
new_value = find_value(result(board, action), best_value)
if next_player == X:
new_value = max(best_value, new_value)
if next_player == O:
new_value = min(best_value, new_value)
if new_value != best_value:
best_value = new_value
optimal_action = action
return optimal_action
def find_value(board, best_value):
"""
Finds the best value for each recursive iteration. Optimized to use Alpha-Beta Pruning.
"""
if terminal(board):
return utility(board)
next_player = player(board)
value = -2 if next_player == X else 2
# Loops through all possible actions and finds their corresponding value
for action in actions(board):
new_value = find_value(result(board, action), value)
if next_player == X:
if new_value > best_value:
return new_value
value = max(value, new_value)
if next_player == O:
if new_value < best_value:
return new_value
value = min(value, new_value)
return value
class ActionError(Exception):
"""Raised when an action is None or returns an invalid value."""
pass