number guessing game
Author Message
number guessing game

Hi guys
Sorry to bother again, I am at wits end with a small game that I am trying
to code.
Basically a random number is generated and the user must guess the number
within 5 chances. If sucessful, user moves onto level 2(2 digit random no.)
with
11 guess chances. If sucessful on level 2, user moves onto level 3(3 digit
random no.)
with 16 guess chances. If user guesses the no. on level 3, then game is
finish.
Everytime the user fails on level 2 or level3, the game must drop down a
level.
i.e. if user fails to guess no. on level 3, then user must start on level 2
again.
If user fails on level one he, just repeats level 1 the whole time.

My problem i think is with my if - then else constructs. How do i get the
program
to loop the whole time (i.e. if fails on level 3, drop to level2, then if
pass on level 2
move up to level 3.)

Attached is my code. Its a bit long but i am still a newbie. The program
works
if i guess correctly on all levels, but hangs some places if i fail on a
level.
Thanks, Marlo

{ This program generates a random numbers and then allows the
user to guess the number }

Uses crt;

Var
randomno, cnt : integer;
guess : integer;
Begin
clrscr;
{writeln ('For this game you need to guess the correct number within 4
tries');}
writeln ('Level 1');
randomize;
randomno := random(10);
cnt := 0;
repeat

repeat
begin
write ('Enter your guess from 0 to 9, ');
if (guess < 0) and (guess > 9) then writeln ('Illegal number,
re-enter');
end;
until (guess >= 0) and (guess <= 9);
cnt := cnt + 1;
if guess = randomno then writeln ('Yes, you"ve got it')
else if guess > randomno then writeln ('Your guess is to large')
else if guess < randomno then writeln ('Your guess is to small');
until (guess = randomno) or (cnt = 5);
writeln;
if (guess = randomno) {and (cnt <> 4) then} then    {Level1 Complete}
begin
writeln ('Level 2');
cnt := 0;
randomize;
randomno := random(100);
repeat
repeat
begin
write ('Enter your guess from 0 to 99, ');
if (guess < 0) and (guess > 100) then writeln ('Illegal
number, re-enter');
end;
until (guess >= 0) and (guess <= 99);
cnt := cnt + 1;
if guess = randomno then writeln ('Yes, you"ve got it')
else if guess > randomno then writeln ('Your guess is to large')
else if guess < randomno then writeln ('Your guess is to
small');
until (guess = randomno) or (cnt = 11);
writeln;
end;

if (guess = randomno) {and (cnt <> 4) then} then  {Level2 Complete}
begin
writeln ('Level 3');
cnt := 0;
randomize;
randomno := random(1000);
repeat
repeat
begin
write ('Enter your guess from 0 to 999, ');
if (guess < 0) and (guess > 1000) then writeln ('Illegal
number, re-enter');
end;
until (guess >= 0) and (guess <= 999);
cnt := cnt + 1;
if guess = randomno then writeln ('Yes, you"ve got it, Game
Over')
else if guess > randomno then writeln ('Your guess is to large')
else if guess < randomno then writeln ('Your guess is to
small');
until (guess = randomno) or (cnt = 16);
writeln;
end
else if (guess <> randomno) and (cnt = 16) then
begin
writeln ('Level 2');
cnt := 0;
randomize;
randomno := random(100);
repeat
repeat
begin
write ('Enter your guess from 0 to 99, ');
if (guess < 0) and (guess > 100) then writeln ('Illegal
number, re-enter');
end;
until (guess >= 0) and (guess <= 99);
cnt := cnt + 1;
if guess = randomno then writeln ('Yes, you"ve got it')
else if guess > randomno then writeln ('Your guess is to large')
else if guess < randomno then writeln ('Your guess is to
small');
until (guess = randomno) or (cnt = 11);
writeln;
end

else
begin
writeln ('Level 1');
randomize;
randomno := random(10);
cnt := 0;
repeat
repeat
begin
write ('Enter your guess from 0 to 9, ');
if (guess < 0) and (guess > 9) then writeln ('Illegal number,
re-enter');
end;
until (guess >= 0) and (guess <= 9);
cnt := cnt + 1;
if guess = randomno then writeln ('Yes, you"ve got it')
else if guess > randomno then writeln ('Your guess is to large')
else if guess < randomno then writeln ('Your guess is to small');
until (guess = randomno) or (cnt = 5);
end;
writeln;