Maze solving: Difference between revisions
Content added Content deleted
Line 1,101: | Line 1,101: | ||
<lang>intvars |
<lang>intvars |
||
size = 20 |
|||
n = 2 * size + 1 |
|||
endpos = n * n - n - 3 |
|||
'free = 1 |
|||
startpos = 2 * n + 2 |
|||
f# = 100 / n |
|||
'f# = 100 / 'n |
|||
'f2# = 'f# / 2 |
|||
# |
# |
||
func draw_square pos col . . |
func draw_square pos col . . |
||
x = pos mod |
x = pos mod n |
||
y = pos / |
y = pos / n |
||
color col |
color col |
||
move x * |
move x * f# y * f# |
||
rect |
rect f# * 1.05 f# * 1.05 |
||
. |
. |
||
func mark pos . . |
func mark pos . . |
||
x = pos mod |
x = pos mod n |
||
y = pos / |
y = pos / n |
||
color 900 |
color 900 |
||
move x * |
move x * f# + f# / 2 y * f# + f# / 2 |
||
circle |
circle f# / 4 |
||
. |
. |
||
len m[] |
len m[] n * n |
||
# |
# |
||
func show_maze . . |
func show_maze . . |
||
Line 1,129: | Line 1,127: | ||
rect 100 100 |
rect 100 100 |
||
for i range len m[] |
for i range len m[] |
||
if m[i] = |
if m[i] = 0 |
||
call draw_square i 777 |
call draw_square i 777 |
||
. |
. |
||
. |
. |
||
call draw_square |
call draw_square startpos 900 |
||
. |
. |
||
offs[] = [ 1 |
offs[] = [ 1 n -1 (-n) ] |
||
func visited pos . r . |
func visited pos . r . |
||
r = 0 |
r = 0 |
||
Line 1,143: | Line 1,141: | ||
. |
. |
||
func m_maze pos . . |
func m_maze pos . . |
||
m[pos] = |
m[pos] = 0 |
||
repeat |
repeat |
||
call visited pos res |
call visited pos res |
||
until res = |
until res = 0 |
||
dir = random 4 |
dir = random 4 |
||
posn = pos + 2 * offs[dir] |
posn = pos + 2 * offs[dir] |
||
if m[posn] <> |
if m[posn] <> 0 |
||
m[pos + offs[dir]] = |
m[pos + offs[dir]] = 0 |
||
call m_maze posn |
call m_maze posn |
||
. |
. |
||
Line 1,156: | Line 1,154: | ||
. |
. |
||
func make_maze . . |
func make_maze . . |
||
for i range |
for i range len m[] |
||
m[i] = |
m[i] = 1 |
||
. |
|||
⚫ | |||
for i range n |
|||
m[ |
m[i] = 0 |
||
⚫ | |||
m[n * i + n - 1] = 0 |
|||
m[n * (n - 1) + i] = 0 |
|||
. |
. |
||
call m_maze |
call m_maze startpos |
||
m[ |
m[endpos] = 0 |
||
. |
. |
||
func solve dir0 pos . found . |
func solve dir0 pos . found . |
||
call mark pos |
call mark pos |
||
sleep 0.05 |
sleep 0.05 |
||
if pos = |
if pos = endpos |
||
found = 1 |
found = 1 |
||
else |
else |
||
for dir range 4 |
for dir range 4 |
||
posn = pos + offs[dir] |
posn = pos + offs[dir] |
||
if dir <> dir0 and m[posn] = |
if dir <> dir0 and m[posn] = 0 and found = 0 |
||
call solve (dir + 2) mod 4 posn found |
call solve (dir + 2) mod 4 posn found |
||
if found = 0 |
if found = 0 |
||
Line 1,187: | Line 1,188: | ||
call show_maze |
call show_maze |
||
sleep 1 |
sleep 1 |
||
call solve -1 |
call solve -1 startpos found</lang> |
||
=={{header|EGL}}== |
=={{header|EGL}}== |