samedi 3 juin 2017

store a random number permanently livecode

I want to create a guessing game that stores numbers (out of 100) into quadrants, and lets the user know that A. they are too high/low and B. If they have entered the correct quadrant.

But I want the number that is randomly generated at the beginning of the program to stay the same until the user has guessed correctly.

I have researched random numbers on this site and the Chosen Answer (it's a C# thread though) is actually the same method I have come up with. But apparently I do not have the syntax right. Guessing game random number to static

I tried to use a global number and recreate it in each set of handlers, and put that global number into another variable. This made theAnswer come up blank after each guess though. I'm almost more interested in how I managed that than how to solve this problem, because this means I don't understand my own code, which is a bigger problem.

I followed a similar thread on this site about Javascript, and the Chosen Answer said to perform the entire initialization outside of the function.

JavaScript: How to stop random number from regenerating?

I changed the code below so that the openStack method I made contained both the randomization of thePreAnswer and the setting of theAnswer to that number. It is still blank. The code below makes more sense to me because Livecode uses

on openStack

global thePreAnswer

set the randomSeed to the long seconds

put random(100) into thePreAnswer

end openStack

on mouseUp

put the text of fld "fld_guess" into theGuess

local quarterZero, quarterOne, quarterTwo, quarterThree, quarterFour

put 0 into quarterZero

put 25 into quarterOne

put 50 into quarterTwo

put 75 into quarterThree

put 100 into quarterFour

global thePreAnswer

global theAnswer

put thePreAnswer into theAnswer

repeat until theGuess = theAnswer

if theGuess < theAnswer then

answer "Too low, guess higher!"

answer "The answer was " &&theAnswer

exit repeat

end if

if theGuess > theAnswer then

answer "Too high, guess lower!"

answer "The answer was " &&theAnswer

exit repeat

end if

if theGuess is theAnswer then

answer "Correct"

answer "The answer was " &&theGuess

put empty into fld "fld_guess"

put empty into theGuess

exit repeat

if "fld_guess" is empty then exit repeat

if theGuess > quarterZero AND theGuess <= quarterOne AND theAnswer >quarterZero AND theAnswer <=quarterOne then

answer "It's between 1 and 25! You're close!"

answer "The answer was " &&theAnswer

put empty into fld "fld_guess"

put empty into theGuess

exit repeat

if "fld_guess" is empty then exit repeat

if theGuess > quarterOne AND theGuess <= quarterTwo AND theAnswer >quarterOne AND theAnswer <=quarterTwo then

answer "It's between 25 and 50! You're close!"

answer "The answer was " &&theAnswer

put empty into fld "fld_guess"

put empty into theGuess

exit repeat

if "fld_guess" is empty then exit repeat

exit repeat

if theGuess > quarterTwo AND theGuess <= quarterThree AND theAnswer >quarterTwo AND theAnswer <=quarterThree then

answer "It's between 1 and 75! You're close!"

answer "The answer was " &&theAnswer

put empty into fld "fld_guess"

put empty into theGuess

exit repeat

if fld "fld_guess" is empty then exit repeat

exit repeat

if theGuess > quarterThree AND theGuess <= quarterFour AND theAnswer >quarterThree AND theAnswer <=quarterFour then

answer "It's between 1 and 100! You're close!"

answer "The answer was " &&theAnswer

put empty into fld "fld_guess"

put empty into theGuess

exit repeat

if fld "fld_guess" is empty then exit repeat

answer "repeat exited" exit repeat

exit repeat

end if

end if

end if

end if

end if

end repeat

end mouseUp




Aucun commentaire:

Enregistrer un commentaire