program games;
uses crt;

var b1: boolean;
player1score, player2score, a,B,c,p,range, highestrecord, lowestrecord, compguess: integer;
choose_to_end: string;
procedure rangeforuser;
begin

write('                      ', lowestrecord, ' < X < ', highestrecord);
writeln;
end;

procedure highlow;
     begin
     if A<B
        then begin
          writeln('too high');
          B1:=false;
          end;

          if A>B
          then
              begin
              writeln('too low');
              B1:=true;
         end;
IF B1 = TRUE
   THEN IF B > LOWESTRECORD
        THEN lowestrecord:= B;
IF B1 = FALSE
   THEN IF B < HIGHESTRECORD
        THEN HIGHESTRECORD:= B;
end;

procedure winner;
begin
textcolor(15);
IF A = COMPGUESS
   then writeln('You lose. The computer guessed correctly: ', compguess)
else writeln('Correct. It was ', B);
end;

{program starts here}
procedure program2;
begin
player1score:=0;
player2score:=0;
repeat
repeat
textcolor(3);
writeln('guess a number between 1 and 1000');
randomize;
A:=random(1001); {A = NUMBER TO GUESS}
highestrecord:=1000;
lowestrecord:=0;
while A <> B or compguess do
begin
textcolor(2);
repeat
repeat
write('You guess: ');
     readln(B);      {b=USERS GUESS}
until B < 1001;
until B > 0;
highlow;
if A = B
then begin
asm
inc player1score
end;
winner;
end;

 RANGE:= HIGHESTRECORD - LOWESTRECORD;
           COMPGUESS:= RANDOM(RANGE) + LOWESTRECORD;
textcolor(4);
    WRITELN('THE COMPUTER GUESSED ', COMPGUESS);
b:= compguess;
highlow;
rangeforuser;
if A = compguess
then begin

asm
inc player2score
end;
winner;
end;

end;
writeln('Do you wish to play again?[Y/N]');
readln(Choose_to_end);
until Choose_to_end = 'N';
until choose_to_end = 'n';
{menu;}
end;

procedure program3;
begin
player1score:=0;
player2score:=0;
repeat
textcolor(3);
writeln('guess a number between 1 and 1000');
randomize;
A:=random(1001); {A = NUMBER TO GUESS}
highestrecord:=1000;
lowestrecord:=0;
while A <> B or compguess do
begin
textcolor(2);
repeat
repeat
write('You guess: ');
     readln(B);      {b=USERS GUESS}
until B < 1001;
until B > 0;
highlow;
if A = B
then begin
asm
inc player1score
end;
winner;
end;

 RANGE:= HIGHESTRECORD - LOWESTRECORD;
          Compguess:= range div 2 + lowestrecord;
          {^only change from program 2^}
textcolor(4);
    WRITELN('THE COMPUTER GUESSED ', COMPGUESS);
b:= compguess;
highlow;
rangeforuser;
if A = compguess
then begin
asm
inc player2score
end;
winner;
end;
writeln('You have won ', player1score, ' matches. The computer has won ',player2score, ' matches.');
writeln('Do you wish to play again?[y/n]');
readln(Choose_to_end);
until Choose_to_end = 'n';
end;

   begin
   program3;

   end.