'QB64 X 64 version 1.2 20180228/86 from git b301f92
'started 2018-08-11 when Colbalt asked about A* pathfinder
' He has now 2018-08-12 posted a QB64 version that is nice!
' 2018-08-11 started PathFinder 1
' 2018-08-12 almost working but buggy backtract to point A after point B is found.
' 2018-08-13 PathFinder Paint mod
' Mod and test first part of buggy PathFinder 1 as a custom PAINT program.
' This will Paint until point B has been found,
' one could put point B outside the ww, wh range of screen
' which is where the paint would stop with this code.
CONST maxx
= 16 'image width CONST maxy
= 12 'image height
'not using
DIM SHARED ax
, ay
, bx
, by
'start x, y and end x, y of path
'board can be all or part of points from an image
'create a random board / image
board(x, y) = " "
'with these obstacles there is no guarantee a path will exist
'obstacles could be color values, dpending on how you DIM board
FOR i
= 1 TO maxx
* maxy
* .7 ox = rand(1, maxx): oy = rand(1, maxy)
ox = rand(1, maxx): oy = rand(1, maxy)
board(ox, oy) = "O"
ax = rand(1, maxx): ay = rand(1, maxy)
bx = rand(1, maxx): by = rand(1, maxy)
bx = rand(1, maxx): by = rand(1, maxy)
board(ax, ay) = "A"
board(bx, by) = "B"
displayB
_TITLE "Painting from Blue to Red until it is reached, press esc to quit... spacebar to continue..."
'display board and allow user to see blue start square and red stop square
parentF = 1: tick = 0: parentx = 0
parentF = 0: tick = tick + 1
'IF tick > maxx * maxy THEN EXIT WHILE 'this was crude infinite loop stopper
ystart = max(ay - tick, 1): ystop = min(ay + tick, maxy)
xstart = max(ax - tick, 1): xstop = min(ax + tick, maxx)
'PRINT ystart, ystop, xstart, xstop
'END
'check out the neighbors
IF x
- 1 >= 1 THEN xxstart
= x
- 1 ELSE xxstart
= x
IF x
+ 1 <= maxx
THEN xxstop
= x
+ 1 ELSE xxstop
= x
IF y
- 1 >= 1 THEN yystart
= y
- 1 ELSE yystart
= y
IF y
+ 1 <= maxy
THEN yystop
= y
+ 1 ELSE yystop
= y
IF RTRIM$(board
(x
, y
)) = "" THEN 'unpainted spot, you can set any color rannge condition here changes$ = ""
FOR yy
= yystart
TO yystop
FOR xx
= xxstart
TO xxstop
'connection to a previous painted point or start A point?
'paint this point or rec
frec
(x
- 1) * sq
+ 5, (y
- 1) * sq
+ 5, x
* sq
- 5, y
* sq
- 5, _RGB32(60 + tick
* 15, 60 + tick
* 15, 60 + tick
* 15) parentF = 1 'so will continue looping
'do we have a condition to quit painting, otherwise will quit when screen is full or no new parent is assigned
FOR yy
= yystart
TO yystop
FOR xx
= xxstart
TO xxstop
parentx = x: parenty = y 'from this we should be able to backtrack to A
'_DISPLAY
rgb 999
LOCATE 10, 15:
PRINT "No connection from A to B, press esc to quit... spacebar to continue..."
'CASE ELSE: k = 30
frec (x - 1) * sq + 5, (y - 1) * sq + 5, x * sq - 5, y * sq - 5, k&
SUB rec
(x1
, y1
, x2
, y2
, rgbN&
) LINE (x1
, y1
)-(x2
, y2
), rgbN&
, B
SUB frec
(x1
, y1
, w
, h
, rgbN&
) LINE (x1
, y1
)-(w
, h
), rgbN&
, BF
SUB rgb
(n
) ' New (even less typing!) New Color System 1000 colors with up to 3 digits rand%
= INT(RND * (hi%
- lo%
+ 1)) + lo%
posOf
= INSTR(source$
, of$
) IF posOf
> 0 THEN leftOf$
= MID$(source$
, 1, posOf
- 1) posOf
= INSTR(source$
, of$
)