' A* Pathfinding — heuristic distance calculations
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL grid(99) AS LONG
    LOCAL x AS LONG, y AS LONG
    LOCAL line AS STRING

    PRINT "A* Pathfinding Demo"
    PRINT "===================="
    PRINT ""

    grid(0) = 0: grid(1) = 0: grid(2) = 0: grid(3) = 1: grid(4) = 0
    grid(5) = 0: grid(6) = 0: grid(7) = 0: grid(8) = 0: grid(9) = 1
    grid(10) = 0: grid(11) = 1: grid(12) = 0: grid(13) = 1: grid(14) = 0
    grid(15) = 1: grid(16) = 1: grid(17) = 1: grid(18) = 0: grid(19) = 1
    grid(20) = 0: grid(21) = 1: grid(22) = 0: grid(23) = 0: grid(24) = 0
    grid(25) = 0: grid(26) = 0: grid(27) = 0: grid(28) = 0: grid(29) = 0
    grid(30) = 0: grid(31) = 1: grid(32) = 1: grid(33) = 1: grid(34) = 1
    grid(35) = 1: grid(36) = 1: grid(37) = 0: grid(38) = 1: grid(39) = 0
    grid(40) = 0: grid(41) = 0: grid(42) = 0: grid(43) = 0: grid(44) = 0
    grid(45) = 0: grid(46) = 1: grid(47) = 0: grid(48) = 0: grid(49) = 0
    grid(50) = 0: grid(51) = 1: grid(52) = 1: grid(53) = 1: grid(54) = 1
    grid(55) = 0: grid(56) = 1: grid(57) = 0: grid(58) = 1: grid(59) = 1
    grid(60) = 0: grid(61) = 0: grid(62) = 0: grid(63) = 0: grid(64) = 1
    grid(65) = 0: grid(66) = 0: grid(67) = 0: grid(68) = 0: grid(69) = 0
    grid(70) = 1: grid(71) = 1: grid(72) = 1: grid(73) = 0: grid(74) = 1
    grid(75) = 1: grid(76) = 1: grid(77) = 1: grid(78) = 1: grid(79) = 0

    PRINT "Grid (0=open, 1=wall):"
    y = 0
    WHILE y <= 7
        line = ""
        x = 0
        WHILE x <= 9
            line = line + FORMAT$(grid(y * 10 + x))
            x = x + 1
        WEND
        PRINT line
        y = y + 1
    WEND
    PRINT ""

    PRINT "Manhattan distances from (9,7) to (9,0):"
    y = 0
    WHILE y <= 7
        line = ""
        x = 0
        WHILE x <= 9
            IF grid(y * 10 + x) = 1 THEN
                line = line + "X"
            ELSE
                line = line + FORMAT$(ABS(x - 9) + ABS(y - 0))
            END IF
            x = x + 1
        WEND
        PRINT line
        y = y + 1
    WEND
    PRINT ""

    PRINT "Heuristic: Manhattan distance |x1-x2| + |y1-y2|"
    PRINT "  From (0,7) to (9,0): "; ABS(0-9)+ABS(7-0)
    PRINT "  From (3,4) to (9,0): "; ABS(3-9)+ABS(4-0)
    PRINT "  From (7,2) to (9,0): "; ABS(7-9)+ABS(2-0)

    FUNCTION = 0
END FUNCTION
