Page 1 of 1

A way to store a permanent number from a random number

Posted: Sat Jun 03, 2017 2:44 pm
by Unfortunate
What I want to do is create generate a random number and then store it, and only change once the user gets it right. This will allow the user to make more than one guess for the same number.

The main idea to this program is to categorize the guess and the answer into quadrants, and only tell the user which quadrant they are in if they've managed to lower or raise the number into said quadrant. The program will always tell them if they are too high or too low. The only thing left is to keep the number from constantly changing.



I have every part of this code telling me what the answer is at the moment so that I can see if it returns correctly. However, at the moment the number keeps coming up blank. I don't think I am using globals correctly.

If anyone could help point me in the right general direction it would be greatly appreciated.

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


repeat until theGuess = theAnswer

put thePreAnswer into 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

Re: A way to store a permanent number from a random number

Posted: Sat Jun 03, 2017 4:14 pm
by jmburnod
Hi,
I don't think I am using globals correctly.
Right.
You have to declare them on top of script

Code: Select all

global thePreAnswer
on openStack
   set the randomSeed to the long seconds
   put random(100) into thePreAnswer
end openStack
Best regards
Jean-Marc

Re: A way to store a permanent number from a random number

Posted: Sat Jun 03, 2017 8:20 pm
by dunbarx
I would use a custom property instead of a global.

I did not look deeply at your code, but I bet it can be made much more compact. Give this a try again (if you care to) rethinking from the very beginning the way the handler progresses.

Craig Newman

Re: A way to store a permanent number from a random number

Posted: Sun Jun 04, 2017 9:02 pm
by Unfortunate
jmburnod wrote:Hi,
I don't think I am using globals correctly.
Right.
You have to declare them on top of script

Code: Select all

global thePreAnswer
on openStack
   set the randomSeed to the long seconds
   put random(100) into thePreAnswer
end openStack
Best regards
Jean-Marc
Thank you so much for your reply and answer.

Unfortunately the number still came up blank somehow.

dunbarx wrote:I would use a custom property instead of a global.

I did not look deeply at your code, but I bet it can be made much more compact. Give this a try again (if you care to) rethinking from the very beginning the way the handler progresses.

Craig Newman
Thank you for taking the time to reply to my thread. I will try doing that.

I'm not sure if it can be made more compact myself either, definitely so in other languages. I am only a livecoder for a week and a half now and it is very different from Java or C++ (it is more similar to the "scripts" found in game engines than a pure coding language like the former two).

Re: A way to store a permanent number from a random number

Posted: Sun Jun 04, 2017 10:50 pm
by jmburnod
Unfortunately the number still came up blank somehow.
Which number ?
I tested it, thePreAnswer is not empty. It contains a random number

Re: A way to store a permanent number from a random number

Posted: Sun Jun 04, 2017 10:51 pm
by Newbie4
Basic commands in LiveCode are not very different from other languages and the basic logic is usually the same. The problem most people have in coding is that they start writing the code too early, before they have completely figured out the logic. For some people, it is better to start small and simple, and get the base program working. Then gradually improve it in small steps.

You are on the right track. You might look at this link https://sites.google.com/a/pgcps.org/li ... home/guess to help you think through your code.

A few suggestions:
1. You do not need a seed, You only need to generate the number once. Use a repeat loop until the user guesses it. The generate a new number after they have guessed it
2. Your code is too complex because you have too much code in the repeat loops. Take the code that only needs done once, and remove it from the repeat loop.
3. Use variables to hold data that is calculated once. Try not to duplicate too much.

After looking at that link, here is rough pseudocode to help you get your code working

Code: Select all

on openStack
   put random(100) into thePreAnswer
   if theAnswer > 0 and theAnswer <= 25 then  put "Quartier 0" into theQ
   if theAnswer > 25 and theAnswer <= 50 then  put "Quartier 1" into theQ
   if theAnswer > 50 and theAnswer <= 75 then  put "Quartier 2" into theQ
   if theAnswer > 75 and theAnswer <= 100 then  put "Quartier 3" into theQ
end openStack

on mouseUp
   ....
   repeat until theGuess is equal to theAnswer
      ....basic logic here
      answer "The answer is in " & theQ
   end repeat
   ...
end mouseUp
Hope this helps

Re: A way to store a permanent number from a random number

Posted: Mon Jun 05, 2017 12:04 am
by Unfortunate
jmburnod wrote:
Unfortunately the number still came up blank somehow.
Which number ?
I tested it, thePreAnswer is not empty. It contains a random number
theAnswer, after putting thePreAnswer into it.
Newbie4 wrote:Basic commands in LiveCode are not very different from other languages and the basic logic is usually the same. The problem most people have in coding is that they start writing the code too early, before they have completely figured out the logic. For some people, it is better to start small and simple, and get the base program working. Then gradually improve it in small steps.

You are on the right track. You might look at this link to help you think through your code.

A few suggestions:
1. You do not need a seed, You only need to generate the number once. Use a repeat loop until the user guesses it. The generate a new number after they have guessed it
2. Your code is too complex because you have too much code in the repeat loops. Take the code that only needs done once, and remove it from the repeat loop.
3. Use variables to hold data that is calculated once. Try not to duplicate too much.

After looking at that link, here is rough pseudocode to help you get your code working

Code: Select all

on openStack
   put random(100) into thePreAnswer
   if theAnswer > 0 and theAnswer <= 25 then  put "Quartier 0" into theQ
   if theAnswer > 25 and theAnswer <= 50 then  put "Quartier 1" into theQ
   if theAnswer > 50 and theAnswer <= 75 then  put "Quartier 2" into theQ
   if theAnswer > 75 and theAnswer <= 100 then  put "Quartier 3" into theQ
end openStack

on mouseUp
   ....
   repeat until theGuess is equal to theAnswer
      ....basic logic here
      answer "The answer is in " & theQ
   end repeat
   ...
end mouseUp
Hope this helps
I don't mean the basic commands, haha. I'm just saying it things like the "ask" and "question", as well as the Handler system are quite different from what you'd see in a traditional language. I think Visual Basic/C# have a somewhat similar function, but not quite as direct. I agree on your point about "most people". I hate to have joined that club, but unfortunately for me I don't have a choice since this is a class and that's how they have it set up. If it were up to me this assignment would be given during week four, not week two (at the minimum). We were given code to start with, and then we had to alter it. I added the quadrants and the too-high too-low comparison loops. In the week before this we were making our own guessing game.

Thank you so much for the link, I have actually been following that. Is it better to use an ask box instead of a field in this situation?

About your suggestions, do you mind if I reply to them?

1. Thanks (not really a reply, I just didn't know that).

2. All of it needs to be done more than once though. It does six tests, tests for: a number too high, a number too low, a number that is correct, a number in the first quadrant, second quadrant, third quadrant and fourth quadrant. Maybe there is unnecessary syntax in the code...

3. You mean like an array?

Re: A way to store a permanent number from a random number

Posted: Mon Jun 05, 2017 1:38 am
by Newbie4
It sounds like you already know something about programming.

That is a good assignment. You can't just look up the code on the Internet. You have to figure it out yourself. You're in good shape. You have enough from these posts to be able to do the assignment now. It is good practice. Good luck.

Using an ask vs a field is a matter of design. What fits best in the design and purpose of your program? One is not necessarily better than another. Most people prefer fields, especially when you are collecting a lot of data.

It is too early to get into arrays. I was just pointing out that if you have similar or "almost" repetitive code, use a variable for the part that is different and you only need 1 statement with that variable in it.

For a good experience, first get your code working, then see if you can shorten it It is a good learning experience.

Post your working program when you get it done.

Re: A way to store a permanent number from a random number

Posted: Mon Jun 05, 2017 4:19 am
by Unfortunate
Sure I can look up code, so long as it's the same as looking in a book-educational code. I don't think there are any books on Livecode so you literally have to do that. Most programming books have example code to teach you, for the purpose of teaching syntax (unless by "code" you mean illegally stealing and ripping off someone's program). It is already too late anyhow (I have failed the assignment already) I just thought I would try and figure it out just to further myself.


It turned out I was not allowed to modify the assignment's code (even though that's exactly what it said). I was supposed to modify the guessing game to make it fun.


Thank you guys for helping me. I will take everything you said into account.

Re: A way to store a permanent number from a random number

Posted: Mon Jun 05, 2017 4:14 pm
by dunbarx
Hi.

So since you are so polite and engaging, try this. I used Newbies idea as a start. Note that he is not really a newbie.

On a new card make two buttons. Name one button "Set Number". Name the other one "Start Guessing"

In the script of btn "Set Number";

Code: Select all

on mouseUp
       put random(100) into temp
   
      if temp > 0 and temp <= 25 then  put "Quadrant 1,25" into theQ
      if temp > 25 and temp <= 50 then  put "Quadrant 2,50"  into theQ
      if temp > 50 and temp <= 75 then  put "Quadrant 3,75"  into theQ
      if temp > 75 and temp <= 100 then  put "Quadrant 4,100"  into theQ
   
   set the secretNumber of this card to temp
   set the quadrant of this card to theQ 
end mouseUp
And in the script of btn "Start Guessing":

Code: Select all

on mouseUp
   ask "Enter your Guess"
   if it is an integer and it = the secretNumber of this card then
      answer "Correct!"
      exit to top
   end if
   
   if it > the secretNumber of this card then put "Too high" into missedBy else put "Too low" into missedBy
   
   answer "You are in" && item 1 of the quadrant of this card & return & "You are a little" && missedBy
end mouseUp
Now this can be made more fun if the user does not have to keep pressing that button over and over. Newbie implied this in his pseudoCode. Your job now. It is also your job to work out the setting of the ranges of the four quadrants if any random number (not just 100) is desired as a start. Oh, and make it look nicer than a card with two buttons.

Craig

How am I doing, Jacque?