Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

Destiny007Destiny007 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
Readln (Guess);
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');
Readln(Again);
end;
end.

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

Thankyou.

Comments

  • zibadianzibadian 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
    : Readln (Guess);
    : 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');
    : Readln(Again);
    : 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).
Sign In or Register to comment.