-
Notifications
You must be signed in to change notification settings - Fork 0
/
testingconnect4model.py
362 lines (298 loc) · 13.6 KB
/
testingconnect4model.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import numpy as np
import random
import sys
import math
from tensorflow.keras.models import load_model
import matplotlib.pyplot as plt
class ConnectFour:
## SET BOARD PARAMETERS HERE, CAN PLAY AROUND WITH THEM IN DEEP NEURAL NET POSSIBLY
def __init__(self, rows=6, columns=7):
self.rows = rows
self.columns = columns
self.USER = 0
self.AI = 1
self.EMPTY = 0
self.USER_PIECE = 1
self.AI_PIECE = 2
self.WIN_RANGE = 4
self.board = self.create_board()
self.turn = random.choice([self.USER, self.AI])
def create_board(self):
board = np.zeros((self.rows, self.columns))
return board
def drop_piece(self, board, row, col, piece):
if row is not None and 0 <= row < self.rows and 0 <= col < self.columns:
board[row][col] = piece
else:
raise ValueError(f"Attempted to drop piece in an invalid location: row {row}, col {col}")
def is_valid_location(self, board, col):
return board[self.rows - 1][col] == self.EMPTY
def get_next_open_row(self, board, col):
for r in range(self.rows):
if board[r][col] == self.EMPTY:
return r
def print_board(self, board):
print(np.flip(board, 0))
# check if any piece has won either player the game
def winning_move(self, board, piece):
for c in range(self.columns - self.WIN_RANGE + 1):
for r in range(self.rows):
if (board[r][c] == piece and board[r][c + 1] == piece and
board[r][c + 2] == piece and board[r][c + 3] == piece):
return True
for c in range(self.columns):
for r in range(self.rows - self.WIN_RANGE + 1):
if (board[r][c] == piece and board[r + 1][c] == piece and
board[r + 2][c] == piece and board[r + 3][c] == piece):
return True
for c in range(self.columns - self.WIN_RANGE + 1):
for r in range(self.rows - self.WIN_RANGE + 1):
if (board[r][c] == piece and board[r + 1][c + 1] == piece and
board[r + 2][c + 2] == piece and board[r + 3][c + 3] == piece):
return True
if (board[r + 3][c] == piece and board[r + 3 - 1][c + 1] == piece and
board[r + 3 - 2][c + 2] == piece and board[r + 3 - 3][c + 3] == piece):
return True
return False
def is_game_over(self):
if self.winning_move(self.board, self.USER_PIECE) or self.winning_move(self.board, self.AI_PIECE):
return True
if len(self.get_valid_locations(self.board)) == 0:
return True
return False
################################################################################
##################### MINIMAX AND ALPHA BETA PRUNING ALGORITHMS ################
################################################################################
def evaluate_window(self, window, piece):
score = 0
opp_piece = self.USER_PIECE
if piece == self.USER_PIECE:
opp_piece = self.AI_PIECE
if window.count(piece) == self.WIN_RANGE:
score += 100
elif (window.count(piece) == self.WIN_RANGE - 1 and
window.count(self.EMPTY) == 1):
score += 5
elif (window.count(piece) == self.WIN_RANGE - 2 and
window.count(self.EMPTY) == 2):
score += 2
if (window.count(opp_piece) == self.WIN_RANGE - 1 and
window.count(self.EMPTY) == 1):
score -= 4
return score
def score_position(self, board, piece):
score = 0
center_array = [int(i) for i in list(board[:, self.columns // 2])]
center_count = center_array.count(piece)
score += center_count * 3
for r in range(self.rows):
row_array = [int(i) for i in list(board[r, :])]
for c in range(self.columns - self.WIN_RANGE + 1):
window = row_array[c:c + self.WIN_RANGE]
score += self.evaluate_window(window, piece)
for c in range(self.columns):
col_array = [int(i) for i in list(board[:, c])]
for r in range(self.rows - self.WIN_RANGE + 1):
window = col_array[r:r + self.WIN_RANGE]
score += self.evaluate_window(window, piece)
for r in range(self.rows - self.WIN_RANGE + 1):
for c in range(self.columns - self.WIN_RANGE + 1):
window = [board[r + i][c + i] for i in range(self.WIN_RANGE)]
score += self.evaluate_window(window, piece)
for r in range(self.rows - self.WIN_RANGE + 1):
for c in range(self.columns - self.WIN_RANGE + 1):
window = [board[r + self.WIN_RANGE - 1 - i][c + i] for i in range(self.WIN_RANGE)]
score += self.evaluate_window(window, piece)
return score
def is_terminal_node(self, board):
return (self.winning_move(board, self.USER_PIECE) or
self.winning_move(board, self.AI_PIECE) or
len(self.get_valid_locations(board)) == 0)
def minimax(self, board, depth, alpha, beta, maximizingPlayer):
valid_locations = self.get_valid_locations(board)
is_terminal = self.is_terminal_node(board)
if depth == 0 or is_terminal:
if is_terminal:
if self.winning_move(board, self.AI_PIECE):
return (None, 100000000000000)
elif self.winning_move(board, self.USER_PIECE):
return (None, -10000000000000)
else: # Game is over, no more valid moves
return (None, 0)
else: # Depth is zero
return (None, self.score_position(board, self.AI_PIECE))
if maximizingPlayer:
value = -math.inf
column = random.choice(valid_locations)
for col in valid_locations:
row = self.get_next_open_row(board, col)
b_copy = np.copy(board)
self.drop_piece(b_copy, row, col, self.AI_PIECE)
new_score = self.minimax(b_copy, depth - 1, alpha, beta, False)[1]
if new_score > value:
value = new_score
column = col
alpha = max(alpha, value)
if alpha >= beta:
break
return column, value
else: # Minimizing player
value = math.inf
column = random.choice(valid_locations)
for col in valid_locations:
row = self.get_next_open_row(board, col)
b_copy = np.copy(board)
self.drop_piece(b_copy, row, col, self.USER_PIECE)
new_score = self.minimax(b_copy, depth - 1, alpha, beta, True)[1]
if new_score < value:
value = new_score
column = col
beta = min(beta, value)
if alpha >= beta:
break
return column, value
def get_valid_locations(self, board):
valid_locations = []
for col in range(self.columns):
if self.is_valid_location(board, col):
valid_locations.append(col)
return valid_locations
def pick_best_move(self, board, piece):
valid_locations = self.get_valid_locations(board)
best_score = -10000
best_col = random.choice(valid_locations)
for col in valid_locations:
row = self.get_next_open_row(board, col)
temp_board = np.copy(board)
self.drop_piece(temp_board, row, col, piece)
score = self.score_position(temp_board, piece)
if score > best_score:
best_score = score
best_col = col
return best_col
################################################################################
########################## PLAYING THE ACTUAL GAME #############################
################################################################################
def draw_board(self, board):
for r in range(self.rows):
for c in range(self.columns):
if board[r][c] == self.USER_PIECE:
print("P ", end="")
elif board[r][c] == self.AI_PIECE:
print("AI ", end="")
else:
print("- ", end="")
print()
print()
def ai_make_move(self):
# Load the model
model = load_model('connect4_new_model.h5')
# Flatten the board and reshape it to match the model's input shape
board_flattened = np.flip(self.board, 0).flatten().reshape((1,42))
# Use the model to predict the probabilities for each column
predictions = model.predict(board_flattened)[0]
print(predictions)
# Sort the columns by predicted probability in descending order
sorted_columns = np.argsort(predictions)[::-1]
# Try each column in order until a valid one is found
for col in sorted_columns:
if self.is_valid_location(self.board, col):
return col
# If no valid column is found (which should never happen), return None
return None
def play_game(self):
game_over = False
while not game_over:
self.print_board(self.board)
if self.turn == self.USER:
try:
col = int(input(f"Player 1, enter column (0-{self.columns - 1}): "))
if not self.is_valid_location(self.board, col):
print("Invalid move. Try again.")
continue
except ValueError:
print("Invalid input. Please enter a number.")
continue
else:
col = self.ai_make_move() # AI makes a move
row = self.get_next_open_row(self.board, col)
# self.col_moves[col] -= 1
self.drop_piece(self.board, row, col, self.turn + 1)
if self.winning_move(self.board, self.turn + 1):
self.print_board(self.board)
print(f"Player {self.turn + 1} wins!!")
game_over = True
elif len(self.get_valid_locations(self.board)) == 0:
print("It's a draw!")
game_over = True
print("-" * (self.columns * 4))
self.turn = 1 - self.turn
def play_game_against_random(self, num_games=100):
num_wins = 0
for _ in range(num_games):
game_over = False
self.board = self.create_board() # Reset the board
while not game_over:
if self.turn == self.USER:
col = self.ai_make_move() # AI makes a move
else:
valid_locations = self.get_valid_locations(self.board)
col = np.random.choice(valid_locations) # Random AI makes a move
row = self.get_next_open_row(self.board, col)
self.drop_piece(self.board, row, col, self.turn + 1)
if self.winning_move(self.board, self.turn + 1):
if self.turn == self.USER:
num_wins += 1
game_over = True
elif len(self.get_valid_locations(self.board)) == 0:
game_over = True
self.turn = 1 - self.turn
win_rate = num_wins / num_games
return win_rate
def play_game_against_minimax(self, num_games):
num_wins = 0
for _ in range(num_games):
game_over = False
self.board = self.create_board() # Reset the board
while not game_over:
if self.turn == self.USER:
best_move = self.ai_make_move() # CNN model makes a move
else:
best_move, _ = self.minimax(self.board, 5, -sys.maxsize, sys.maxsize, True) # Minimax makes a move
row = self.get_next_open_row(self.board, best_move)
self.drop_piece(self.board, row, best_move, self.turn + 1)
if self.winning_move(self.board, self.turn + 1):
if self.turn == self.USER:
num_wins += 1
game_over = True
elif len(self.get_valid_locations(self.board)) == 0: # Check for a draw
game_over = True
self.turn = self.USER if self.turn == self.AI else self.AI
win_rate = num_wins / num_games
return win_rate
def plot_win_rate_against_random(self, num_games_list=[5, 10, 20, 50, 100, 200]):
win_rates = []
for num_games in num_games_list:
win_rate = self.play_game_against_random(num_games)
win_rates.append(win_rate)
# Plot the win rates
plt.plot(num_games_list, win_rates, marker='o')
plt.xlabel('Number of Games')
plt.ylabel('Win Rate')
plt.savefig('win_rate_vs_random_ai.png') # Save the plot as an image
plt.show()
def plot_win_rate_against_minimax(self, num_games_list=[5, 10, 20, 50, 100, 200]):
win_rates = []
for num_games in num_games_list:
win_rate = self.play_game_against_minimax(num_games)
win_rates.append(win_rate)
# Plot the win rates
plt.plot(num_games_list, win_rates, marker='o')
plt.xlabel('Number of Games')
plt.ylabel('Win Rate')
plt.savefig('win_rate_vs_minimax_ai.png') # Save the plot as an image
plt.show()
if __name__ == '__main__':
connect = ConnectFour(rows=6, columns=7)
connect.plot_win_rate_against_random()
connect.plot_win_rate_against_minimax()