|
@@ -0,0 +1,225 @@
|
|
1
|
+#!/usr/bin/env python
|
|
2
|
+# -*- coding: utf-8 -*-
|
|
3
|
+
|
|
4
|
+import random
|
|
5
|
+import Tkinter as tk
|
|
6
|
+import sys
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+class Application(tk.Frame):
|
|
10
|
+
|
|
11
|
+ def __init__(self, width=21, height=21, size=10):
|
|
12
|
+ tk.Frame.__init__(self)
|
|
13
|
+ self.maze = Maze(width, height)
|
|
14
|
+ self.size = size
|
|
15
|
+ self.steps = 0
|
|
16
|
+ self.grid()
|
|
17
|
+ self.create_widgets()
|
|
18
|
+ self.draw_maze()
|
|
19
|
+ self.create_events()
|
|
20
|
+
|
|
21
|
+ def create_widgets(self):
|
|
22
|
+ width = self.maze.width * self.size
|
|
23
|
+ height = self.maze.height * self.size
|
|
24
|
+ self.canvas = tk.Canvas(self, width=width, height=height)
|
|
25
|
+ self.canvas.grid()
|
|
26
|
+ self.status = tk.Label(self)
|
|
27
|
+ self.status.grid()
|
|
28
|
+
|
|
29
|
+ def draw_maze(self):
|
|
30
|
+ for i, row in enumerate(self.maze.maze):
|
|
31
|
+ for j, col in enumerate(row):
|
|
32
|
+ x0 = j * self.size
|
|
33
|
+ y0 = i * self.size
|
|
34
|
+ x1 = x0 + self.size
|
|
35
|
+ y1 = y0 + self.size
|
|
36
|
+ color = self.get_color(x=j, y=i)
|
|
37
|
+ id = self.canvas.create_rectangle(x0, y0, x1, y1, width=0, fill=color)
|
|
38
|
+ if self.maze.start_cell == (j, i):
|
|
39
|
+ self.cell = id
|
|
40
|
+
|
|
41
|
+ self.canvas.tag_raise(self.cell)
|
|
42
|
+ self.status.config(text='minimale Anzahl Schritte: %d' % self.maze.steps)
|
|
43
|
+
|
|
44
|
+ def create_events(self):
|
|
45
|
+ self.canvas.bind_all('<KeyPress-Up>', self.move_cell)
|
|
46
|
+ self.canvas.bind_all('<KeyPress-Down>', self.move_cell)
|
|
47
|
+ self.canvas.bind_all('<KeyPress-Left>', self.move_cell)
|
|
48
|
+ self.canvas.bind_all('<KeyPress-Right>', self.move_cell)
|
|
49
|
+
|
|
50
|
+ def move_cell(self, event):
|
|
51
|
+ if event.keysym == 'Up':
|
|
52
|
+ if self.check_move(0, -1):
|
|
53
|
+ self.canvas.move(self.cell, 0, -self.size)
|
|
54
|
+ self.steps += 1
|
|
55
|
+ if event.keysym == 'Down':
|
|
56
|
+ if self.check_move(0, 1):
|
|
57
|
+ self.canvas.move(self.cell, 0, self.size)
|
|
58
|
+ self.steps += 1
|
|
59
|
+ if event.keysym == 'Left':
|
|
60
|
+ if self.check_move(-1, 0):
|
|
61
|
+ self.canvas.move(self.cell, -self.size, 0)
|
|
62
|
+ self.steps += 1
|
|
63
|
+ if event.keysym == 'Right':
|
|
64
|
+ if self.check_move(1, 0):
|
|
65
|
+ self.canvas.move(self.cell, self.size, 0)
|
|
66
|
+ self.steps += 1
|
|
67
|
+
|
|
68
|
+ args = (self.steps, self.maze.steps)
|
|
69
|
+ self.status.config(text='Schritte: %d/%d' % args)
|
|
70
|
+ self.check_status()
|
|
71
|
+
|
|
72
|
+ def check_move(self, x, y):
|
|
73
|
+ x0, y0 = self.get_cell_coords()
|
|
74
|
+ x1 = x0 + x
|
|
75
|
+ y1 = y0 + y
|
|
76
|
+ return self.maze.maze[y1][x1] == 0
|
|
77
|
+
|
|
78
|
+ def get_cell_coords(self):
|
|
79
|
+ position = self.canvas.coords(self.cell)
|
|
80
|
+ x = int(position[0] / self.size)
|
|
81
|
+ y = int(position[1] / self.size)
|
|
82
|
+ return (x, y)
|
|
83
|
+
|
|
84
|
+ def check_status(self):
|
|
85
|
+ if self.maze.exit_cell == self.get_cell_coords():
|
|
86
|
+ args = (self.steps, self.maze.steps)
|
|
87
|
+ self.status.config(text='Resultat: %d/%d Schritte!' % args)
|
|
88
|
+
|
|
89
|
+ def get_color(self, x, y):
|
|
90
|
+ if self.maze.start_cell == (x, y):
|
|
91
|
+ return 'red'
|
|
92
|
+ if self.maze.exit_cell == (x, y):
|
|
93
|
+ return 'green'
|
|
94
|
+ if self.maze.maze[y][x] == 1:
|
|
95
|
+ return 'black'
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+class Maze(object):
|
|
99
|
+
|
|
100
|
+ def __init__(self, width=21, height=21, exit_cell=(19,1), start_cell=(1,19)):
|
|
101
|
+ self.width = width
|
|
102
|
+ self.height = height
|
|
103
|
+ self.exit_cell = exit_cell
|
|
104
|
+ self.start_cell = start_cell
|
|
105
|
+ self.create()
|
|
106
|
+
|
|
107
|
+ def create(self):
|
|
108
|
+ self.maze = [[1] * self.width for _ in range(self.height)]
|
|
109
|
+ self.steps = None
|
|
110
|
+ self.recursion_depth = None
|
|
111
|
+ self._visited_cells = []
|
|
112
|
+ self._visit_cell(self.exit_cell)
|
|
113
|
+
|
|
114
|
+ def _visit_cell(self, cell, depth=0):
|
|
115
|
+ x, y = cell
|
|
116
|
+ self.maze[y][x] = 0
|
|
117
|
+ self._visited_cells.append(cell)
|
|
118
|
+ neighbors = self._get_neighbors(cell)
|
|
119
|
+ random.shuffle(neighbors)
|
|
120
|
+ for neighbor in neighbors:
|
|
121
|
+ if not neighbor in self._visited_cells:
|
|
122
|
+ self._remove_wall(cell, neighbor)
|
|
123
|
+ self._visit_cell(neighbor, depth+1)
|
|
124
|
+ self._update_start_cell(cell, depth)
|
|
125
|
+
|
|
126
|
+ def _get_neighbors(self, cell):
|
|
127
|
+ """
|
|
128
|
+ Beispiel:
|
|
129
|
+ Die Nachbarzellen von a sind b
|
|
130
|
+ # # # # # # # # # # # # # #
|
|
131
|
+ # # # b # # # # a # b # # #
|
|
132
|
+ # # # # # # # # # # # # # #
|
|
133
|
+ # b # a # b # # b # # # # #
|
|
134
|
+ # # # # # # # # # # # # # #
|
|
135
|
+ # # # b # # # # # # # # # #
|
|
136
|
+ # # # # # # # # # # # # # #
|
|
137
|
+ """
|
|
138
|
+ x, y = cell
|
|
139
|
+ neighbors = []
|
|
140
|
+
|
|
141
|
+ # links
|
|
142
|
+ if x - 2 > 0:
|
|
143
|
+ neighbors.append((x-2, y))
|
|
144
|
+ # rechts
|
|
145
|
+ if x + 2 < self.width:
|
|
146
|
+ neighbors.append((x+2, y))
|
|
147
|
+ # hoch
|
|
148
|
+ if y - 2 > 0:
|
|
149
|
+ neighbors.append((x, y-2))
|
|
150
|
+ # runter
|
|
151
|
+ if y + 2 < self.height:
|
|
152
|
+ neighbors.append((x, y+2))
|
|
153
|
+
|
|
154
|
+ return neighbors
|
|
155
|
+
|
|
156
|
+ def _remove_wall(self, cell, neighbor):
|
|
157
|
+ """
|
|
158
|
+ Entferne die Wand zwischen den beiden Zellen
|
|
159
|
+ Beispiel:
|
|
160
|
+ Die Wand zwischen a und b ist w
|
|
161
|
+ # # # # #
|
|
162
|
+ # # # # #
|
|
163
|
+ # a w b #
|
|
164
|
+ # # # # #
|
|
165
|
+ # # # # #
|
|
166
|
+ """
|
|
167
|
+ x0, y0 = cell
|
|
168
|
+ x1, y1 = neighbor
|
|
169
|
+ # vertikal
|
|
170
|
+ if x0 == x1:
|
|
171
|
+ x = x0
|
|
172
|
+ y = (y0 + y1) / 2
|
|
173
|
+ # horizontal
|
|
174
|
+ if y0 == y1:
|
|
175
|
+ x = (x0 + x1) / 2
|
|
176
|
+ y = y0
|
|
177
|
+ self.maze[y][x] = 0
|
|
178
|
+
|
|
179
|
+ def _update_start_cell(self, cell, depth):
|
|
180
|
+ if depth > self.recursion_depth:
|
|
181
|
+ self.recursion_depth = depth
|
|
182
|
+ self.start_cell = cell
|
|
183
|
+ self.steps = depth * 2
|
|
184
|
+
|
|
185
|
+ def show(self, verbose=False):
|
|
186
|
+ MAP = {0: ' ', # Durchgang
|
|
187
|
+ 1: '#', # Wand
|
|
188
|
+ 2: 'B', # Ausgang
|
|
189
|
+ 3: 'A', # Start
|
|
190
|
+ }
|
|
191
|
+ x0, y0 = self.exit_cell
|
|
192
|
+ self.maze[y0][x0] = 2
|
|
193
|
+ x1, y1 = self.start_cell
|
|
194
|
+ self.maze[y1][x1] = 3
|
|
195
|
+ for row in self.maze:
|
|
196
|
+ print ' '.join([MAP[col] for col in row])
|
|
197
|
+ if verbose:
|
|
198
|
+ print "Steps from A to B:", self.steps
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+if __name__ == '__main__':
|
|
202
|
+
|
|
203
|
+ from optparse import OptionParser
|
|
204
|
+
|
|
205
|
+ parser = OptionParser(description="Random maze game")
|
|
206
|
+ parser.add_option('-W', '--width', type=int, default=21,
|
|
207
|
+ help="maze width (default 21)")
|
|
208
|
+ parser.add_option('-H', '--height', type=int, default=21,
|
|
209
|
+ help="maze height (default 21)")
|
|
210
|
+ parser.add_option('-s', '--size', type=int, default=10,
|
|
211
|
+ help="cell size (default 10)")
|
|
212
|
+ args, _ = parser.parse_args()
|
|
213
|
+
|
|
214
|
+ for arg in ('width', 'height'):
|
|
215
|
+ if getattr(args, arg) % 2 == 0:
|
|
216
|
+ setattr(args, arg, getattr(args, arg) + 1)
|
|
217
|
+ print "Warning: %s muss ungerade sein, benutze %d stattdessen" % \
|
|
218
|
+ (arg, getattr(args, arg))
|
|
219
|
+
|
|
220
|
+ sys.setrecursionlimit(5000)
|
|
221
|
+
|
|
222
|
+ app = Application(args.width, args.height, args.size)
|
|
223
|
+ app.master.title('Maze game')
|
|
224
|
+ app.mainloop()
|
|
225
|
+
|