|
@@ -0,0 +1,239 @@
|
|
1
|
+
|
|
2
|
+import random
|
|
3
|
+import Tkinter as tk
|
|
4
|
+import sys
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+class Application(tk.Frame):
|
|
8
|
+
|
|
9
|
+ def __init__(self, width=21, height=21, size=10):
|
|
10
|
+ tk.Frame.__init__(self)
|
|
11
|
+ self.maze = Maze(width, height)
|
|
12
|
+ self.size = size
|
|
13
|
+ self.steps = 0
|
|
14
|
+ self.grid()
|
|
15
|
+ self.create_widgets()
|
|
16
|
+ self.draw_maze()
|
|
17
|
+ self.create_events()
|
|
18
|
+
|
|
19
|
+ def create_widgets(self):
|
|
20
|
+ width = self.maze.width * self.size
|
|
21
|
+ height = self.maze.height * self.size
|
|
22
|
+ self.canvas = tk.Canvas(self, width=width, height=height)
|
|
23
|
+ self.canvas.grid()
|
|
24
|
+ self.status = tk.Label(self)
|
|
25
|
+ self.status.grid()
|
|
26
|
+
|
|
27
|
+ def draw_maze(self):
|
|
28
|
+ for i, row in enumerate(self.maze.maze):
|
|
29
|
+ for j, col in enumerate(row):
|
|
30
|
+ x0 = j * self.size
|
|
31
|
+ y0 = i * self.size
|
|
32
|
+ x1 = x0 + self.size
|
|
33
|
+ y1 = y0 + self.size
|
|
34
|
+ color = self.get_color(x=j, y=i)
|
|
35
|
+ id = self.canvas.create_rectangle(x0, y0, x1, y1, width=0, fill=color)
|
|
36
|
+ if self.maze.start_cell == (j, i):
|
|
37
|
+ self.cell = id
|
|
38
|
+
|
|
39
|
+ self.canvas.tag_raise(self.cell) # bring to front
|
|
40
|
+ self.status.config(text='Movidas mínimas: %d' % self.maze.steps)
|
|
41
|
+
|
|
42
|
+ def create_events(self):
|
|
43
|
+ self.canvas.bind_all('<KeyPress-Up>', self.move_cell)
|
|
44
|
+ self.canvas.bind_all('<KeyPress-Down>', self.move_cell)
|
|
45
|
+ self.canvas.bind_all('<KeyPress-Left>', self.move_cell)
|
|
46
|
+ self.canvas.bind_all('<KeyPress-Right>', self.move_cell)
|
|
47
|
+
|
|
48
|
+ def move_cell(self, event):
|
|
49
|
+ if event.keysym == 'Up':
|
|
50
|
+ if self.check_move(0, -1):
|
|
51
|
+ self.canvas.move(self.cell, 0, -self.size)
|
|
52
|
+ self.steps += 1
|
|
53
|
+ if event.keysym == 'Down':
|
|
54
|
+ if self.check_move(0, 1):
|
|
55
|
+ self.canvas.move(self.cell, 0, self.size)
|
|
56
|
+ self.steps += 1
|
|
57
|
+ if event.keysym == 'Left':
|
|
58
|
+ if self.check_move(-1, 0):
|
|
59
|
+ self.canvas.move(self.cell, -self.size, 0)
|
|
60
|
+ self.steps += 1
|
|
61
|
+ if event.keysym == 'Right':
|
|
62
|
+ if self.check_move(1, 0):
|
|
63
|
+ self.canvas.move(self.cell, self.size, 0)
|
|
64
|
+ self.steps += 1
|
|
65
|
+
|
|
66
|
+ args = (self.steps, self.maze.steps)
|
|
67
|
+ self.status.config(text='Movidas: %d/%d' % args)
|
|
68
|
+ self.check_status()
|
|
69
|
+
|
|
70
|
+ def check_move(self, x, y):
|
|
71
|
+ x0, y0 = self.get_cell_coords()
|
|
72
|
+ x1 = x0 + x
|
|
73
|
+ y1 = y0 + y
|
|
74
|
+ return self.maze.maze[y1][x1] == 0
|
|
75
|
+
|
|
76
|
+ def get_cell_coords(self):
|
|
77
|
+ position = self.canvas.coords(self.cell)
|
|
78
|
+ x = int(position[0] / self.size)
|
|
79
|
+ y = int(position[1] / self.size)
|
|
80
|
+ return (x, y)
|
|
81
|
+
|
|
82
|
+ def check_status(self):
|
|
83
|
+ if self.maze.exit_cell == self.get_cell_coords():
|
|
84
|
+ args = (self.steps, self.maze.steps)
|
|
85
|
+ self.status.config(text='Resuelto en %d/%d movidas!' % args)
|
|
86
|
+
|
|
87
|
+ def get_color(self, x, y):
|
|
88
|
+ if self.maze.start_cell == (x, y):
|
|
89
|
+ return 'red'
|
|
90
|
+ if self.maze.exit_cell == (x, y):
|
|
91
|
+ return 'green'
|
|
92
|
+ if self.maze.maze[y][x] == 1:
|
|
93
|
+ return 'black'
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+class Maze(object):
|
|
97
|
+
|
|
98
|
+ """
|
|
99
|
+ ## MAZE GENERATOR ##
|
|
100
|
+ Based on Depth-first search algorithm:
|
|
101
|
+ http://en.wikipedia.org/wiki/Maze_generation_algorithm#Depth-first_search
|
|
102
|
+ 1. Start at a particular cell and call it the "exit."
|
|
103
|
+ 2. Mark the current cell as visited, and get a list of its neighbors.
|
|
104
|
+ For each neighbor, starting with a randomly selected neighbor:
|
|
105
|
+ 1. If that neighbor hasn't been visited, remove the wall between
|
|
106
|
+ this cell and that neighbor, and then recurse with that neighbor as
|
|
107
|
+ the current cell.
|
|
108
|
+ __author__ = "Leonardo Vidarte"
|
|
109
|
+ """
|
|
110
|
+
|
|
111
|
+ def __init__(self, width=21, height=21, exit_cell=(1, 1)):
|
|
112
|
+ self.width = width
|
|
113
|
+ self.height = height
|
|
114
|
+ self.exit_cell = exit_cell
|
|
115
|
+ self.create()
|
|
116
|
+
|
|
117
|
+ def create(self):
|
|
118
|
+ self.maze = [[1] * self.width for _ in range(self.height)] # full of walls
|
|
119
|
+ self.start_cell = None
|
|
120
|
+ self.steps = None
|
|
121
|
+ self.recursion_depth = None
|
|
122
|
+ self._visited_cells = []
|
|
123
|
+ self._visit_cell(self.exit_cell)
|
|
124
|
+
|
|
125
|
+ def _visit_cell(self, cell, depth=0):
|
|
126
|
+ x, y = cell
|
|
127
|
+ self.maze[y][x] = 0 # remove wall
|
|
128
|
+ self._visited_cells.append(cell)
|
|
129
|
+ neighbors = self._get_neighbors(cell)
|
|
130
|
+ random.shuffle(neighbors)
|
|
131
|
+ for neighbor in neighbors:
|
|
132
|
+ if not neighbor in self._visited_cells:
|
|
133
|
+ self._remove_wall(cell, neighbor)
|
|
134
|
+ self._visit_cell(neighbor, depth+1)
|
|
135
|
+ self._update_start_cell(cell, depth)
|
|
136
|
+
|
|
137
|
+ def _get_neighbors(self, cell):
|
|
138
|
+ """
|
|
139
|
+ Get the cells next to the cell
|
|
140
|
+ Example:
|
|
141
|
+ Given the following mazes
|
|
142
|
+ The a neighbor's are b
|
|
143
|
+ # # # # # # # # # # # # # #
|
|
144
|
+ # # # b # # # # a # b # # #
|
|
145
|
+ # # # # # # # # # # # # # #
|
|
146
|
+ # b # a # b # # b # # # # #
|
|
147
|
+ # # # # # # # # # # # # # #
|
|
148
|
+ # # # b # # # # # # # # # #
|
|
149
|
+ # # # # # # # # # # # # # #
|
|
150
|
+ """
|
|
151
|
+ x, y = cell
|
|
152
|
+ neighbors = []
|
|
153
|
+
|
|
154
|
+ # Left
|
|
155
|
+ if x - 2 > 0:
|
|
156
|
+ neighbors.append((x-2, y))
|
|
157
|
+ # Right
|
|
158
|
+ if x + 2 < self.width:
|
|
159
|
+ neighbors.append((x+2, y))
|
|
160
|
+ # Up
|
|
161
|
+ if y - 2 > 0:
|
|
162
|
+ neighbors.append((x, y-2))
|
|
163
|
+ # Down
|
|
164
|
+ if y + 2 < self.height:
|
|
165
|
+ neighbors.append((x, y+2))
|
|
166
|
+
|
|
167
|
+ return neighbors
|
|
168
|
+
|
|
169
|
+ def _remove_wall(self, cell, neighbor):
|
|
170
|
+ """
|
|
171
|
+ Remove the wall between two cells
|
|
172
|
+ Example:
|
|
173
|
+ Given the cells a and b
|
|
174
|
+ The wall between them is w
|
|
175
|
+ # # # # #
|
|
176
|
+ # # # # #
|
|
177
|
+ # a w b #
|
|
178
|
+ # # # # #
|
|
179
|
+ # # # # #
|
|
180
|
+ """
|
|
181
|
+ x0, y0 = cell
|
|
182
|
+ x1, y1 = neighbor
|
|
183
|
+ # Vertical
|
|
184
|
+ if x0 == x1:
|
|
185
|
+ x = x0
|
|
186
|
+ y = (y0 + y1) / 2
|
|
187
|
+ # Horizontal
|
|
188
|
+ if y0 == y1:
|
|
189
|
+ x = (x0 + x1) / 2
|
|
190
|
+ y = y0
|
|
191
|
+ self.maze[y][x] = 0 # remove wall
|
|
192
|
+
|
|
193
|
+ def _update_start_cell(self, cell, depth):
|
|
194
|
+ if depth > self.recursion_depth:
|
|
195
|
+ self.recursion_depth = depth
|
|
196
|
+ self.start_cell = cell
|
|
197
|
+ self.steps = depth * 2 # wall + cell
|
|
198
|
+
|
|
199
|
+ def show(self, verbose=False):
|
|
200
|
+ MAP = {0: ' ', # path
|
|
201
|
+ 1: '#', # wall
|
|
202
|
+ 2: 'B', # exit
|
|
203
|
+ 3: 'A', # start
|
|
204
|
+ }
|
|
205
|
+ x0, y0 = self.exit_cell
|
|
206
|
+ self.maze[y0][x0] = 2
|
|
207
|
+ x1, y1 = self.start_cell
|
|
208
|
+ self.maze[y1][x1] = 3
|
|
209
|
+ for row in self.maze:
|
|
210
|
+ print ' '.join([MAP[col] for col in row])
|
|
211
|
+ if verbose:
|
|
212
|
+ print "Steps from A to B:", self.steps
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+if __name__ == '__main__':
|
|
216
|
+
|
|
217
|
+ from optparse import OptionParser
|
|
218
|
+
|
|
219
|
+ parser = OptionParser(description="Random maze game")
|
|
220
|
+ parser.add_option('-W', '--width', type=int, default=21,
|
|
221
|
+ help="maze width (default 21)")
|
|
222
|
+ parser.add_option('-H', '--height', type=int, default=21,
|
|
223
|
+ help="maze height (default 21)")
|
|
224
|
+ parser.add_option('-s', '--size', type=int, default=10,
|
|
225
|
+ help="cell size (default 10)")
|
|
226
|
+ args, _ = parser.parse_args()
|
|
227
|
+
|
|
228
|
+ for arg in ('width', 'height'):
|
|
229
|
+ if getattr(args, arg) % 2 == 0:
|
|
230
|
+ setattr(args, arg, getattr(args, arg) + 1)
|
|
231
|
+ print "Warning: %s must be odd, using %d instead" % \
|
|
232
|
+ (arg, getattr(args, arg))
|
|
233
|
+
|
|
234
|
+ sys.setrecursionlimit(5000)
|
|
235
|
+
|
|
236
|
+ app = Application(args.width, args.height, args.size)
|
|
237
|
+ app.master.title('Maze game')
|
|
238
|
+ app.mainloop()
|
|
239
|
+
|