-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
423 lines (359 loc) · 15.3 KB
/
main.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import time
import tkinter as tk
# Function to create an empty board
def create_board():
board = []
for _ in range(8):
row = []
for _ in range(8):
row.append(' ')
board.append(row)
return board
# Function to update a cell on the board
def update_cell(row, col):
global current_player, board
# Check if the selected cell is empty
if board[row][col] == ' ':
# Check if stacking is valid based on adjacent cells
if col > 0 and board[row][col - 1] != ' ':
board[row][col] = current_player
button = buttons[row][col]
button.config(text=current_player)
# Check if the current player wins or the game is a tie
if check_win(board, current_player):
message = f'Player {current_player} wins!'
label.config(text=message)
disable_buttons()
elif is_board_full(board):
message = 'It\'s a tie!'
label.config(text=message)
disable_buttons()
else:
# Switch the current player
current_player = '□' if current_player == '■' else '■'
message = f'Current Player: {current_player}'
label.config(text=message)
if mode == "2" and current_player == '□':
row, col = find_best_move(board, current_player)
update_cell(row, col)
elif mode == "3" and current_player == '■':
row, col = find_best_move(board, current_player)
update_cell(row, col)
elif col < 7 and board[row][col + 1] != ' ':
board[row][col] = current_player
button = buttons[row][col]
button.config(text=current_player)
if check_win(board, current_player):
message = f'Player {current_player} wins!'
label.config(text=message)
disable_buttons()
elif is_board_full(board):
message = 'It\'s a tie!'
label.config(text=message)
disable_buttons()
else:
current_player = '□' if current_player == '■' else '■'
message = f'Current Player: {current_player}'
label.config(text=message)
if mode == "2" and current_player == '□':
row, col = find_best_move(board, current_player)
update_cell(row, col)
elif mode == "3" and current_player == '■':
row, col = find_best_move(board, current_player)
update_cell(row, col)
elif col == 0 or col == 7:
board[row][col] = current_player
button = buttons[row][col]
button.config(text=current_player)
if check_win(board, current_player):
message = f'Player {current_player} wins!'
label.config(text=message)
disable_buttons()
elif is_board_full(board):
message = 'It\'s a tie!'
label.config(text=message)
disable_buttons()
else:
current_player = '□' if current_player == '■' else '■'
message = f'Current Player: {current_player}'
label.config(text=message)
if mode == "2" and current_player == '□':
row, col = find_best_move(board, current_player)
update_cell(row, col)
elif mode == "3" and current_player == '■':
row, col = find_best_move(board, current_player)
update_cell(row, col)
else:
# Invalid move if none of the adjacent cells are occupied
message = f'Invalid move. You can only stack the brick next to the wall or another brick.'
label.config(text=message)
def disable_buttons():
for row in range(8):
for col in range(8):
buttons[row][col].config(state=tk.DISABLED)
# Initialize the board
board = [[' ' for _ in range(8)] for _ in range(8)]
# Display the Board
def print_board1(board):
print(" ", end="")
for i in range(8):
print(f" {i} ", end="\t")
print("\n +" + "---+" * 8)
for i in range(8):
print(f"{i} |", end="")
for j in range(8):
print(f" {board[i][j]} |", end="")
print("\n +" + "---+" * 8)
# Function to check if a player has won
def check_win(board, player):
# Check rows
for i in range(8):
for j in range(4):
if all(board[i][j + k] == player for k in range(5)):
return True
# Check columns
for i in range(4):
for j in range(8):
if all(board[i + k][j] == player for k in range(5)):
return True
# Check diagonals (top left to bottom right)
for i in range(4):
for j in range(4):
if all(board[i + k][j + k] == player for k in range(5)):
return True
# Check diagonals (top right to bottom left)
for i in range(4):
for j in range(4, 8):
if all(board[i + k][j - k] == player for k in range(5)):
return True
return False
# Function to evaluate the board state for the AI player
def evaluate_board(board, player):
# Evaluate the board based on your own criteria
ai_score = 0
# Evaluate rows
for i in range(8):
for j in range(4):
window = board[i][j:j + 5]
ai_score += evaluate_window(window, player)
# Evaluate columns
for j in range(8):
for i in range(4):
window = [board[i + k][j] for k in range(5)]
ai_score += evaluate_window(window, player)
# Evaluate diagonals (top left to bottom right)
for i in range(4):
for j in range(4):
window = [board[i + k][j + k] for k in range(5)]
ai_score += evaluate_window(window, player)
# Evaluate diagonals (top right to bottom left)
for i in range(4):
for j in range(4, 8):
window = [board[i + k][j - k] for k in range(5)]
ai_score += evaluate_window(window, player)
return ai_score
# Helper function to evaluate a window of 5 consecutive cells
def evaluate_window(window, player):
ai_count = window.count(player)
opponent_count = window.count('■' if player == '□' else '□')
if ai_count == 5:
return 1000
elif ai_count == 4 and opponent_count == 0:
return 30
elif ai_count == 3 and opponent_count == 0:
return 10
elif ai_count == 2 and opponent_count == 0:
return 2
elif opponent_count == 4 and ai_count == 0:
return -90
elif opponent_count == 3 and ai_count == 0:
return -30
return 0
# Function to perform minimax algorithm with alpha-beta pruning
def minimax(board, depth, maximizing_player, alpha, beta, player):
# Check if the game is over or maximum depth is reached
if depth == 0 or check_win(board, player) or check_win(board, '□' if player == '■' else '■'):
if check_win(board, player):
return evaluate_board(board, player) + 100 # AI wins
elif check_win(board, '□' if player == '■' else '■'):
return evaluate_board(board, player) - 100 # Opponent wins
else:
return evaluate_board(board, player) # Game is a draw
if maximizing_player:
max_eval = float('-inf')
for row in range(8):
for col in range(8):
if board[row][col] == ' ':
board[row][col] = player
eval = minimax(board, depth - 1, False, alpha, beta, player)
board[row][col] = ' '
max_eval = max(max_eval, eval)
alpha = max(alpha, eval)
if beta <= alpha:
break
if beta <= alpha:
break
return max_eval
else:
min_eval = float('inf')
for row in range(8):
for col in range(8):
if board[row][col] == ' ':
board[row][col] = '□' if player == '■' else '■'
eval = minimax(board, depth - 1, True, alpha, beta, player)
board[row][col] = ' '
min_eval = min(min_eval, eval)
beta = min(beta, eval)
if beta <= alpha:
break
if beta <= alpha:
break
return min_eval
# Function to check if the board is full
def is_board_full(board):
for row in board:
if ' ' in row:
return False
return True
# Function to create the GUI using tkinter
def create_gui():
global buttons, current_player, board, label
root = tk.Tk()
root.title("Magnetic Cave")
root.configure(bg="light pink") # Set root window background color to baby blue
# Configure rows and columns to resize properly
for i in range(8):
root.grid_rowconfigure(i, weight=1, minsize=50)
root.grid_columnconfigure(i, weight=1, minsize=50)
root.grid_rowconfigure(8, weight=1, minsize=30)
buttons = []
for row in range(8):
button_row = []
for col in range(8):
button = tk.Button(root, text=' ', width=5, height=2, command=lambda r=row, c=col: update_cell(r, c),
bg="light pink", font=("Arial", 14)) # Increase font size to 14
button.grid(row=row, column=col, sticky="nsew") # Use sticky option to fill the available space
button_row.append(button)
buttons.append(button_row)
board = create_board()
current_player = '■'
label = tk.Label(root, text=f'Current Player: {current_player}', bg="light pink", font=("Arial", 16)) # Increase font size to 16
label.grid(row=8, columnspan=8, sticky="nsew") # Use sticky option to fill the available space
if mode == "3" and current_player == '■':
row, col = find_best_move(board, current_player)
update_cell(row, col)
root.mainloop()
# Function to find the best move using the minimax algorithm with alpha-beta pruning
def find_best_move(board, player):
print("AI IS THINKING ... ")
best_score = float('-inf')
best_move = None
start_time = time.time()
for row in range(8):
for col in range(8):
if board[row][col] == ' ':
if col > 0 and board[row][col - 1] != ' ':
# Stack the brick next to the left wall
board[row][col] = player
if check_win(board, player):
board[row][col] = ' '
return row, col # Complete the bridge with the 5th brick
score = minimax(board, 2, False, float('-inf'), float('inf'), player)
board[row][col] = ' '
if score > best_score:
best_score = score
best_move = (row, col)
elif col < 7 and board[row][col + 1] != ' ':
# Stack the brick next to the right wall
board[row][col] = player
if check_win(board, player):
board[row][col] = ' '
return row, col # Complete the bridge with the 5th brick
score = minimax(board, 2, False, float('-inf'), float('inf'), player)
board[row][col] = ' '
if score > best_score:
best_score = score
best_move = (row, col)
elif col == 0 or col == 7:
# Stack the brick on either side of the cave
board[row][col] = player
if check_win(board, player):
board[row][col] = ' '
return row, col # Complete the bridge with the 5th brick
score = minimax(board, 2, False, float('-inf'), float('inf'), player)
board[row][col] = ' '
if score > best_score:
best_score = score
best_move = (row, col)
end_time = time.time()
time_taken = end_time - start_time
print(f"Time taken: {time_taken} seconds")
return best_move
# Function to get the next move from a player
def get_move(player):
while True:
try:
row, col = map(int, input(
f'Player {player}, enter the row and column (e.g., 2 3 for row 2 and column 3): ').split())
if row >= 0 and row < 8 and col >= 0 and col < 8:
if board[row][col] == ' ':
if col > 0 and board[row][col - 1] != ' ':
return row, col
elif col < 7 and board[row][col + 1] != ' ':
return row, col
elif col == 0 or col == 7:
return row, col
else:
print('Invalid move. You can only stack the brick next to the wall or another brick.')
else:
print('Invalid move. The cell is already occupied.')
else:
print('Invalid move. Out of board bounds. Try again.')
except ValueError:
print('Invalid input. Try again.')
# Game loop
current_player = '■'
FLAG = True
while FLAG:
mode = input("Enter the mode \n1. Manual entry for both ■’s moves and □’s moves\n2. Manual entry for ■’s moves & "
"automatic moves for □ \n3. Manual entry for □’s moves & automatic moves for ■): \n")
create_gui()
print_board1(board)
if mode == "1":
if current_player == '■':
row, col = get_move(current_player)
else:
row, col = get_move(current_player)
elif mode == "2":
if current_player == '■':
row, col = get_move(current_player)
else:
row, col = find_best_move(board, current_player)
elif mode == "3":
if current_player == '■':
row, col = find_best_move(board, current_player)
else:
row, col = get_move(current_player)
else:
print("Invalid mode. Please choose 1, 2, or 3.")
break
board[row][col] = current_player
if check_win(board, current_player):
print_board1(board)
print(f'Player {current_player} wins!')
FLAG = False
if is_board_full(board):
print_board1(board)
print('It\'s a tie!')
FLAG = False
LOOP = input("Do you want to play again? y/n")
if LOOP == 'y' or LOOP == 'Y':
FLAG = True
elif LOOP == 'n' or LOOP == 'N':
FLAG = False
else:
print("Invalid , If you want to play again enter y or n")
FLAG = True
# Reset the board and switch player for the next game
board = [[' ' for _ in range(8)] for _ in range(8)]
current_player = '□' if current_player=='■'else'■'