Posted: Thu Oct 11, 2012 4:28 pm Post subject: Problem with Restarting Loop, and Counter
Ok, so I have to make this game for class, where the computer makes a random int. from 1- 20, and you guess what it is, and it tells you if youre too high or too low, but problem is, after you finally guess the right amount, the program needs to tell you how many tries it took you to find the right amount, and i came in to the class 3 weeks late, so I didnt learn counters, counted loops and etc. So how do i put a counter in here that would tell you how much tries it took you to do it ? Also, once they get it right, i want the program, to re-start, but this time with a different number .
Turing:
var random :int var guessInt :int var guessString :string
random := Rand.Int (1, 20)
loop loop loop put"Hello, Please guess a number from 1-20" get guessString
exitwhenstrintok(guessString) put"Error! Error! Error!" delay(1000) put"Please enter a number from 1-20!" delay(1500) cls
endloop
guessInt :=strint(guessString) if guessInt > 0and guessInt < 20then exit else put"Error! Error! Error!" delay(1000) put"Please enter a number from 1-20!" delay(1000) cls endif endloop
if guessInt > random then put"The Number you entered is too high!" elsif guessInt < random then put"The Number you entered is too low!" elsif guessInt = random then put"You got it!" endif endloop
Sponsor Sponsor
Clayton
Posted: Thu Oct 11, 2012 5:09 pm Post subject: Re: Problem with Restarting Loop, and Counter
First of all, you have a whole lot of redundant code here:
Turing:
loop loop put"Hello, Please guess a number from 1-20" get guessString
exitwhenstrintok(guessString) put"Error! Error! Error!" delay(1000) put"Please enter a number from 1-20!" delay(1500) cls
endloop
guessInt :=strint(guessString) if guessInt > 0and guessInt < 20then exit else put"Error! Error! Error!" delay(1000) put"Please enter a number from 1-20!" delay(1000) cls endif endloop
Any time you feel like you're copy-pasting code is usually a time to step back and re-think how you're doing things. So for example, that ugly if statement can all be pushed up to your exit condition in the loop above like this:
Turing:
loop put"Hello, please guess a number between 1 and 20 inclusive" get guessString
ifstrintok(guessString)then exitwhenstrint(guessString) >= 1andstrint(guessString) <= 20 endif put"Error! Error! Error!" put"Please enter a number between 1 and 20 inclusive!" Time.Delay(1000) Draw.Cls endloop
Now then, onto your actual question, order to keep track of the number guesses, you'll have to have a counter that increments every time that a valid guess is input. Counters are nothing more than an integer variable that increment at some fixed rate when the conditions for it to do so are met. In this case, we just want it to increment by one every time a valid guess occurs, and then resets at the beginning of each game. For reference:
Turing:
var myCounter :int:=0 loop
myCounter +=1 put myCounter
Time.Delay(300) endloop
As far as re-running the game each time a successful guess occurs, simply wrap the whole game in a loop that re-initializes everything relevant at the start. Good luck!
tristanbryce7
Posted: Thu Oct 11, 2012 7:36 pm Post subject: Re: Problem with Restarting Loop, and Counter
Thanks! But my teacher said to do it for practice and dummyproofing, and btw , I have another problem . My code at the moment is
Turing:
var random :int var guessInt :int var guessString :string var myCounter :int:=0
random := Rand.Int (1, 20)
loop loop loop put"Hello, Please guess a number from 1-20" get guessString
exitwhenstrintok(guessString) put"Error! Error! Error!" delay(1000) put"Please enter a number from 1-20!" delay(1500) cls
endloop
guessInt :=strint(guessString) if guessInt > 0and guessInt < 20then exit else put"Error! Error! Error!" delay(1000) put"Please enter a number from 1-20!" delay(1000) cls endif endloop
if guessInt > random then put"The Number you entered is too high!" elsif guessInt < random then put"The Number you entered is too low!" elsif guessInt = random then put"You got it!"
myCounter := +1 put"It took you ", myCounter, " try/tries to find the number!" delay(300) endif
the problem is that no matter what i do , the counter always says it took you only 1 try? so how do i change / fix it. And also, when it ends, the number is still the same from last time, so if it was 13 the first time, it stays that way through out the program after the first time
Tony
Posted: Thu Oct 11, 2012 7:58 pm Post subject: Re: Problem with Restarting Loop, and Counter
tristanbryce7 @ Thu Oct 11, 2012 7:36 pm wrote:
the counter always says it took you only 1 try?
Where in the code do you increment the counter?
tristanbryce7 @ Thu Oct 11, 2012 7:36 pm wrote:
And also, when it ends, the number is still the same from last time
Presumably you mean the random number you are trying to guess? Where in the code do you pick that random number? How many times does that line get called?
Posted: Thu Oct 11, 2012 8:03 pm Post subject: Re: Problem with Restarting Loop, and Counter
Tony @ Thu Oct 11, 2012 7:58 pm wrote:
tristanbryce7 @ Thu Oct 11, 2012 7:36 pm wrote:
the counter always says it took you only 1 try?
Where in the code do you increment the counter?
tristanbryce7 @ Thu Oct 11, 2012 7:36 pm wrote:
And also, when it ends, the number is still the same from last time
Presumably you mean the random number you are trying to guess? Where in the code do you pick that random number? How many times does that line get called?
umm, im really new to turing lol , so i have no clue wat you're saying sorry man . im new to comp. programming in general,
if by increment you mean where did i put it ,i put it near the end lol
and the random number is put at the really beggining . OHHH, which is outside the loop , so i should prb put it inside lol . thanks , ill try it right noe,
if i do that tho, it changes it every time , so eevery time i guess, its a different number , so ill guess 1, and it says too low, and then ill say 2, than itll say too high, meaning it wasnt the same number as last time i guessed, so im totally confused
Tony
Posted: Thu Oct 11, 2012 9:01 pm Post subject: RE:Problem with Restarting Loop, and Counter
I'm just trying to get you to look at the right parts of your code. You already correctly figured out what the problem with the random number generation is -- now the question is: when do you want to pick a new random number? (that is, what should happen right before you'd need to pick a new number; place the code there).
As for the increment, you just need to look closely at
Posted: Fri Oct 12, 2012 7:13 am Post subject: RE:Problem with Restarting Loop, and Counter
Another thing to note is that you only ever modify the value of myCounter when you successfully guess the right number, so naturally it's only going to be one when you increment it. Every guess you make, regardless of the outcome, needs to increment myCounter.
tristanbryce7
Posted: Fri Oct 12, 2012 10:28 am Post subject: Re: Problem with Restarting Loop, and Counter
I tried all of it, I just cant get it , I tried changing the myCounter statement but nothing works , probably just my nubby programming knowledge
Thanks anyways lol
Sponsor Sponsor
Tony
Posted: Fri Oct 12, 2012 11:20 am Post subject: RE:Problem with Restarting Loop, and Counter
How are the following three values different (if at all)?
Posted: Fri Oct 12, 2012 1:56 pm Post subject: RE:Problem with Restarting Loop, and Counter
On top of that, carefully look at this code again, and try and spot what you did differently from me when incrementing myCounter. Keeping these things in mind, you should be able to spot why it's not working properly.
Turing:
var myCounter :int:=0 loop
myCounter +=1 put myCounter
Time.Delay(300) endloop
tristanbryce7
Posted: Mon Oct 15, 2012 6:48 am Post subject: Re: Problem with Restarting Loop, and Counter
i tried putting the counter in its own loop, both before and after the big loop,
and -1,1,+1 are different, as first goes decresing, other is nuetral, and other increases
Insectoid
Posted: Mon Oct 15, 2012 7:41 am Post subject: RE:Problem with Restarting Loop, and Counter
Quote:
i tried putting the counter in its own loop, both before and after the big loop
Read over that a few times and think about where you *haven't* tried it yet.
Quote:
-1,1,+1 are different, as first goes decresing, other is nuetral, and other increases
No, they're all just numbers. What's the difference between +1 and 1?
tristanbryce7
Posted: Mon Oct 15, 2012 12:05 pm Post subject: Re: Problem with Restarting Loop, and Counter
Yeah haha, i got it lol , problem with my old one was it was supposed to be
Turing:
if guessInt > random then put"The Number you entered is too high!"
myCounter := myCounter + 1 elsif guessInt < random then put"The Number you entered is too low!"
myCounter := myCounter + 1 elsif guessInt = random then put"You got it!"
myCounter := myCounter + 1 put"It took you ", myCounter, " try/tries to find the number!"
instead of what i had previously
Turing:
if guessInt > random then put"The Number you entered is too high!"
myCounter + 1 elsif guessInt < random then put"The Number you entered is too low!"
myCounter + 1 elsif guessInt = random then put"You got it!"
myCounter + 1 put"It took you ", myCounter, " try/tries to find the number!"
Insectoid
Posted: Mon Oct 15, 2012 5:03 pm Post subject: RE:Problem with Restarting Loop, and Counter
You could find a better spot, where you don't have to type it three times.
Clayton
Posted: Mon Oct 15, 2012 8:50 pm Post subject: RE:Problem with Restarting Loop, and Counter
Think about it this way, if you're going to increment it no matter what the result of the if statement is, what better option do you have?