06-30-2005, 11:18 AM
mua ha ha ha.... yours is really complicated. here's my modified a* algo, from the description i read the normal a* shouldn't be able to find its way through what i gave it without modification like i did...
[edit] fixed a couple of dimensioning problems, should be good with any map now.
Code:
declare function timesvisited( byval x as integer, byval y as integer ) as byte
declare function outofbounds( byval x as integer, byval y as integer ) as byte
cls
type coord
x as ubyte
y as ubyte
end type
redim preserve shared badnodes( 0 ) as coord
dim as integer working( 7 )
dim as integer x, y, cx, cy, ex, ey, i
dim shared as integer mx, my
read mx
read my
dim as ubyte room( mx * my )
for y = 0 to my - 1
for x = 0 to mx - 1
read room( x + y * mx )
select case room( x + y * mx )
case 1
? "|";
case 2
? "@";
cx = x
cy = y
case 3
? "*";
ex = x
ey = y
case else
? " ";
end select
next
?
next
while cx <> ex or cy <> ey
dim as integer max = 100
working( 0 ) = 10 + sqr( ( cx - ex ) ^ 2 + ( cy - 1 - ey ) ^ 2 ) * 10 + timesvisited( cx, cy - 1 )
if room( cx + ( cy - 1 ) * mx ) = 1 or outofbounds( cx, cy - 1 ) then working( 0 ) = -1
max = working( 0 )
working( 1 ) = 14 + sqr( ( cx + 1 - ex ) ^ 2 + ( cy - 1 - ey ) ^ 2 ) * 10 + timesvisited( cx + 1, cy - 1 )
if room( cx + 1 + ( cy - 1 ) * mx ) = 1 or room( cx + ( cy - 1 ) * mx ) = 1 or room( cx + 1 + cy * mx ) = 1 or outofbounds( cx + 1, cy - 1 ) then working( 1 ) = -1
if working( 1 ) > max then max = working( 1 )
working( 2 ) = 10 + sqr( ( cx + 1 - ex ) ^ 2 + ( cy - ey ) ^ 2 ) * 10 + timesvisited( cx + 1, cy )
if room( cx + 1 + cy * mx ) = 1 or outofbounds( cx + 1, cy ) then working( 2 ) = -1
if working( 2 ) > max then max = working( 2 )
working( 3 ) = 14 + sqr( ( cx + 1 - ex ) ^ 2 + ( cy + 1 - ey ) ^ 2 ) * 10 + timesvisited( cx + 1, cy + 1 )
if room( cx + 1 + ( cy + 1 ) * mx ) = 1 or room( cx + ( cy + 1 ) * mx ) = 1 or room( cx + 1 + cy * mx ) = 1 or outofbounds( cx + 1, cy + 1 ) then working( 3 ) = -1
if working( 3 ) > max then max = working( 3 )
working( 4 ) = 10 + sqr( ( cx - ex ) ^ 2 + ( cy + 1 - ey ) ^ 2 ) * 10 + timesvisited( cx, cy + 1 )
if room( cx + ( cy + 1 ) * mx ) = 1 or outofbounds( cx, cy + 1 ) then working( 4 ) = -1
if working( 4 ) > max then max = working( 4 )
working( 5 ) = 14 + sqr( ( cx - 1 - ex ) ^ 2 + ( cy + 1 - ey ) ^ 2 ) * 10 + timesvisited( cx - 1, cy + 1 )
if room( cx - 1 + ( cy + 1 ) * mx ) = 1 or room( cx + ( cy + 1 ) * mx ) = 1 or room( cx - 1 + cy * mx ) = 1 or outofbounds( cx - 1, cy + 1 ) then working( 5 ) = -1
if working( 5 ) > max then max = working( 5 )
working( 6 ) = 10 + sqr( ( cx - 1 - ex ) ^ 2 + ( cy - ey ) ^ 2 ) * 10 + timesvisited( cx - 1, cy )
if room( cx - 1 + cy * mx ) = 1 or outofbounds( cx - 1, cy ) then working( 6 ) = -1
if working( 6 ) > max then max = working( 6 )
working( 7 ) = 14 + sqr( ( cx - 1 - ex ) ^ 2 + ( cy - 1 - ey ) ^ 2 ) * 10 + timesvisited( cx - 1, cy - 1 )
if room( cx - 1 + ( cy - 1 ) * mx ) = 1 or room( cx + ( cy - 1 ) * mx ) = 1 or room( cx - 1 + cy * mx ) = 1 or outofbounds( cx - 1, cy - 1 ) then working( 7 ) = -1
if working( 7 ) > max then max = working( 7 )
dim as integer lowest, lowestidx = 0
lowest = max + 1
for i = 0 to 7
if working( i ) < lowest and working( i ) <> -1 then lowest = working( i ) : lowestidx = i
next
locate cy + 1, cx + 1: ? " "
if lowest <> max + 1 then
select case lowestidx
case 0
cy -= 1
case 1
cx += 1
cy -= 1
case 2
cx += 1
case 3
cx += 1
cy += 1
case 4
cy += 1
case 5
cx -= 1
cy += 1
case 6
cx -= 1
case 7
cx -= 1
cy -= 1
case else
? "error":sleep:end
end select
end if
locate cy + 1, cx + 1: ? "!"
badnodes( ubound( badnodes ) ).x = cx
badnodes( ubound( badnodes ) ).y = cy
redim preserve shared badnodes( ubound( badnodes ) + 1 )
if cx = ex and cy = ey then locate 9,9: ? "done!"
sleep
wend
data 8,5
data 0,0,0,0,0,0,0,1
data 0,0,0,1,1,1,1,1
data 0,2,0,1,1,0,3,0
data 0,0,0,1,1,0,0,0
data 0,0,0,0,0,0,0,0
function timesvisited( byval x as integer, byval y as integer ) as byte
dim as integer i, num = 0
for i = 0 to ubound( badnodes ) - 1
if badnodes( i ).x = x and badnodes( i ).y = y then num += 1
next
return num * 10
end function
function outofbounds( byval x as integer, byval y as integer ) as byte
return x < 0 or x > mx - 1 or y < 0 or y > my - 1
end function
[edit] fixed a couple of dimensioning problems, should be good with any map now.
ttp://m0n573r.afraid.org/
Quote:quote: "<+whtiger> you... you don't know which way the earth spins?" ... see... stupidity leads to reverence, reverence to shakiness, shakiness to... the dark side...phear