Maze solving: Difference between revisions

Content added Content deleted
Line 1,101: Line 1,101:


<lang>intvars
<lang>intvars
'size = 25
size = 20
'n = 2 * 'size + 1
n = 2 * size + 1
endpos = n * n - n - 3
'free = 1
'endpos = 'n * 'n - 'n - 3
startpos = 2 * n + 2
'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 'n
x = pos mod n
y = pos / 'n
y = pos / n
color col
color col
move x * 'f# y * 'f#
move x * f# y * f#
rect 'f# * 1.05 'f# * 1.05
rect f# * 1.05 f# * 1.05
.
.
func mark pos . .
func mark pos . .
x = pos mod 'n
x = pos mod n
y = pos / 'n
y = pos / n
color 900
color 900
move x * 'f# + 'f2# y * 'f# + 'f2#
move x * f# + f# / 2 y * f# + f# / 2
circle 'f2# / 2
circle f# / 4
.
.
len m[] 'n * 'n
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] = 'free
if m[i] = 0
call draw_square i 777
call draw_square i 777
.
.
.
.
call draw_square 'startpos 900
call draw_square startpos 900
.
.
offs[] = [ 1 'n -1 (-'n) ]
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] = 'free
m[pos] = 0
repeat
repeat
call visited pos res
call visited pos res
until res = 4
until res = 0
dir = random 4
dir = random 4
posn = pos + 2 * offs[dir]
posn = pos + 2 * offs[dir]
if m[posn] <> 'free
if m[posn] <> 0
m[pos + offs[dir]] = 'free
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 'n
for i range len m[]
m[i] = 'free
m[i] = 1
.
m['n * i] = 'free
m['n * i + 'n - 1] = 'free
for i range n
m['n * ('n - 1) + i] = 'free
m[i] = 0
m[n * i] = 0
m[n * i + n - 1] = 0
m[n * (n - 1) + i] = 0
.
.
call m_maze 'startpos
call m_maze startpos
m['endpos] = 'free
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 = 'endpos
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] = 'free and found = 0
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 'startpos found</lang>
call solve -1 startpos found</lang>


=={{header|EGL}}==
=={{header|EGL}}==