program Ghost_Guzzler;

(* Ported to mainstream TP3 by CP/M User - 9th May 2008
   Original BASIC game written by Colin Reynolds, published
   by Usbourne Publishing in 1983 in Creepy Computer Games. *)

(* To play this game simply compile it into Memory or as a COM
   file from TP3, the concept of the game is to Guzzle up as 
   many Ghosts as possible. Each Ghost is represented by a number
   between 0 and 9 (selected randomly of course!) and to Guzzle
   them up you need set your Guzzler to the correct number and
   Guzzle them up - to do this 'M' or 'm' to selects the desired
   number and 'X' or 'x' to Guzzle them up! A word of warning if
   your compiling this to a COM file this game starts up 
   imediately!! :-D Good Luck - oh and see how far you can go -
   each ghost you score off will be the amounts of points your
   score will acculate by with the exception of 0 which will give
   you 10 points! *)

(* Notes: While I have tried to make this simple game play close
          to the original BASIC version, one change I did 
          improve on was movement of the ghosts which are 
          represented by a number. 
          I've test this game on an Amstrad CPC6128 emulator 
          which typically runs at 4Mhz, this program uses a 
          delay routine to set the speed of the game I've 
          generally have set it to 100, though the original game
          appears to have used a delay of 50 for faster computers
          so perhaps it's better like that, it was indeed 
          suggested by Usbourne that alterning the delay speed 
          is a good way of setting a good difficulty level. *)

var gotit : boolean;
    oldghostpos : byte;

function getghostvalue : byte;
begin
 getghostvalue:=trunc(random(10));
end;

procedure viewlives(numlives : byte);
var loop : byte;
begin
 for loop:=1 to numlives do
  begin
   gotoxy(loop,24);
   write('/');
  end;
 if numlives=2 then
  begin
   gotoxy(3,24);
   write(' ');
  end;
 if numlives=1 then
  begin
   gotoxy(2,24);
   write(' ');
  end;
end;

procedure updatescore(score : integer);
begin
 gotoxy(10,24);
 write('Score: ');
 write(score);
end;

procedure printposition(whereisghost, ghostvalue, myvalue : byte);
var oldposition : byte;
begin
 oldghostpos:=whereisghost;
 if (whereisghost<>1) then
  begin
   oldposition:=whereisghost-1;
   gotoxy(oldposition,10);
   write(' ');
  end;
 if (whereisghost=1) then
  begin
   oldposition:=24;
   gotoxy(oldposition,10);
   write(' ');
  end;
 gotoxy(whereisghost,10);
 write(ghostvalue);
 gotoxy(25,10);
 write(':');
 write(myvalue);
end;

procedure checkresult(goodvalue, badvalue : byte);
 var clresult:byte;
begin
 if goodvalue=badvalue then
  begin
   gotoxy(40,24);
   write('GOT IT');
   gotoxy(40,25);
   write('******');
   gotit:=true;
   { mydelay; }
   delay(100);
   gotoxy(oldghostpos,10);
   write(' ');
   for clresult:=0 to 1 do
    begin
     gotoxy(40,clresult+24);
     write('      ');
    end;
  end;
end;

var score, myvalue, lives, distance {i}, ghostvalue, mypos : byte;
    getkey : char;

begin {main}
clrscr;
gotoxy(35,1);
write('Ghost Guzzler');
score:=0;
myvalue:=0;
lives:=3;
 ghostvalue:=getghostvalue;
 distance:=1;
 viewlives(lives);
repeat
 printposition(distance,ghostvalue,myvalue);
 { mydelay; }
 delay(100);
 if keypressed then begin
  read(kbd,getkey);
  if getkey IN [#120, #88, #109, #77] then begin
   case getkey of
    #120, #88 : checkresult(myvalue,ghostvalue);
    #109, #77 : begin
                 myvalue:=myvalue+1;
                 if myvalue=10 then myvalue:=0
                end;
   end;
  end;
 end;
 distance:=distance+1;
 if gotit then
  begin
   distance:=1;
   if ghostvalue=0 then score:=score+10 else score:=score+ghostvalue;
   updatescore(score);
   ghostvalue:=getghostvalue;
  end;
 gotit:=false;
 if distance=25 then
  begin
   lives:=lives-1;
   viewlives(lives);
   ghostvalue:=getghostvalue;
   distance:=1;
  end;
 until lives=0;
clrscr;
Gotoxy(30,11);
write('Your Ghost Guzzling');
gotoxy(34,12);
write('Score Is ',score);

end. {main}