Repository navigation
Expand file tree
/
Copy pathsimulator.py
More file actions
154 lines (127 loc) · 4.53 KB
/
Copy pathsimulator.py
File metadata and controls
154 lines (127 loc) · 4.53 KB
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
import pygame
import argparse
import importlib
from data.classes.Board import Board
pygame.init()
WINDOW_SIZE = (600, 600)
screen = pygame.display.set_mode(WINDOW_SIZE)
def draw(display, board):
display.fill("white")
board.draw(display)
pygame.display.update()
def run_game(bot1_class, bot2_class, delay):
board = Board(WINDOW_SIZE[0], WINDOW_SIZE[1])
bot1 = bot1_class()
bot2 = bot2_class()
running = True
while running:
mx, my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
board.handle_click(mx, my)
print(board.last_captured)
if board.turn == "black":
move = bot1.move("black", board)
else:
move = bot2.move("white", board)
board.handle_move(*move)
pygame.time.delay(delay)
if board.is_in_checkmate("black"):
print("White wins!")
running = False
elif board.is_in_checkmate("white"):
print("Black wins!")
running = False
elif board.is_in_draw():
print("Draw!")
running = False
draw(screen, board)
# ... [rest of the code remains unchanged above] ...
if __name__ == "__main__":
print("Made it this far 2")
parser = argparse.ArgumentParser()
parser.add_argument(
"--bot1",
type=str,
default="random_bot",
help="Bot for black (e.g. 'random_bot')",
)
parser.add_argument(
"--bot2",
type=str,
default="random_bot",
help="Bot for white (e.g. 'random_bot')",
)
parser.add_argument(
"--delay", type=int, default=0, help="Delay in ms between moves"
)
parser.add_argument(
"--simulations", type=int, default=1, help="Number of simulations to run"
)
args = parser.parse_args()
try:
bot1_module = importlib.import_module(f"data.classes.bots.{args.bot1}")
bot2_module = importlib.import_module(f"data.classes.bots.{args.bot2}")
bot1_class = bot1_module.Bot
bot2_class = bot2_module.Bot
except ModuleNotFoundError as e:
print(f"Error: Could not find bot module - {e}")
exit()
bot1_wins = 0
bot2_wins = 0
draws = 0
for i in range(args.simulations):
print(f"\n--- Starting Simulation {i + 1} ---")
def run_game_with_result():
board = Board(WINDOW_SIZE[0], WINDOW_SIZE[1])
bot1 = bot1_class()
bot2 = bot2_class()
running = True
while running:
mx, my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
board.handle_click(mx, my)
print(board.last_captured)
if board.turn == "black":
move = bot1.move("black", board)
else:
move = bot2.move("white", board)
success = board.handle_move(*move)
if not success:
print("invalid move. think about it...")
while True:
pass
pygame.time.delay(args.delay)
draw(screen, board)
if board.is_in_checkmate("black"):
print("White wins!")
return "white"
elif board.is_in_checkmate("white"):
print("Black wins!")
return "black"
elif board.is_in_draw():
print("Draw!")
return "draw"
result = run_game_with_result()
if result == "black":
bot1_wins += 1 # bot1 always plays black
elif result == "white":
bot2_wins += 1 # bot2 always plays white
elif result == "draw":
draws += 1
total = bot1_wins + bot2_wins + draws
print(f"\n=== Simulation Results: {args.bot1} (Black) vs {args.bot2} (White) ===")
print(
f"{args.bot1} (bot1/black) wins: {bot1_wins} ({(bot1_wins / total) * 100:.1f}%)"
)
print(
f"{args.bot2} (bot2/white) wins: {bot2_wins} ({(bot2_wins / total) * 100:.1f}%)"
)
print(f"Draws: {draws} ({(draws / total) * 100:.1f}%)")