Rem 0:le,1:jobbra,2:fel,3:balra}
Rem 0:lentről,1:jobbról,2:fentről,3:balról}

DIM map(1000,1000) as byte
    Global done as boolean

    Set text font "Arial"
    Set text size 7
  randomize timer()
  done=0
  map(50,50)=1
  map(49,50)=1
  kirak(49,50,2,200)

    Rem Main
    Do
     cls
     For w=0 to 80
      For v=0 to 120
       Print map(w,v)," ";
      Next v
      Print " "
     Next w
     Sync
    Loop

Function kirak(x as integer,y as integer,honnan as integer,van_meg as integer)

    Rem Locális deklarációk
    Local DIM mehet(3) as boolean
    i as integer
    uj_x as integer
    uj_y as integer
    tempx as integer
    tempy as integer
    irany as integer
    jo as boolean

Rem Begin

  Rem fillchar(mehet,sizeof(mehet),true);
  For t=0 to 3
   mehet(t)=1
  Next t

  tempx=x
  tempy=y

  Select honnan
    Case 0
        mehet(2)=0
        tempx=x-1
      endcase
    Case 1
        mehet(3)=0
        tempy=y-1
      endcase
    Case 2
        mehet(0)=0
        tempx=x+1
      endcase
    Case 3
        mehet(1)=0
        tempy=y+1
      endcase
  Endselect

  i=0
  while done=0 and (i<=2)
   Rem begin
    map(x,y)=0

    irany=RND(3)

    while mehet(irany)=0
     irany=RND(3)
    endwhile

    mehet(irany)=0
    uj_x=x
    uj_y=y

    Select irany

      Case 0
       uj_x=x+1
      endcase

      Case 1
       uj_y=y+1
      endcase

      Case 2
       uj_x=x-1
      endcase
      Case 3
       uj_y=y-1
      endcase

    endselect

    jo=1

    if map(uj_x,uj_y)=1 then jo=0
    if (map(uj_x-1,uj_y)=1) then jo=0
    if (map(uj_x,uj_y-1)=1) then jo=0
    if (map(uj_x+1,uj_y)=1) then jo=0
    if (map(uj_x,uj_y+1)=1) then jo=0

    map(tempx,tempy)=0
    if (map(uj_x+1,uj_y+1)=1) then jo=0
    if (map(uj_x+1,uj_y-1)=1) then jo=0
    if (map(uj_x-1,uj_y+1)=1) then jo=0
    if (map(uj_x-1,uj_y-1)=1) then jo=0
    map(tempx,tempy)=1

    if jo
    Rem begin
      map(uj_x,uj_y)=1

      if van_meg=0
      Rem begin
        map(x,y)=1
        done=1
      else
        map(x,y)=1
        kirak(uj_x,uj_y,irany,van_meg-1)
      endif

    endif
    inc i
  endwhile

  if done=0
  Rem begin
    map(x,y)=0
  endif

Endfunction

Remstart
procedure kiir;
var f:text;
    i,j:integer;
    temp:string[101];
begin
  assign(f,'ki.ki');
  rewrite(f);
  for i:=0 to 100 do
  begin
    temp:='';
    for j:=0 to 100 do
    begin
      if map[i,j]=1 then
      begin
        temp:=temp+'X';
      end else begin
        temp:=temp+' ';
      end;
    end;
    writeln(f,temp);
  end;
  close(f);
end;

begin
  randomize;
  done:=false;
  map[50,50]:=1;
  map[49,50]:=1;
  kirak(49,50,2,200);
  kiir;
end.
Remend
