Maze solving: Difference between revisions
Content added Content deleted
Line 1,440: | Line 1,440: | ||
endpos = n * n - 2 |
endpos = n * n - 2 |
||
startpos = n + 1 |
startpos = n + 1 |
||
⚫ | |||
# |
# |
||
⚫ | |||
func draw_square pos col . . |
|||
⚫ | |||
y = pos div n |
|||
⚫ | |||
⚫ | |||
⚫ | |||
. |
|||
func mark pos . . |
|||
x = pos mod n |
|||
y = pos div n |
|||
set_color 900 |
|||
move_pen x * f + f / 2 y * f + f / 2 |
|||
draw_circle f / 4 |
|||
. |
|||
len m[] n * n |
len m[] n * n |
||
# |
# |
||
Line 1,463: | Line 1,449: | ||
for i range len m[] |
for i range len m[] |
||
if m[i] = 0 |
if m[i] = 0 |
||
x = i mod n |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
. |
. |
||
. |
. |
||
call draw_square startpos 900 |
|||
sleep 0.001 |
sleep 0.001 |
||
. |
. |
||
Line 1,476: | Line 1,465: | ||
if dir = 0 and c <> n - 2 or dir = 1 and r <> n - 2 or dir = 2 and c <> 1 or dir = 3 and r <> 1 |
if dir = 0 and c <> n - 2 or dir = 1 and r <> n - 2 or dir = 2 and c <> 1 or dir = 3 and r <> 1 |
||
posn = pos + 2 * offs[dir] |
posn = pos + 2 * offs[dir] |
||
. |
|||
. |
|||
func visited pos . res . |
|||
res = 0 |
|||
for dir range 4 |
|||
call getposn pos dir posn |
|||
if posn <> -1 |
|||
res += m[posn] |
|||
. |
|||
. |
. |
||
. |
. |
||
Line 1,490: | Line 1,470: | ||
m[pos] = 0 |
m[pos] = 0 |
||
call show_maze |
call show_maze |
||
⚫ | |||
repeat |
|||
for i = 3 downto 0 |
|||
d = random (i + 1) |
|||
dir = |
dir = d[d] |
||
⚫ | |||
call getposn pos dir posn |
call getposn pos dir posn |
||
if posn <> -1 and m[posn] <> 0 |
if posn <> -1 and m[posn] <> 0 |
||
Line 1,507: | Line 1,488: | ||
call m_maze startpos |
call m_maze startpos |
||
m[endpos] = 0 |
m[endpos] = 0 |
||
. |
|||
func solve dir0 pos . found . |
|||
call mark pos |
|||
sleep 0.05 |
|||
if pos = endpos |
|||
⚫ | |||
else |
|||
for dir range 4 |
|||
posn = pos + offs[dir] |
|||
if dir <> dir0 and m[posn] = 0 and found = 0 |
|||
call solve (dir + 2) mod 4 posn found |
|||
if found = 0 |
|||
call draw_square posn 777 |
|||
sleep 0.05 |
|||
⚫ | |||
. |
|||
. |
|||
. |
|||
. |
. |
||
# |
# |
||
call make_maze |
call make_maze |
||
call show_maze |
call show_maze</lang> |
||
sleep 5 |
|||
call solve -1 startpos found</lang> |
|||
=={{header|EGL}}== |
=={{header|EGL}}== |