Maze solving: Difference between revisions
Content added Content deleted
Line 1,100: | Line 1,100: | ||
[https://easylang.online/apps/_r_maze.html Run it] |
[https://easylang.online/apps/_r_maze.html Run it] |
||
<lang># maze |
<lang># maze generation and solving |
||
⚫ | |||
'n = 51 |
|||
'size = 25 |
|||
# n must be odd |
|||
' |
'n = 2 * 'size + 1 |
||
'free = 1 |
'free = 1 |
||
'endpos = 'n * 'n - 'n - 3 |
'endpos = 'n * 'n - 'n - 3 |
||
'startpos = 2 * 'n + 2 |
'startpos = 2 * 'n + 2 |
||
⚫ | |||
'f# = 100 / 'n |
'f# = 100 / 'n |
||
'f2# = 'f# / 2 |
'f2# = 'f# / 2 |
||
# |
|||
func draw_square pos . . |
func draw_square pos col . . |
||
x = pos mod 'n |
x = pos mod 'n |
||
y = pos / 'n |
y = pos / 'n |
||
⚫ | |||
move x * 'f# y * 'f# |
move x * 'f# y * 'f# |
||
rect 'f# * 1.05 'f# * 1.05 |
rect 'f# * 1.05 'f# * 1.05 |
||
Line 1,128: | Line 1,129: | ||
color 000 |
color 000 |
||
rect 100 100 |
rect 100 100 |
||
⚫ | |||
for i range len m[] |
for i range len m[] |
||
if m[i] = 'free |
if m[i] = 'free |
||
call draw_square i |
call draw_square i 777 |
||
. |
. |
||
. |
. |
||
⚫ | |||
color 900 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
. |
. |
||
offs[] = [ 1 'n -1 (-'n) ] |
offs[] = [ 1 'n -1 (-'n) ] |
||
Line 1,152: | Line 1,143: | ||
. |
. |
||
. |
. |
||
func |
func m_maze pos . . |
||
m[pos] = ' |
m[pos] = 'free |
||
repeat |
repeat |
||
call visited pos res |
call visited pos res |
||
Line 1,159: | Line 1,150: | ||
dir = random 4 |
dir = random 4 |
||
posn = pos + 2 * offs[dir] |
posn = pos + 2 * offs[dir] |
||
if m[posn] <> ' |
if m[posn] <> 'free |
||
m[pos + offs[dir]] = 'free |
m[pos + offs[dir]] = 'free |
||
call |
call m_maze posn |
||
. |
. |
||
. |
. |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
call m_maze 'startpos |
|||
⚫ | |||
. |
. |
||
func solve dir0 pos . found . |
func solve dir0 pos . found . |
||
call mark pos |
call mark pos |
||
sleep 0. |
sleep 0.05 |
||
if pos = 'endpos |
if pos = 'endpos |
||
⚫ | |||
call show_maze |
|||
found = 1 |
found = 1 |
||
else |
else |
||
Line 1,177: | Line 1,176: | ||
if dir <> dir0 and m[posn] = 'free and found = 0 |
if dir <> dir0 and m[posn] = 'free and found = 0 |
||
call solve (dir + 2) mod 4 posn found |
call solve (dir + 2) mod 4 posn found |
||
if found = |
if found = 0 |
||
call |
call draw_square posn 777 |
||
⚫ | |||
. |
. |
||
. |
. |
||
Line 1,184: | Line 1,184: | ||
. |
. |
||
. |
. |
||
# |
|||
call make_outside |
|||
call make_maze |
call make_maze |
||
⚫ | |||
call show_maze |
call show_maze |
||
sleep |
sleep 1 |
||
found = 0 |
|||
call solve -1 'startpos found |
call solve -1 'startpos found |
||
</lang> |
</lang> |