set display mode 1024,768,32  :  backdrop off: sync rate 60
sync off
set text size 16
 
start:
enable escapekey
repeat
   cls
   print " Please set the Speed first."
   print "    0 = no images, just time and steps"
   print "    low number = fast"
   print "  high number = slow "
   input " (0 - 200) :",speed
until speed>=0 and speed<200
 
if speed=0
   sync rate 0
else
   sync rate 60
endif
 
disable escapekey
 
startzeit=timer()
 
sync on
 
type sucher
   x as integer
   y as integer
   feldzahl as integer
endtype
 
type feld
   x as integer
   y as integer
   info as integer
   war as boolean
   typ as byte
endtype
 
CPU as sucher
dim karte(32,32) as feld
dim wege(32,32) as integer
 
 
datatoimage_by_ric(1)
 
 
paste image 1,0,0
for x=0 to 31
   for y=0 to 31
      karte(x,y).x=x*8
      karte(x,y).y=y*8
      karte(x,y).typ=rgbr(point(karte(x,y).x,karte(x,y).y))/255
      karte(x,y).info=-999
      if rgbr(point(karte(x,y).x,karte(x,y).y))>250 and rgbb(point(karte(x,y).x,karte(x,y).y))<5 then karte(x,y).typ=3
      if rgbr(point(karte(x,y).x,karte(x,y).y))<5 and rgbg(point(karte(x,y).x,karte(x,y).y))>250 then karte(x,y).typ=2 : CPU.x=x : CPU.y=y
   next y
next x
 
 
do
   oldx=CPU.x
   oldy=CPU.y
   ink rgb(0,0,0),0 : box 0,0,1023,767 : ink rgb(255,255,255),0
   randomize timer()*rnd(500)/rnd(100)
   randomize timer()*rnd(500)/rnd(100)
 
   if speed<>0 then paste image 1,0,0
 
if zeit>speed
   for x=0 to 32 : for y=0 to 32 : wege(x,y)=0 : next y : next x
 
   inc karte(CPU.x,CPU.y).war
   denken:
   wegzahl=0
   `umsehen
   for x=-1 to 1
      for y=-1 to 1
         if x<>0 or y<>0
            karte(CPU.x+x,CPU.y+y).info=karte(CPU.x+x,CPU.y+y).typ
            if karte(CPU.x+x,CPU.y+y).info<>0 and karte(CPU.x+x,CPU.y+y).war=0 then wege(CPU.x+x,CPU.y+y)=1  : inc wegzahl
            if karte(CPU.x+x,CPU.y+y).info<>0 and karte(CPU.x+x,CPU.y+y).war=1 then wege(CPU.x+x,CPU.y+y)=karte(CPU.x+x,CPU.y+y).war*-1
            if karte(CPU.x+x,CPU.y+y).info=3 then wege(CPU.x+x,CPU.y+y)=2 : inc wegzahl
         endif
      next y
   next x
 
   if wegzahl>1
      weg=1
      dim wegx(wegzahl+1) as integer
      dim wegy(wegzahl+1) as integer
      for x=0 to wegzahl+1 : wegx(x)=CPU.x : wegy(x)=CPU.y : next x
      `entscheiden
      for x=0 to 31
         for y=0 to 31
            if wege(x,y)=2
               if x=CPU.x or y=CPU.y
                  CPU.x=x : CPU.y=y : goto raus
               endif
            endif
            if wege(x,y)=1 then wegx(weg)=x : wegy(weg)=y : inc weg
         next y
      next x
      n=rnd(wegzahl-1)+1
      CPU.x=wegx(n) : CPU.y=wegy(n)
      raus:
      undim wegx(0)
      undim wegy(0)
   else
      if wegzahl=1
         `entscheiden
         for x=0 to 31
            for y=0 to 31
               if wege(x,y)=1
                  if x=CPU.x or y=CPU.y
                     CPU.x=x : CPU.y=y : goto rau
                  endif
               endif
            next y
         next x
         rau:
      else
         `entscheiden
         kleinster=0
         for x=0 to 31
            for y=0 to 31
               if wege(x,y)<kleinster then kleinster=wege(x,y)
            next y
         next x
         for x=0 to 31
            for y=0 to 31
               if wege(x,y)=kleinster
                  if karte(x,y).typ=1
                     if x=CPU.x or y=CPU.y
                        CPU.x=x : CPU.y=y : goto ra
                     endif
                  endif
               endif
            next y
         next x
         ra:
      endif
   endif
 
   ds=0 : z=0
   for x=0 to 31
      for y=0 to 31
         ds = ds + karte(x,y).war
         z=z+1
      next y
   next x
   ds=ds/z
 
   if karte(CPU.x,CPU.y).war>ds+5 then for x=0 to 31 : for y=0 to 31 : karte(x,y).war=0 : karte(x,y).info=-999 : next y : next x
   zeit=0
endif
 
   if CPU.x<>oldx then inc CPU.feldzahl
   if CPU.y<>oldy then inc CPU.feldzahl
 
   if speed<>0
      `zeichne spieler
      sk=sk+rnd(2)-rnd(2)
      box karte(CPU.x,CPU.y).x,karte(CPU.x,CPU.y).y,karte(CPU.x,CPU.y).x+8,karte(CPU.x,CPU.y).y+8,rgb(0,0,200+sk),rgb(0,25+sk,200),rgb(25+sk,0,200) ,rgb(25+sk,25+sk,200+sk)
   endif
 
   if karte(CPU.x,CPU.y).typ=3 then goto gewonnen
 
   if speed=0 then set cursor 500,500 : print str$((timer()-startzeit)/1000)+" seconds elapsed." : print "Walked "+str$(CPU.feldzahl)+" steps."
 
   sync
   inc zeit
 
   if escapekey() then goto restart
loop
 
 
gewonnen:
   repeat
      cls
      sync off
      set cursor 500,500
      ink rgb(0,200,0),0
      set text size 20
      set text to bold
      print "FINISHED with "+str$(CPU.feldzahl)+" steps !"
      print "restart? (y/n)";
      input " ",frage$
   until frage$="y" or frage$="Y" or frage$="n" or frage$="N"
   if frage$="y" or frage$="Y"
      goto restart
   endif
 
cls
undim karte(0,0)
undim wege(0)
delete image 1
end
 
restart:
      frage$=""
      flush video memory
      for x=1 to 100
         if image exist(x) then delete image x
         if bitmap exist(x) then delete image x
      next x
      undim karte(0,0)
      undim wege(0,0)
      CPU.feldzahl=0
      restore
      goto start
 
cls
end
 
 
function datatoimage_by_ric(image)
 
 
  memblock=1
  read size
  make memblock 1,size
  read sx `as dword
  read sy `as dword
  read depth `as dword
  write memblock dword 1,0,sx
  write memblock dword 1,4,sy
  write memblock dword 1,8,depth
 
  for x=0 to sx-1
    for y=0 to sy-1
      pos=(y*sx+x)*4+12
      read colour
 
      if colour=0 then r=0:g=0:b=0
      if colour=1 then r=255:g=255:b=255
      if colour=4 then r=255:g=0:b=0
      if colour=3 then r=0:g=255:b=0
 
      write memblock byte 1,pos,b
      write memblock byte 1,pos+1,g
      write memblock byte 1,pos+2,r
 
    next y
  next x
make image from memblock image,1
delete memblock 1
 
create bitmap 1,32,32
create bitmap 2,256,256
set current bitmap 1
paste image image,0,0
copy bitmap 1,0,0,32,32,2,0,0,256,256
set current bitmap 2
get image image,0,0,256,256,1
delete bitmap 1
delete bitmap 2
 
endf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