Repository navigation
Expand file tree
/
Copy pathMain.py
More file actions
171 lines (132 loc) · 5.71 KB
/
Copy pathMain.py
File metadata and controls
171 lines (132 loc) · 5.71 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import copy
import threading
import sys
from GameBoard import *
from Solver import Solver
pygame.init()
screen_width, screen_height = 1000, 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Rush Hour")
background_color = (50, 150, 255)
button_color = (0, 128, 255)
button_hover_color = (0, 200, 255)
gameBoards = []
solvers = []
for i in range(10):
gameBoard = GameBoard(6, 6, i)
gameBoard.static_setup(i)
gameBoards.append(gameBoard)
solver_types = ["dfs", "greedy", "aStar"]
for i in range(3):
solvers.append(Solver(copy.deepcopy(gameBoards[0])))
class Button:
def __init__(self, x, y, width, height, text, action=None):
self.rect = pygame.Rect(x, y, width, height)
self.text = text
self.action = action
def draw(self, screen):
color = button_hover_color if self.rect.collidepoint(pygame.mouse.get_pos()) else button_color
pygame.draw.rect(screen, color, self.rect)
font = pygame.font.SysFont("Arial", 24)
text_surface = font.render(self.text, True, (255, 255, 255))
screen.blit(text_surface, (self.rect.x + (self.rect.width - text_surface.get_width()) // 2,
self.rect.y + (self.rect.height - text_surface.get_height()) // 2))
def click(self):
if self.action:
self.action()
class Text:
def __init__(self, x, y, width, height, text):
self.rect = pygame.Rect(x, y, width, height)
self.text = text
def draw(self, screen):
color = (0,0,0)
pygame.draw.rect(screen, color, self.rect)
font = pygame.font.SysFont("Arial", 21)
text_surface = font.render(self.text, True, (255, 255, 255))
screen.blit(text_surface, (self.rect.x + (self.rect.width - text_surface.get_width()) // 2,
self.rect.y + (self.rect.height - text_surface.get_height()) // 2))
def start_solving(solver_index, solver_type):
solver = solvers[solver_index]
if solver_type == "dfs":
threading.Thread(target=solver.dfsRaw).start()
elif solver_type == "greedy":
threading.Thread(target=solver.greedy).start()
elif solver_type == "aStar":
threading.Thread(target=solver.aStar).start()
def select_board(board_index):
global selected_board
selected_board = board_index
print(f"Selected Board {board_index + 1}")
for solver in solvers:
gameBoards[selected_board].reset()
solver.gameBoard = copy.deepcopy(gameBoards[selected_board])
solver.current_board = copy.deepcopy(gameBoards[selected_board])
solver.reset()
def select_solver(solver_index):
global selected_solver
selected_solver = solver_index
print(f"Selected Solver {solver_index + 1}")
def reset_game():
global selected_board
gameBoards[selected_board].reset()
for solver in solvers:
solver.gameBoard = copy.deepcopy(gameBoards[selected_board])
solver.current_board = copy.deepcopy(gameBoards[selected_board])
solver.reset()
def mainLoop():
global selected_board, selected_solver
clock = pygame.time.Clock()
board_buttons = []
for i in range(10):
board_button = Button(10, 10 + i * 50, 180, 40, f"Board {i + 1}", action=lambda i=i: select_board(i))
board_buttons.append(board_button)
solver_buttons = []
for i, solver_type in enumerate(solver_types):
solver_button = Button(screen_width - 190, 10 + i * 50, 180, 40, f"{solver_type.capitalize()} Solver",
action=lambda i=i: select_solver(i))
solver_buttons.append(solver_button)
start_button = Button(screen_width // 2 - 180, screen_height - 100, 160, 50, "Start Solving",
action=lambda: start_solving(selected_solver, solver_types[selected_solver]))
reset_button = Button(screen_width // 2 + 20, screen_height - 100, 160, 50, "Reset Game", action=reset_game)
while True:
screen.fill(background_color)
for button in board_buttons:
button.draw(screen)
for button in solver_buttons:
button.draw(screen)
start_button.draw(screen)
reset_button.draw(screen)
text_solver_type = Text(250, 50, 130, 40, f"{solver_types[selected_solver].capitalize()} Solver")
text_solver_type.draw(screen)
nodes_count = len(solvers[selected_solver].visited)
text_nodes_visited = Text(385, 50, 180, 40, f"Nodes visited: {nodes_count}")
text_nodes_visited.draw(screen)
solution_count = len(solvers[selected_solver].solution_path)
if not solution_count:
solution_count = "Not found"
else:
solution_count = f"{solution_count} moves"
text_solution = Text(570, 50, 180, 40, f"Solution: {solution_count}")
text_solution.draw(screen)
solvers[selected_solver].get_current_board().draw(screen, x=250, y=100, size=500)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
for button in board_buttons:
if button.rect.collidepoint(event.pos):
button.click()
for button in solver_buttons:
if button.rect.collidepoint(event.pos):
button.click()
if start_button.rect.collidepoint(event.pos):
start_button.click()
if reset_button.rect.collidepoint(event.pos):
reset_button.click()
clock.tick(60)
if __name__ == "__main__":
selected_board = 0
selected_solver = 0
mainLoop()