Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My own try at A*
#89
mine worked fine for me. it should only look like it's going through walls if you're on windows 98 or 95 Tongue and besides, the point behind mine was to be able to make it into a function, therefore what it looks like is irrelevant.

btw, here's my new code, modified with constants and reads from a file, if you're too lazy to hack it yourself:

Code:
#include once "crt.bi"
#define WALL 4
#define FINISH 5
#define START 6

option explicit
option escape

type coord
    x as ubyte
    y as ubyte
end type
type node
    x as ubyte
    y as ubyte
    g as integer
    f as integer
    p as node ptr
end type

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
declare function onclosedlist( byref what as coord ) as byte
declare function removefromopenlist( byval idx as integer ) as byte
declare function onopenlist( byref what as coord ) as byte
declare sub _read( byref var as integer )

redim preserve shared openlist( 0 ) as node ptr, closedlist( 1 ) as coord, allnodes( 0 ) as node ptr
dim as integer rx, ry, cx, cy, ex, ey, i
dim as string yn

input "show path? [y/n]:", yn
yn = ucase$( left$( yn, 1 ) )

cls
dim shared as integer mx, my
open "board.brd" for binary as #1
_read mx
_read my
dim as integer room( mx * my )
for ry = 0 to my - 1
    for rx = 0 to mx - 1
        _read room( rx + ry * mx )
        select case room( rx + ry * mx )
        case WALL
            ? "|";
        case START
            ? "@";
            cx = rx
            cy = ry
            closedlist( 0 ).x = rx
            closedlist( 0 ).y = ry
        case FINISH
            ? "*";
            ex = rx
            ey = ry
        case else
            ? " ";
        end select
    next
    ?
next

if yn = "Y" then sleep

dim as node ptr tmpnode
tmpnode = callocate( len( node ) )
tmpnode->x = cx
tmpnode->y = cy
allnodes( ubound( allnodes ) ) = tmpnode
redim preserve shared allnodes( ubound( allnodes ) + 1 )
while cx <> ex or cy <> ey
    
    dim as integer max = 100, aye, x, y
    
    aye = 0
    
    for x = -1 to 1
        for y = -1 to 1 step 1 - ( x = 0 )
            
            dim as coord working
            working.x = cx + x
            working.y = cy + y
            
            if onclosedlist( working ) = 0 and onopenlist( working ) = 0 and _
               room( cx + x + ( cy + y ) * mx ) <> WALL and _
               room( cx + ( cy + y ) * mx ) <> WALL and _
               room( cx + x + cy * mx ) <> WALL and _
               outofbounds( cx + x, cy + y ) = 0 then
                
                openlist( ubound( openlist ) ) = callocate( len( node ) )
                dim as node ptr temp
                temp = openlist( ubound( openlist ) )  '' use a temp var so we don't have to
                                                       '' keep using ubound()
                temp->x = working.x
                temp->y = working.y
                
                temp->f = 10 * sqr( abs( x ) + abs( y ) ) + _                   '' the g score
                          10 * sqr( ( cx + x - ex ) ^ 2 + ( cy + y - ey ) ^ 2 ) '' the h score
                if temp->f > max then max = temp->f
                temp->p = tmpnode
                
                allnodes( ubound( allnodes ) ) = temp
                redim preserve shared allnodes( ubound( allnodes ) + 1 )
                
                redim preserve shared openlist( ubound( openlist ) + 1 )
                
            end if
            
        next
    next
    
    dim as integer lowest, lowestidx = 0
    lowest = max + 1
    for i = 0 to ubound( openlist ) - 1
        if openlist( i )->f < lowest and openlist( i )->f <> -1 then
            lowest = openlist( i )->f
            lowestidx = i
        end if
    next
    
    if yn = "Y" then locate cy + 1, cx + 1: ? " "
    
    if lowest <> max + 1 then
        cx = openlist( lowestidx )->x
        cy = openlist( lowestidx )->y
    end if
    
    if yn = "Y" then locate cy + 1, cx + 1: ? "@"
    
    closedlist( ubound( closedlist ) ).x = cx
    closedlist( ubound( closedlist ) ).y = cy
    redim preserve shared closedlist( ubound( closedlist ) + 1 )
    
    tmpnode = openlist( lowestidx )
    removefromopenlist( lowestidx )
    
    if yn = "Y" then sleep
    
wend

'' as a function this would return tmpnode

while tmpnode <> 0
    locate tmpnode->y + 1, tmpnode->x + 1
    ? "*"
    tmpnode = tmpnode->p
wend

'' it will be necessary to clean up memory afterward, otherwise we'll end up with
'' a leak the size of the pacific ocean ;P

for i = 0 to ubound( allnodes )
    
    deallocate allnodes( i )
    
next

sleep

end

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

function onclosedlist( byref what as coord ) as byte
    
    dim as integer i
    for i = 0 to ubound( closedlist ) - 1
        if memcmp( @closedlist( i ), @what, len( coord ) ) = 0 then return -1
    next
    return 0
    
end function

function removefromopenlist( byval idx as integer ) as byte
    
    if idx > ubound( openlist ) then return 0
    dim as integer i
    for i = idx to ubound( openlist ) - 1
        openlist( i ) = openlist( i + 1 )
    next
    redim preserve openlist( ubound( openlist ) - 1 )
    
    return -1
    
end function

function onopenlist( byref what as coord ) as byte
    
    dim as integer i
    for i = 0 to ubound( openlist ) - 1
        if memcmp( openlist( i ), @what, len( coord ) ) = 0 then return -1
    next
    return 0
    
end function

sub _read( byref var as integer )
    
    dim as ubyte char
    dim as string thing
    
    get #1, , char
    while char <> asc( "," ) and char <> asc( "\n" ) and eof( 1 ) = 0
        thing += chr$( char )
        get #1, , char
    wend
    
    var = val( thing )
    
end sub
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
Reply


Messages In This Thread
My own try at A* - by Torahteen - 06-28-2005, 07:55 AM
My own try at A* - by rpgfan3233 - 06-28-2005, 08:02 AM
My own try at A* - by Torahteen - 06-28-2005, 08:43 AM
My own try at A* - by Torahteen - 06-28-2005, 07:40 PM
My own try at A* - by Torahteen - 06-29-2005, 04:18 AM
My own try at A* - by Torahteen - 06-29-2005, 04:20 AM
My own try at A* - by Neo - 06-29-2005, 04:22 AM
My own try at A* - by Torahteen - 06-29-2005, 05:56 AM
My own try at A* - by TheBlueKeyboard - 06-29-2005, 06:14 PM
My own try at A* - by whitetiger0990 - 06-29-2005, 07:01 PM
My own try at A* - by Torahteen - 06-29-2005, 11:18 PM
My own try at A* - by Torahteen - 06-29-2005, 11:22 PM
My own try at A* - by DrV - 06-29-2005, 11:24 PM
My own try at A* - by Torahteen - 06-29-2005, 11:32 PM
My own try at A* - by DrV - 06-29-2005, 11:44 PM
My own try at A* - by Torahteen - 06-30-2005, 12:01 AM
My own try at A* - by DrV - 06-30-2005, 12:14 AM
My own try at A* - by Torahteen - 06-30-2005, 12:30 AM
My own try at A* - by DrV - 06-30-2005, 01:00 AM
My own try at A* - by Torahteen - 06-30-2005, 01:16 AM
My own try at A* - by DrV - 06-30-2005, 01:18 AM
My own try at A* - by Torahteen - 06-30-2005, 01:24 AM
My own try at A* - by DrV - 06-30-2005, 01:31 AM
My own try at A* - by rpgfan3233 - 06-30-2005, 02:28 AM
My own try at A* - by Torahteen - 06-30-2005, 02:53 AM
My own try at A* - by rpgfan3233 - 06-30-2005, 03:14 AM
My own try at A* - by Torahteen - 06-30-2005, 04:08 AM
My own try at A* - by Torahteen - 06-30-2005, 04:19 AM
My own try at A* - by Torahteen - 06-30-2005, 04:39 AM
My own try at A* - by Torahteen - 06-30-2005, 04:51 AM
My own try at A* - by dumbledore - 06-30-2005, 11:18 AM
My own try at A* - by rpgfan3233 - 06-30-2005, 11:41 AM
My own try at A* - by Torahteen - 06-30-2005, 07:29 PM
My own try at A* - by Torahteen - 06-30-2005, 07:39 PM
My own try at A* - by Torahteen - 06-30-2005, 08:23 PM
My own try at A* - by dumbledore - 06-30-2005, 11:46 PM
My own try at A* - by dumbledore - 07-01-2005, 01:13 AM
My own try at A* - by Torahteen - 07-01-2005, 01:15 AM
My own try at A* - by rpgfan3233 - 07-01-2005, 01:54 AM
My own try at A* - by Torahteen - 07-01-2005, 02:22 AM
My own try at A* - by Torahteen - 07-01-2005, 02:30 AM
My own try at A* - by dumbledore - 07-01-2005, 02:53 AM
My own try at A* - by Torahteen - 07-01-2005, 03:05 AM
My own try at A* - by rpgfan3233 - 07-01-2005, 03:12 AM
My own try at A* - by Torahteen - 07-01-2005, 03:59 AM
My own try at A* - by Neo - 07-01-2005, 03:59 AM
My own try at A* - by Torahteen - 07-01-2005, 04:17 AM
My own try at A* - by Torahteen - 07-01-2005, 09:19 PM
My own try at A* - by dumbledore - 07-02-2005, 01:07 AM
My own try at A* - by Torahteen - 07-02-2005, 01:21 AM
My own try at A* - by rpgfan3233 - 07-02-2005, 02:03 AM
My own try at A* - by dumbledore - 07-02-2005, 02:11 AM
My own try at A* - by dumbledore - 07-02-2005, 02:23 AM
My own try at A* - by Torahteen - 07-02-2005, 02:38 AM
My own try at A* - by dumbledore - 07-02-2005, 03:06 AM
My own try at A* - by dumbledore - 07-02-2005, 03:47 AM
My own try at A* - by Torahteen - 07-02-2005, 04:18 AM
My own try at A* - by rpgfan3233 - 07-02-2005, 04:25 AM
My own try at A* - by dumbledore - 07-02-2005, 04:33 AM
My own try at A* - by rpgfan3233 - 07-02-2005, 04:45 AM
My own try at A* - by Torahteen - 07-02-2005, 04:45 AM
My own try at A* - by rpgfan3233 - 07-02-2005, 04:46 AM
My own try at A* - by Dr_Davenstein - 07-02-2005, 05:05 AM
My own try at A* - by dumbledore - 07-02-2005, 06:31 AM
My own try at A* - by dumbledore - 07-02-2005, 06:42 AM
My own try at A* - by rpgfan3233 - 07-02-2005, 06:43 AM
My own try at A* - by dumbledore - 07-02-2005, 06:46 AM
My own try at A* - by rpgfan3233 - 07-02-2005, 06:49 AM
My own try at A* - by dumbledore - 07-02-2005, 06:58 AM
My own try at A* - by rpgfan3233 - 07-02-2005, 08:58 AM
My own try at A* - by Torahteen - 07-02-2005, 09:40 AM
My own try at A* - by rpgfan3233 - 07-03-2005, 02:09 AM
My own try at A* - by Torahteen - 07-03-2005, 02:25 AM
My own try at A* - by dumbledore - 07-03-2005, 04:44 AM
My own try at A* - by rpgfan3233 - 07-03-2005, 08:13 AM
My own try at A* - by dumbledore - 07-03-2005, 08:54 AM
My own try at A* - by Dr_Davenstein - 07-03-2005, 09:03 AM
My own try at A* - by Torahteen - 07-03-2005, 09:08 AM
My own try at A* - by rpgfan3233 - 07-03-2005, 09:36 AM
My own try at A* - by Torahteen - 07-03-2005, 09:43 AM
My own try at A* - by Dr_Davenstein - 07-03-2005, 10:23 AM
My own try at A* - by rpgfan3233 - 07-03-2005, 10:25 AM
My own try at A* - by rpgfan3233 - 07-03-2005, 10:30 AM
My own try at A* - by Dr_Davenstein - 07-03-2005, 10:42 AM
My own try at A* - by dumbledore - 07-03-2005, 11:29 AM
My own try at A* - by rpgfan3233 - 07-04-2005, 01:33 AM
My own try at A* - by Torahteen - 07-04-2005, 04:38 AM
My own try at A* - by dumbledore - 07-04-2005, 06:44 AM
My own try at A* - by dumbledore - 07-04-2005, 06:47 AM
My own try at A* - by rpgfan3233 - 07-04-2005, 08:58 AM
My own try at A* - by Torahteen - 07-05-2005, 06:22 AM
My own try at A* - by Torahteen - 07-05-2005, 06:36 PM
My own try at A* - by rpgfan3233 - 07-05-2005, 10:43 PM
My own try at A* - by Torahteen - 07-06-2005, 07:40 AM
My own try at A* - by dumbledore - 07-06-2005, 10:56 AM
My own try at A* - by rpgfan3233 - 07-06-2005, 11:19 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)