Page 1 of 2

Guessing Game

Posted: Wed Feb 24, 2016 6:13 am
by drbfragged
Hi everyone! I am learning LiveCode for a class, and we are making a guessing game. I have the game working pretty well, but I'm trying to keep the same random number until it is correctly guessed, but how I have it set up currently, I set off an endless answer loop and have to force quit to stop it. I'm looking for advice on how to have LiveCode answer once and close the dialog, but keep the same random number generated at the beginning.

Code: Select all

on mouseUp
   put the text of fld "fld_guess" into theGuess
   
   set the randomSeed to the long seconds
   put random(100) into theAnswer
   
   //Will repeat using the same number until the correct number is guessed
   repeat until theGuess is theAnswer
      if theGuess is theAnswer then
         answer "Correct"
         exit repeat
         put empty into fld "fld_guess" 
         
         //If the number is more than 5 numbers too low
      else if theGuess < (theAnswer - 5) then
         answer "Too low! Try a higher number!"
         put empty into fld "fld_guess" 
         
         //If the number is more than 5 numbers too high
      else if theGuess > (theAnswer + 5) then
         answer "Too high! Try a lower number!"
         put empty into fld "fld_guess" 
         
         //If the number is 5 or less numbers too low   
      else if theGuess < theAnswer && (theGuess >= theAnswer - 5) then
         answer "Close! Just a little higher."
         put empty into fld "fld_guess" 
         
         //If the number is 5 or less numbers too high   
      else if theGuess > theAnswer && (theGuess <=theAnswer + 5) then
         answer "Close! Just a little lower!"
         put empty into fld "fld_guess" 
         
      end if
   end repeat   
end mouseUp



Re: Guessing Game

Posted: Wed Feb 24, 2016 7:48 am
by Simon
Hi drbfragged,
Welcome to the forums!

I think one of your classmates asked this question already;
http://forums.livecode.com/viewtopic.php?f=7&t=26547

Simon

Re: Guessing Game

Posted: Wed Feb 24, 2016 2:44 pm
by drbfragged
Hi Simon! Thanks for pointing me to that thread. I tried the suggestions listed, and I am still having trouble, but I'm not really sure why. The code worked before, except for the infinite answer loop, but now, I'm getting an error whenever I run it. I added to my previous code:

Code: Select all

if theAnswer is empty then
      put random(100) into theAnswer
   end if 

Re: Guessing Game

Posted: Wed Feb 24, 2016 5:44 pm
by Simon
Did you read in that thread that the repeat loop wasn't needed?

Simon

Re: Guessing Game

Posted: Wed Feb 24, 2016 6:41 pm
by drbfragged
I did. I tried it with and without the repeat loop, and neither seems to be working. I get a different error message each way. I get the message "button "Submit Guess": execution error at line 17 (Operators -: error in left operand), char 5" without the repeat loop and "button "Submit Guess": execution error at line 17 (Operators -: error in left operand), char 5" with the repeat.

Re: Guessing Game

Posted: Wed Feb 24, 2016 7:06 pm
by Simon
I don't know which line 17 is?
But it's clear that the problem isn't with the loop as the 2 error messages look the same to me.

Simon

Re: Guessing Game

Posted: Wed Feb 24, 2016 7:25 pm
by dunbarx
Hi.

Post your latest handler.

Do not cheat, though it seems you have not (good for you!), by looking at that other thread.

Craig Newman

Re: Guessing Game

Posted: Wed Feb 24, 2016 7:39 pm
by drbfragged
This is my current code. All of the errors seem to be referencing my "if" and "else if" statements, but I'm still unsure what the issue is.

Code: Select all

on mouseUp
   put the text of fld "fld_guess" into theGuess
   
   set the randomSeed to the long seconds
   if theAnswer is empty then
      put random(100) into theAnswer
   end if   
      
   //Will repeat using the same number until the correct number is guessed
   if theGuess is theAnswer then      
         answer "Correct"  
         put empty into fld "fld_guess"       
            
    //If the number is more than 5 numbers too low        
    else if theGuess < (theAnswer - 5) then     
         answer "Too low! Try a higher number!"   
         put empty into fld "fld_guess"    
            
     //If the number is more than 5 numbers too high       
     else if theGuess > (theAnswer + 5) then    
         answer "Too high! Try a lower number!"   
         put empty into fld "fld_guess"    
            
     //If the number is 5 or less numbers too low          
     else if theGuess < theAnswer && (theGuess >= theAnswer - 5) then    
         answer "Close! Just a little higher."   
         put empty into fld "fld_guess"    
            
     //If the number is 5 or less numbers too high          
     else if theGuess > theAnswer && (theGuess <=theAnswer + 5) then    
         answer "Close! Just a little lower!"   
         put empty into fld "fld_guess"    
            
     end if         
end mouseUp


Re: Guessing Game

Posted: Wed Feb 24, 2016 8:50 pm
by dunbarx
Step by step, literally, By that I mean have you stepped through your handler? You will find at the very top that the variable "theAnswer" may contain the text "theAnswer" as opposed to the value you will, in a later step, load with your random number. But then your conditional never fires.

Just as a way to move forward, comment out the "if/then" lines that frame that random function line, and then step through. You can then think about ways to initialize better.

Craig

Re: Guessing Game

Posted: Thu Feb 25, 2016 3:08 am
by drbfragged
Hi Craig. Thanks for the advice. I think that is exactly what is happening, since it works without the if statement around the random code, but I'm not entirely sure how to resolve that. I want the random number to stay the same until it is correctly guessed, but the if statement around the random code prevents the code from running, and a repeat around the other if/else if statements throws me into an infinite answer loop.

Re: Guessing Game

Posted: Thu Feb 25, 2016 4:06 am
by dunbarx
I am confused about the structure of your game.

Try this in a new button and tell me what you think. Do you know what custom properties are? The point of the handler is that the setting of the hidden answer is separate from the process that "repeats", asking for guesses until either the user gives up (is there a way out of that?) or guesses correctly, or the sun burns out.

Code: Select all

on mouseUp
   set the guessNumber of me to  random(100) 
   startAsking 
end mouseUp

on startAsking
   ask "Guess a number"
   if it is a number then
      put the guessNumber of btn 1 into tAnswer
      switch
         case it =  tAnswer
            answer "Bingo"
            exit to top
            break
         case it < tAnswer
            answer "Too Low"
            break
         case it >  tAnswer
            answer "Too High"
            break
      end switch
   end if
   startAsking 
end startAsking
Step through this. The custom property will appear in the lower handler. You can always put it somewhere more visible during development.

This ought to be a practice game for what is called a binary search. In other words, the game ought to track and exhort the user to find a way to use a minimum number of guesses. You might work on that.

There are other things it requires, besides your careful examination and feedback to me.

Craig

EDIT.

I changed "btn 2" in the above handler to "btn 1", which is what you would get if you made a new button on a card. I already had a button for another purpose.

Re: Guessing Game

Posted: Thu Feb 25, 2016 5:21 pm
by jacque
This will fail:

Code: Select all

    else if theGuess < theAnswer && (theGuess >= theAnswer - 5) then
Ampersands are used to concatenate text. For logical comparisons use "and".

Re: Guessing Game

Posted: Sat Feb 27, 2016 3:36 am
by drbfragged
Jacque, thank you for the information. I am more familiar with Java and C#, so I am used to using ampersands.

Hi Craig. That code works well, but I am hoping to find a way to continue the input from the field, rather than the ask window. I tried to edit the code to work with the field, but I have not been able to figure that out, if that is even an option. This is what my code looks like now.

Code: Select all

on mouseUp
   put the text of fld "fld_guess" into theGuess
   set the theGuess of me to random(100) 
   startAsking 
end mouseUp

on startAsking
   if it is a number then
      put the theGuess of fld "fld_guess" into theAnswer
      switch
         //if the number is guessed correctly
         case it = theAnswer then           
            answer "Correct"  
            put empty into fld "fld_guess"       
            exit to top
            break
            
            //If the number is more than 5 numbers too low        
         case it < (theAnswer - 5) then    
            answer "Too low! Try a higher number!"   
            put empty into fld "fld_guess"
            break    
            
            //If the number is more than 5 numbers too high       
         case it > (theAnswer + 5) then    
            answer "Too high! Try a lower number!"   
            put empty into fld "fld_guess"
            break    
            
            //If the number is 5 or less numbers too low          
         case it < theAnswer and (it >= theAnswer - 5) then    
            answer "Close! Just a little higher."   
            put empty into fld "fld_guess"
            break   
            
            //If the number is 5 or less numbers too high          
         case it > theAnswer and (it<=theAnswer + 5) then    
            answer "Close! Just a little lower!"   
            put empty into fld "fld_guess"
            break
            
      end switch
   end if
   startAsking 
end startAsking
I get the error "button "Submit Guess": compilation error at line 12 (switch: bad 'case' condition) near "then", char 20" for the line case it = theAnswer then

Re: Guessing Game

Posted: Sat Feb 27, 2016 3:56 am
by Simon
Hi drbfragged,
In a Switch/Case you don't need the "then".
Case handles true or false all by it's self. This can cause problems when the answer fits two different Case statements so be aware that the first Case will always trigger before the second.

Simon

Re: Guessing Game

Posted: Sat Feb 27, 2016 5:36 am
by dunbarx
Hi.

So much fun.

Oops. Ahem

Make a button and two fields. Name the button "Start". Put this into its script:

Code: Select all

global tAnswer

on mouseUp
   put random(99) into tAnswer
   put tAnswer into fld 2
   set the label of me to "Enter Guess"
   select text of fld 1
end mouseUp
In the FIRST field, where you will be entering the guesses, this into its script:

Code: Select all

global tAnswer

on returnInField
     switch
      case me =  tAnswer
         answer "Bingo"
         set the label of btn "start" to "Start"
         put "" into me
         exit to top
         break
       case me < tAnswer
         set the label of btn 1 to "Too Low"
         break
      case me >  tAnswer
         set the label of btn 1 to "Too High"
         break
   end switch
   put "" into me
   select text of me
end returnInField
Click the button, You can now enter a number into the guessing field, and hit return to terminate.

The second field is there just to display the random number. I used a global variable, well, just because. You can surely think of ways to improve this in terms of user interface. Let me know if this is getting closer to what you really want.

Craig