Need help with a number guessing game (from 1 to 100)
Author |
Message |
Danyn
|
Posted: Thu Sep 26, 2013 12:36 pm Post subject: Need help with a number guessing game (from 1 to 100) |
|
|
What is it you are trying to achieve?
I need to make a game where you guess from 1 to 100 and you only have 10 lives. As well as the fact that it doesn't allow you to choose a number under 1 and over 100.
What is the problem you are having?
I get syntax errors when I try to make it disable a number under 1 and over 100. As well as it still asks the user to pick a number even after they successfully beat the game
Describe what you have tried to solve this problem
I've tried rewriting and rewording but to no avail.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
%declares variables
var name, firstAnswer : string
var secretNumber, firstGuess, tries : int
%chooses a random interger
randint (secretNumber, 1, 100)
put "Hello stranger, what is your name?"
get name : *
loop
put "Okay ", name, ". Do you want to play a little game?"
get firstAnswer : *
exit when firstAnswer = "yes" or firstAnswer = "Yes"
put
"I'll ask you again... "
end loop
tries := 0
%main program starts here
put "Can you guess my secret number? It's a number between 1 and 100."
loop
tries + = 1
get firstGuess
put "Pick a number between 1 and 100."
if firstGuess > 100 then
put "Try reading the line above this again."
end if
if firstGuess < 1 then
put "Try reading the line above this again."
end if
if firstGuess > secretNumber then
put "Lower!"
end if
if firstGuess < secretNumber then
put "Higher!"
end if
exit when
firstGuess = secretNumber or tries = 10
end loop
if tries = 10 then
put ""
put "Sorry, you guessed 10 times. You lose sucka!"
end if
if firstGuess = secretNumber then
put ""
put "Congratulations! You win!"
end if
|
Please specify what version of Turing you are using
4.11 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Raknarg
|
Posted: Thu Sep 26, 2013 1:11 pm Post subject: RE:Need help with a number guessing game (from 1 to 100) |
|
|
Ever heard of elsif before?
What you want to do is put all of this into a single if statement like this:
Turing: |
if firstGuess > 100 or firstGuess < 1 then
put "Try reading the line above this again."
elsif firstGuess > secretNumber then
put "Lower!"
elsif firstGuess < secretNumber then
put "Higher!"
end if
|
What this does is it checks the first statement. If that's true, then it will skip all the other if statements, and just do whatever is in that block. This way you can make sure it only picks the right statement for the number picked.
Another thing you should note is that you should probably only add to tries when they made a valid, incorrect guess. Maybe show the number of tries left. |
|
|
|
|
|
Zren
|
Posted: Thu Sep 26, 2013 1:29 pm Post subject: RE:Need help with a number guessing game (from 1 to 100) |
|
|
Turing: |
exit when firstAnswer = "yes" or firstAnswer = "Yes"
|
Try converting the input all into lowercase (so you don't have to do 2^3 combinations).
|
|
|
|
|
|
Danyn
|
Posted: Thu Sep 26, 2013 3:01 pm Post subject: Re: Need help with a number guessing game (from 1 to 100) |
|
|
Okay so I rewrote the whole thing and made it better and more efficient.
Now, how do I make it so that it asks if the player wants to play again?
I heard that I can make it a process but I haven't learned about that yet.
Turing: |
%declares variables
var name, answer, endAnswer, no : string
var secretNumber, guess, tries : int
%chooses a random interger
randint (secretNumber, 1, 100)
put "Hello stranger, what is your name?"
get name : *
loop
put "Okay ", name, ". Do you want to play a little game?"
get answer : *
exit when Str.Lower (answer ) = "yes"
put
"I'll ask you again... "
end loop
tries := 10
%main program starts here
cls
loop
put "Pick a number between 1 and 100." ..
if tries = 1 then
put " You have ", tries, " try left!"
elsif tries > 1 then
put " You have ", tries, " tries left!"
end if
get guess
if guess > 100 or guess < 1 then
put "I said between 1 and 100."
end if
if guess > secretNumber and guess < 100 and guess >= 0 then
put "Lower"
tries - = 1
elsif guess < secretNumber and guess < 100 and guess >= 0 then
put "Higher"
tries - = 1
end if
exit when guess = secretNumber or tries = 0
end loop
%ending message
cls
if guess = secretNumber then
put "Congratulations, you won! The secret number was ", secretNumber, "!"
elsif guess not= secretNumber then
put "You lose! The secret number was ", secretNumber, "!"
end if
|
|
|
|
|
|
|
Raknarg
|
Posted: Thu Sep 26, 2013 3:22 pm Post subject: RE:Need help with a number guessing game (from 1 to 100) |
|
|
Dont go near processes, stay far, far away from them. In the Turing bible, God proclaimed that Processes were of Satan, and those who partook would burn in the fiery hell of brimstone an ignorance.
But seriously you'll never need them, and they screw with your program.
What you can do on the other hand is have a double nested loop, which is what I do for my games. It'll go something like this:
Turing: |
loop
loop
game ()
exit when win () or lose ()
end loop
put "Do you want to play again?"
get input
if input not= "yes" then
exit
end if
end loop
|
Easy as that. If they want to play again, it will just simply loop through. Otherwise, it will exit and the program ends. |
|
|
|
|
|
Danyn
|
Posted: Thu Sep 26, 2013 3:32 pm Post subject: Re: Need help with a number guessing game (from 1 to 100) |
|
|
I'm a little confused. What would you do in my case? I don't want it asking for the name again. I just want it to skip straight to the part where it asks the user to pick a number again. |
|
|
|
|
|
Raknarg
|
Posted: Thu Sep 26, 2013 3:36 pm Post subject: RE:Need help with a number guessing game (from 1 to 100) |
|
|
take your game and encapsulate everything starting from the first loop to the end of the program in another loop. From there, all you need to do is ask the user if they want to play again or not and you're good. |
|
|
|
|
|
btiffin
|
Posted: Sat Sep 28, 2013 2:28 am Post subject: RE:Need help with a number guessing game (from 1 to 100) |
|
|
A little advice Danyn;
Aside from fun factor, your initial loop after getting the player name might as well be "I'll take nothing but yes for an answer. Answer yes or be prompted again."
Try and not do that to people, po'po' gamers.
Better to accept a no, call us losers and spoil sports and then get out.
Cheers |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Sat Sep 28, 2013 8:20 am Post subject: RE:Need help with a number guessing game (from 1 to 100) |
|
|
Quote: randint (secretNumber, 1, 100)
Quote: I said between 1 and 100."
Quote: guess > 100 or guess < 1
Quote: guess < 100 and guess >= 0
You're sending mixed signals there. The number can be a minimum of 1, and a high of 100 (as the parameters for the randint function are inclusive).
I'd personally make the numbers controlling the range (1-100) into variables. That way if I ever wanted to reuse the code for a different range (say 400-1000), all I would have to do is change the variables in one spot rather than ~10 areas all over.
Hardcoding a number that you reuse in general is bad practice. Replacing it with a variable allows you to also describe the number, making the code more readable. |
|
|
|
|
|
Danyn
|
Posted: Sat Sep 28, 2013 12:27 pm Post subject: Re: RE:Need help with a number guessing game (from 1 to 100) |
|
|
Zren @ Sat Sep 28, 2013 8:20 am wrote:
You're sending mixed signals there. The number can be a minimum of 1, and a high of 100 (as the parameters for the randint function are inclusive).
I'd personally make the numbers controlling the range (1-100) into variables. That way if I ever wanted to reuse the code for a different range (say 400-1000), all I would have to do is change the variables in one spot rather than ~10 areas all over.
Hardcoding a number that you reuse in general is bad practice. Replacing it with a variable allows you to also describe the number, making the code more readable.
I think I see what you're saying but I'm not exactly too sure how I would implement it. Could you give me an example? |
|
|
|
|
|
Raknarg
|
Posted: Sat Sep 28, 2013 12:56 pm Post subject: RE:Need help with a number guessing game (from 1 to 100) |
|
|
var low = 1
var high = 100
loop
if guess >=low and guess <= high then
stuff ()
end if
end loop |
|
|
|
|
|
Zren
|
Posted: Sat Sep 28, 2013 1:36 pm Post subject: RE:Need help with a number guessing game (from 1 to 100) |
|
|
The following is the hardcoded version.
code: |
x = Rand.Int(34, 765)
loop:
put "Guess between ", 34, " and ", 765: "..
get input
if input < 34:
% Invalid
continue
else if 34 <= input and input <= 765:
% Valid
if input == x:
% Huzzah
break
else:
% Oh no!
else if 765 < input:
% Invalid
continue
|
Right now, we have to edit ~10 places whenever we want to change the numbers around. If we had instead used variables from the get go, we'd only have to change it in one spot.
code: |
xMin = 34
xMax = 765
x = Rand.Int(xMin, xMax)
loop:
put "Guess between ", xMin, " and ", xMax: "..
get input
if input < xMin:
% Invalid
continue
else if xMin <= input and input <= xMax:
% Valid
if input == x:
% Huzzah
break
else:
% Oh no!
else if xMax < input:
% Invalid
continue
|
Using variables instead of hardcoding also makes converting existing code into procedures a lot easier.
code: |
procedure guessingGame(xMin, xMax):
x = Rand.Int(xMin, xMax)
loop:
put "Guess between ", xMin, " and ", xMax: "..
get input
if input < xMin:
% Invalid
continue
else if xMin <= input and input <= xMax:
% Valid
if input == x:
% Huzzah
break
else:
% Oh no!
else if xMax < input:
% Invalid
continue
guessingGame(1, 10)
guessingGame(64, 324)
guessingGame(1, 1000000)
|
|
|
|
|
|
|
|
|