-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.py
180 lines (146 loc) · 5.67 KB
/
graphics.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
from generation import GeneratedMaze
from maze import Maze, Coord
from tkinter import Frame, Tk, Radiobutton, IntVar, Button
VIEW_SIZE = 600
WIDTH = 8
PAD = 10
def set_all_buttons(buttons, state: str) -> None:
for button in buttons:
button.config(state=state)
class Board:
def __init__(self, maze: Maze):
self.root = Tk()
self.root.title('Maze Machine')
self.root.resizable(False, False)
self.quit = False
self.in_process = False
self.selection = 0
# self.root.bind('<KeyPress>', self.on_key_press)
self.root.protocol("WM_DELETE_WINDOW", self.on_quit)
self.maze = maze
self.tiles = []
self.init_center()
self.init_controls()
def init_center(self) -> None:
center = Frame(self.root, bg='black', width=VIEW_SIZE, height=VIEW_SIZE)
center.grid(row=0, column=1)
for r in range(self.maze.size):
self.tiles.append([])
for c in range(self.maze.size):
tile = Frame(center, width=VIEW_SIZE / self.maze.size, height=VIEW_SIZE / self.maze.size)
tile.grid(row=r, column=c)
tile.bind('<1>', lambda event, coord=Coord(r, c): self.on_mouse_click(event, coord))
self.tiles[r].append(tile)
self.__update_tile(Coord(r, c))
def init_controls(self) -> None:
controls = Frame(self.root, bg='black', width=200, height=VIEW_SIZE, pady=100)
controls.grid(row=0, column=0)
controls.grid_propagate(False)
controls.grid_columnconfigure(0, weight=1)
choices = {
'Start': 0,
'End': 1
}
row = iter(range(10))
radios = []
var = IntVar(value=2)
for text, value in choices.items():
radio = Radiobutton(controls, text=text, value=value, bg='black', fg='white', variable=var,
command=lambda: self.select(var), activebackground='blue')
radio.grid(row=next(row), column=0)
radios.append(radio)
bfs_button = Button(controls, width=WIDTH, text='BFS')
bfs_button.grid(row=next(row), column=0, pady=PAD)
dfs_button = Button(controls, width=WIDTH, text='DFS')
dfs_button.grid(row=next(row), column=0, pady=PAD)
regenerate_button = Button(controls, width=WIDTH, text='Generate\nNew Maze')
regenerate_button.grid(row=next(row), column=0, pady=PAD)
all_buttons = [bfs_button, dfs_button, regenerate_button, *radios]
bfs_button.config(command=lambda: self.show_callback(self.maze.bfs, radios))
dfs_button.config(command=lambda: self.show_callback(self.maze.dfs, radios))
regenerate_button.config(command=lambda: self.regenerate(all_buttons))
def reset(self) -> None:
self.maze.reset()
self.update_all()
def regenerate(self, buttons) -> None:
self.maze = GeneratedMaze()
self.show_callback(self.maze.generate, buttons)
def select(self, var) -> None:
self.selection = var.get()
def on_quit(self) -> None:
self.quit = True
quit(0)
def show_callback(self, callback: callable, buttons: [Button]) -> None:
self.in_process = True
set_all_buttons(buttons, 'disabled')
self.reset()
delay = 31.25 / (self.maze.size ** 2)
callback(self, delay)
self.in_process = False
set_all_buttons(buttons, 'normal')
def update_all(self) -> None:
for r, row in enumerate(self.maze.spaces):
for c, space in enumerate(row):
self.__update_tile(Coord(r, c))
def update(self, coords: [Coord]):
for coord in coords:
if coord:
self.__update_tile(coord)
def __update_tile(self, coord: Coord) -> None:
if self.quit:
quit(1)
r = coord.r
c = coord.c
space = self.maze.spaces[r][c]
color = 'white'
if space == 1:
color = 'black'
if coord in self.maze.visited:
color = '#bbbbbb'
if coord in self.maze.found:
color = 'red'
if coord == self.maze.start:
color = 'orange'
if coord == self.maze.end:
color = 'green'
self.tiles[r][c].configure(bg=color)
def on_mouse_click(self, e, coord: Coord) -> None:
if self.in_process:
return
if coord == self.maze.start:
return
r = coord.r
c = coord.c
to_update = [coord]
curr_space = self.maze.spaces[r][c]
if self.selection == 0 and not curr_space and coord != self.maze.end:
# Do not reorder
to_update.append(self.maze.start)
self.maze.start = coord
if self.selection == 1 and not curr_space:
to_update.append(self.maze.end)
if coord == self.maze.end:
self.maze.end = None
else:
self.maze.end = coord
self.update(to_update)
# # w 119
# # a 97
# # s 115
# # d 100
# def on_key_press(self, e):
# old_p = self.maze.start
# [up, right, down, left] = get_adjacent(self.maze.start)
# if e.keycode == 119:
# if self.maze.check_movable(up):
# self.maze.start = up
# if e.keycode == 100:
# if self.maze.check_movable(right):
# self.maze.start = right
# if e.keycode == 115:
# if self.maze.check_movable(down):
# self.maze.start = down
# if e.keycode == 97:
# if self.maze.check_movable(left):
# self.maze.start = left
# self.update([old_p, self.maze.start])