
-----------------------------------
tristanbryce7
Thu Oct 11, 2012 4:28 pm

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 .




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 
            exit when strintok (guessString) 
            put "Error! Error! Error!" 
            delay (1000) 
            put "Please enter a number from 1-20!" 
            delay (1500) 
            cls 

        end loop 

        guessInt := strint (guessString) 
        if guessInt > 0 and guessInt < 20 then 
            exit 
        else 
            put "Error! Error! Error!" 
            delay (1000) 
            put "Please enter a number from 1-20!" 
            delay (1000) 
            cls 
        end if 
    end loop 


    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!" 
    end if 
end loop 




-----------------------------------
Clayton
Thu Oct 11, 2012 5:09 pm

Re: Problem with Restarting Loop, and Counter
-----------------------------------
First of all, you have a whole lot of redundant code here:
    loop 
        loop 
            put "Hello, Please guess a number from 1-20" 
            get guessString 
            exit when strintok (guessString) 
            put "Error! Error! Error!" 
            delay (1000) 
            put "Please enter a number from 1-20!" 
            delay (1500) 
            cls 

        end loop 

        guessInt := strint (guessString) 
        if guessInt > 0 and guessInt < 20 then 
            exit 
        else 
            put "Error! Error! Error!" 
            delay (1000) 
            put "Please enter a number from 1-20!" 
            delay (1000) 
            cls 
        end if 
    end loop 

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:

loop
	put "Hello, please guess a number between 1 and 20 inclusive"
	get guessString
	if strintok (guessString) then
		exit when strint (guessString) >= 1 and strint (guessString)  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!"

            put "It took you ", myCounter, " try/tries to find the number!" %Once they guess the correct number, this statement tells them how many tries it took them
            delay (2100) %Stops the program for 2.1 seconds before restarting the loop
            cls
            exit
        end if




and it worked either way :D

-----------------------------------
Insectoid
Tue Oct 16, 2012 5:26 pm

RE:Problem with Restarting Loop, and Counter
-----------------------------------
[url=https://www.google.ca/search?q=increment&oq=increment&sugexp=chrome,mod=0&sourceid=chrome&ie=UTF-8]Learn to use Google. Or a dictionary. Solving your own problems is the first and most important step in learning to program.

-----------------------------------
tristanbryce7
Tue Oct 16, 2012 5:38 pm

Re: Problem with Restarting Loop, and Counter
-----------------------------------
Definitely, I just needed some help getting started .
