My number guessing game-hoe can i set lives for the user - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# My number guessing game-hoe can i set lives for the user

Posts: 4Member
Ive managed to write a number guessing game that is working okay. Im pretty stuck now... i want to do the following:

1. set a variable to keep track of how many attempts the user has left(they have a max of 6 lives to get the correct answer in my game)?

2. Then i want the player told how many lives they have left

3. Then the player can type in their guess

This is as far as i have got:

Program_Number_Guessing_Game;
var
number guess: integer;
keypress:string;
Again:char;

begin
again:='y';
randomize;
while Again='Y'do begin

Num:=Random(30)+1;
Guess:=-1;

writeln('Go on guess...');
while guess<>Num do begin
if guessnumber then
writeln('too high')
else
writeln('Well Done...you guessed the correct number')
end;
writeln('would you like to play again?(Y/N');
end;
end.

Ive attemepted but am getting no where but if anyone willing to helpme then id be grateful.

Thankyou.

• Posts: 6,349Member
: Ive managed to write a number guessing game that is working okay. Im pretty stuck now... i want to do the following:
:
: 1. set a variable to keep track of how many attempts the user has left(they have a max of 6 lives to get the correct answer in my game)?
:
: 2. Then i want the player told how many lives they have left
:
: 3. Then the player can type in their guess
:
: This is as far as i have got:
:
: Program_Number_Guessing_Game;
: var
: number guess: integer;
: keypress:string;
: Again:char;
:
: begin
: again:='y';
: randomize;
: while Again='Y'do begin
:
: Num:=Random(30)+1;
: Guess:=-1;
:
: writeln('Go on guess...');
: while guess<>Num do begin
: if guessnumber then
: writeln('too high')
: else
: writeln('Well Done...you guessed the correct number')
: end;
: writeln('would you like to play again?(Y/N');
: end;
: end.
:
: Ive attemepted but am getting no where but if anyone willing to helpme then id be grateful.
:
: Thankyou.
:
You need to add a new integer variable and set that to the number of tries the user gets. After you have shown the result of the guess, you need to decrease this variable by 1 (either use dec() or simple math). The show that number in a writeln. The last thing to do is to change the while-do statement. This is a bit tricky, so I will give it:
[code]
while (guess<>Num) and (TryCounter>0) do begin
[/code]
This line will now check if the guess was different from the number and check if there are any tries left. The brackets are necessary to force the compiler to use the math correctly. If you leave them out it will be like this:
[code]
while guess<>(Num and TryCounter)>0 do begin
[/code]
And the compiler will give an error (probably type-mismatch).