Maze solving: Difference between revisions
Content added Content deleted
(Added emacs lisp example) |
|||
Line 1,095: | Line 1,095: | ||
| . . . . . . S | | | |
| . . . . . . S | | | |
||
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+</pre> |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+</pre> |
||
=={{header|EasyLang}}== |
|||
<lang># maze |
|||
'n = 51 |
|||
# n must be odd |
|||
'visited = 1 |
|||
'free = 1 |
|||
'endpos = 'n * 'n - 'n - 3 |
|||
'startpos = 2 * 'n + 2 |
|||
# |
|||
'f# = 100 / 'n |
|||
'f2# = 'f# / 2 |
|||
func square pos . . |
|||
if pos = 'startpos |
|||
color 900 |
|||
else |
|||
color 777 |
|||
. |
|||
x = pos mod 'n |
|||
y = pos / 'n |
|||
move x * 'f# y * 'f# |
|||
rect 'f# * 1.05 'f# * 1.05 |
|||
. |
|||
func mark pos . . |
|||
x = pos mod 'n |
|||
y = pos / 'n |
|||
color 900 |
|||
move x * 'f# + 'f2# y * 'f# + 'f2# |
|||
circle 'f2# / 2 |
|||
. |
|||
len m[] 'n * 'n |
|||
# |
|||
func make_fence . . |
|||
for i range 'n |
|||
m[i] = 'visited |
|||
m['n * i] = 'visited |
|||
m['n * i + 'n - 1] = 'visited |
|||
m['n * ('n - 1) + i] = 'visited |
|||
. |
|||
. |
|||
offs[] = [ 1 'n -1 (-'n) ] |
|||
func visited pos . r . |
|||
r = 0 |
|||
for i range 4 |
|||
r += m[pos + 2 * offs[i]] |
|||
. |
|||
. |
|||
func make_maze pos . . |
|||
m[pos] = 'visited |
|||
repeat |
|||
call visited pos res |
|||
until res = 4 |
|||
dir = random 4 |
|||
posn = pos + 2 * offs[dir] |
|||
if m[posn] <> 'visited |
|||
m[pos + offs[dir]] = 'free |
|||
call make_maze posn |
|||
. |
|||
. |
|||
. |
|||
func show_maze . . |
|||
color 000 |
|||
rect 100 100 |
|||
for i range len m[] |
|||
if m[i] = 'free |
|||
call square i |
|||
. |
|||
. |
|||
. |
|||
func solve dir0 pos . found . |
|||
if pos = 'endpos |
|||
found = 1 |
|||
else |
|||
for dir range 4 |
|||
posn = pos + offs[dir] |
|||
if dir <> dir0 and m[posn] = 'free and found = 0 |
|||
call solve (dir + 2) mod 4 posn found |
|||
if found = 1 |
|||
call mark posn |
|||
. |
|||
. |
|||
. |
|||
. |
|||
. |
|||
call make_fence |
|||
call make_maze 'startpos |
|||
m['endpos] = 'free |
|||
call show_maze |
|||
sleep 0.5 |
|||
found = 0 |
|||
call solve -1 'startpos found |
|||
</lang> |
|||
=={{header|EGL}}== |
=={{header|EGL}}== |