4 A Programmer's Text Editor
8 Copyright (C) 1991-2009 Angel Ortega <angel@triptico.com>
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 http://www.triptico.com
28 sub mp.maze(doc, keycode)
30 local tx = mp.window.tx;
31 local ty = mp.window.ty;
34 doc = mp.open('<maze>');
36 local maze = doc.maze;
38 if (maze == NULL || maze.tx != tx || maze.ty != ty) {
39 /* new or changed: rebuild everything */
52 maze.map = map(sub { map(sub { '#'; }, [1 .. tx]); }, [1 .. ty]);
65 foreach (t, [[0, -1], [1, 0], [0, 1], [-1, 0]]) {
66 /* can space be opened? */
67 local ny = y + t[0] * 2;
68 local nx = x + t[1] * 2;
70 if (nx > 0 && ny > 0 && maze.map[ny][nx] eq '#')
75 /* more than one way? stack this position */
79 /* pick one direction at random and move there */
80 local m = d[random(size(d))];
91 /* no way from here: pop previous position */
92 if ((d = pop(stack)) == NULL)
100 maze.x = 1 + random(tx / 2) * 2;
101 maze.y = 1 + random(ty / 2) * 2;
103 maze.map[maze.y][maze.x] = '@';
105 x = 1 + random(tx / 2) * 2;
106 y = 1 + random(ty / 2) * 2;
108 maze.map[y][x] = 'X';
110 doc.keypress = sub(d, k) { mp.maze(d, k); };
112 doc.paint = sub(d) { map(sub(e) { [8, join('', e)];}, d.maze.map); };
118 maze.map[y][x] = ' ';
120 if (keycode eq 'cursor-up')
122 if (keycode eq 'cursor-down')
124 if (keycode eq 'cursor-left')
126 if (keycode eq 'cursor-right')
129 if (maze.map[y][x] eq 'X') {
130 mp.alert("You won!");
135 if (maze.map[y][x] eq ' ') {
140 maze.map[maze.y][maze.x] = '@';