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;

End.

Fri, 11 Nov 2005 00:21:54 GMT  number guessing game
One way to do this is with a recursive procedure.  The logic is like
this:

PROCEDURE play (level : integer);

BEGIN   { play }
{ Depending on "level", generate number and the number of allowable
guesses.  }
hiddennumber := generatenumber (level);
maximumtries := numberofguesses (level);
IF numberguessed (hiddennumber, numberoftries)
THEN play (succ(level))
ELSE play (pred(level))
END;

The first few lines generate a "level-specific" hidden number and the
maximum number of guesses allowed.  These are passed to the function
"numberguessed" which returns "true" if we guess the number in the
given number of tries, otherwise it returns "false".  If we guess the
number, then go to the next level, or else go back to the previous
level.

Where this fails is that it doesn't handle the two "end" conditions,
namely success from level 3 or failure from level 1.  I would handle
both these conditions by putting addition code at the beginning of the
routine.  If we succeed from level 3, we'd enter the routine at level
4, so we could say
IF level = 4
THEN writeln ('Congratulations')
ELSE
BEGIN
hiddennumber := ... etc.

Because the rest of the procedure is contained in the ELSE clause, in
particular the recursive call is there, entering with level = 4 will
also cause us to exit from this procedure and return to the calling
routine.

We can do a similar thing to handle "failure from level 1", which
would show up as entering with level = 0.  Simply say
IF level = 0
THEN level := 1;
at the beginning of the procedure.

Bob Schor
Pascal Enthusiast

P.S. -- while some might argue that recursion is "inefficient", I like
it for problems like this because it captures the essence of the
problem, namely "Under these conditions, do the same thing with this
minor change" (i.e. let level go up or down by one).

Quote:
>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;

>End.

Fri, 11 Nov 2005 05:22:16 GMT  number guessing game
Thanks Bob,
I know that your solution is an optimal one, but is there a way to code this
program by  using loops eg. for, repeat and while instead of procedures and
recurrsion?

Quote:
> 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;

> End.

Fri, 11 Nov 2005 06:03:16 GMT  number guessing game
hi marlo?, clint?,

correct way.
http://www.uwasa.fi/~ts/http/quote.html might be a good address to
start.

Quote:

> Thanks Bob,
> I know that your solution is an optimal one, but is there a way to
> code this program by  using loops eg. for, repeat and while instead
> of procedures and recurrsion?

there ist no need to use recursion or even procedures in this situation.
although you should get familiar with procedures/functions at least very
soon.

have a look at the code below. it only uses constructs you have already

it was really much easier to rewrite the code than to correct yours.

if you're not familier with arrays, the code has to be redesigned a
little.

something like this:

repeat
if level = 1 then
your way to handle level 1
else
if level = 2 then
your way to handle level 2
else
your way to handle lelvel 3
until (level>3) or aborted;

the code to handle a certain level is pretty much the same every time.
and that is, why bob told you to use procedures.

i would prefer a function this time.

function level(levno: integer; range: integer; maxGuess:integer) :
boolean;
begin
this function should return true if the level is passed or false
otherwise.
end;

begin
repeat
if currentlevel = 1 then
if level(1,10,4) then inc(currrentlevel)
else
else
if level = 2 then
if level(2,100,7) then inc(currentlevel)
else dec(currentlevel);
else
if level(3,1000,12) then inc(currentlevel)
else dec(currentlevel)
until (currentlevel>3) or aborted;
end.

hth
jochen

- - -

program guessGame;
const
maxLevel = 4;
range : array [1..maxLevel] of integer = (10,100,1000,10000);
maxGuess : array [1..maxLevel] of integer = (4,7,10,14);
level : integer = 1;
var
guess  : integer;
rndNum : integer;
inpStr : string;
inpInt : integer;
err    : integer;
begin
randomize;
writeln;
writeln('Welcome to GuessGame ;-)');
writeln('Enter ''0'' to exit the game at any time');
repeat
writeln;
writeln('Level: ',level,
' Range: 1 - ',range[level],
' Max Try: ',maxGuess[level]);

guess := 1;
rndNum := random(range[level]) + 1 ;
repeat
repeat
write(guess,'. try: ');
val(inpStr, inpInt, err);
if err<>0 then
writeln('Invalid input.')
else
if (inpInt<0) or (inpInt>range[level]) then
begin
writeln('Out of Range');
err := 1
end;
until err=0;
if inpInt = 0 then guess := maxGuess[level]
else
begin
write(inpInt);
if inpInt = rndNum then
writeln(' is correct!')
else
if inpInt < rndNum then writeln(' is too low.')
else writeln(' is too high.')
end;
inc(guess)
Until (inpInt = rndNum) or (guess > maxGuess[level]);
if InpInt = rndNum then inc(level)
else
if level > 1 then dec(level);
Until (inpInt = 0) or (level > maxLevel);
If InpInt = 0 then
writeln('Aborted!')
else
writeln('You won.');
writeln('Bye. <Enter>');
end.

Fri, 11 Nov 2005 18:32:32 GMT  number guessing game

Quote:
> 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

You're right, your if-then-else constructs do have a problem because, in
this particular instance, they are not suited to what you are trying to do.
Consider declaring "level" as an integer variable.  Then you might be able
to to do something like:

level := 1;
repeat
if (level=1) then
begin
{code for level 1 here}
end;
if (level=2) then
begin
{code for level 2 here}
end;
if (level=3) then
begin
{code for level 3 here}
end;
until level<1 or level>3;

Each level is responsible for adjusting the "level" variable.  When the
level = 4, you win.  When level = 0, you quit.  Note that your code doesn't
allow for quitting the game except by winning.
Your code also has a few logic errors which will probably go away if the
above code structure is implemented.  I would also include code to tell the
player when he failed to find the number in the allotted tries, especially
on level 1.
When you learn about subroutines and arrays, you could rewrite the code for
a level to handle them all just by changing the 'highest number' and 'number
of tries' to variables.  Then the above structure might be changed to:

level := 1;
repeat
game(level, maxnum[level], tries[level]);
until level<1 or level>3;

hope this helps

-CB

Fri, 11 Nov 2005 21:46:51 GMT  number guessing game

requirements.

Make a 3 level game for guessing random positive integers.
Level 1-- Guess a number 0..9 within 5 guesses.
If successful advance to level 2.
If too many guesses do level 1 again.
Level 2-- Guess a number 0..99 within 11 guesses.
Success >> level 3.
Failure >> level 1.
Level 3-- Guess a number 0..999 within 16 guesses.
Success >> game over.
Failure >> level 2.

Since you are a novice and I am lazy, I will not bother to search out
the logic error(s) but the program you posted does work wrongly as you
describe.

Most of the fun in making a game is doing the programming in your
own style. The frustration comes in the debugging when your PC is
doing what you told it to do instead of what you thought you told it.
I usually have a few simple games that I rewrite as I learn more and
my style improves.

I have several generic suggestions:
1. Make the program easy to modify and debug.
2. Repeat as few operations as you can without over complexity.
3. When you know how to do something brute force, do it that way
until the game works and improve it later.

Your game needs only three things to perform all the actions:
(a) the game level, (b) the seed for the random number generator
and (c) the number of guesses allowed at that level.

You could just pass these to a single non-reduntant procedure from
the main program using a case statement:
Case level of
1:
2:
3:
For your program, I favor using two simple constant arrays and a
procedure with one variable parameter such as:
Procedure Play(VAR level:integer);
This makes the game easy to modify and debug by just changing some
constants. For ease of writing just set local variables to the
elements in the global constant arrays.

Things are only simple when you know how to do them and constant
arrays don't appear that often. Here is a skeleton of one
implementation (written in Turbo v6.0):}

Program NumberGuessing;
CONST
RandSeed: array[1..3] of integer =   (10, 100, 1000);
MaxGuesses: array[1..3] of integer = (5,  11,  16);
VAR
level:Integer;

Procedure Play(VAR lev:Integer);
VAR  {whatever you prefer}
ct, maxCt, target, guess, result, max:Integer;
Begin
target := Random(RandSeed[lev]);
maxCt := MaxGuesses[lev];
max := RandSeed[lev] - 1;
Writeln; Writeln('LEVEL ', lev, ':');
Writeln('You have ', maxCt, ' tries to guess a number 0..', max);
Writeln;

ct := 1;
Repeat
Repeat
Write (ct, '. Guess a number 0..', max, ': ');
Until (guess > -1) AND (guess <= max);
{ and so on ...
Do the scoring logic and display of results. Change ct and lev as
required.

Quote:
}

Until (result = 0) or (ct > maxCt);
If result = 0 then Inc(lev) else Dec(lev);
End;

BEGIN
Randomize;
level := 1;
Repeat
If level = 0 then Inc(level);
If level in [1..3] then Play(level);
Until level > 3;