program  worm2;
{
  Program: RandomWalk
  Author:  (C) GPL Mohsin Ahmed
  Date:    21/Feb/1988
}

        const   queuesize  =   120 ;
                worm_len   =   100  ;
        type    queueobj   =  record   x,y : integer;
                              end;
{-----------------------------------------------------------}
{ file queue1.pas maintains a queue of size = queuesize     }
{ of predefined type queueobj.       07 Nov 1986            }
var   queue       :   array [0..queuesize] of queueobj;
      front,back  :   0..queuesize;
{-----------------------------------------------------------}
procedure addqueue(k:queueobj);
        var i  :   0..queuesize;
        begin
        i:=(front+1)mod (queuesize+1);
        front:=i;
        queue[front]:=k;
        end;
{-----------------------------------------------------------}
procedure delqueue(var k:queueobj);
         begin
         back:=(back+1)mod(queuesize+1);
         k:=queue[back];
         end;
{-----------------------------------------------------------}
type    NoteRecord = record
                  C,CF,D,DF,E,F,FF,G,GF,A,AF,B: integer;
        end;
const   Notes: NoteRecord =
          (C:1;CF:2;D:3;DF:4;E:5;F:6;FF:7;G:8;GF:9;A:10;AF:11;B:12);
procedure Play(Octave,Note,Duration: integer);
       var     Hz : real;
               I  : integer;
  begin
  Hz := 32.625;
  for I := 1 to Octave do                { Compute C in Octave             }
        Hz := Hz * 2;
  for I := 1 to Note - 1 do              { Increase frequency Note-1 times }
        Hz := Hz * 1.059463094;
  if Duration <> 0 then
        begin
        Sound(Round(Hz));
        Delay(Duration);
        NoSound;
        end
  else
       Sound(Round(Hz));
  end;
{-----------------------------------------------------------}

var     i,pdi,pdj         : integer;
        lineone,linetwo   : queueobj;
begin
      randomize;
      front:=0;   { Initialize Queue }
      back:=0;
      textmode(3);
      pdi:=1;         { pdi : preferred direction +i,-i }
      pdj:=1;         { pdj : preferred direction +j,-j }
      with lineone do
             begin
             x := 5 ; y := 75 ;  { start point: top right corner }
             end ;
      for i := 1 to worm_len do
            addqueue(lineone);
      repeat
            delay(20);
            with lineone do
                 begin
                 gotoxy(y,x); write('²');
                 x := x+pdi;
                 if x > 22 then
                      pdi := -1
                 else
                      if x < 3 then
                            pdi := 1 ;
                 y:=y+pdj;
                 if y > 75 then
                      pdj := -1
                 else
                      if y < 5 then
                            pdj := 1 ;
                 end;
            addqueue(lineone);
            delqueue(linetwo);
            with linetwo do
                 begin
                 gotoxy(y,x) ; write(' ');
                 end ;
            case random(10) of
                    1 : pdi := random(3) - 1 ;
                    2 : pdj := random(3) - 1 ;
                    3 : case random(10) of
                           1       : textcolor( random(15) ) ;
                           2 .. 10 : Play(4,random(12),70);
                        end ;
                    end;
            until keypressed  ;
      for i := 1 to worm_len do
          begin
          delqueue(linetwo);
          with linetwo do
              begin
              gotoxy(y,x) ; write(' ');
              end ;
          end ;
      textmode ;
end.


