Home‎ > ‎

Guess

Guessing Games  (Repeat, Exit, Random) (code - Guess)
You can make a number guessing game. You are going to pick a number and see if someone can guess it
(The finished Guess game is below under "files". It is a LiveCode file that you can download and use)

1. Open a new mainstack and add a button on it called "Guess"
2. Add the following code to the button:

            on mouseUp

                   local myNum

                   put 40 into myNum

                   ask "Guess my number"

                   if it = myNum then

                      answer "Your Guessed it"

                   else

                      answer "Wrong, that is not it"

                   end if

            end mouseUp


Now run it.  We made our number 40 and put it into a variable that we named "myNum". The problem is they only have one guess to guess it. We want to fix it so they can keep guessing until they guess it. We also want to allow them to "cancel" or quit if they get tired guessing


We will add a variable called "guess" to save their guess into. We will also add a "repeat" statement to repeat everything until the guess the number.


    Change the code to read:   (Repeat)


on mouseUp

       local myNum,guess

       put 40 into myNum

   

       repeat until guess = myNum

              ask "Guess my number"

              if it is empty then exit repeat

              put it into guess

      

              if guess = myNum then

                 answer "Your Guessed it"

              else

                 answer "Wrong, that is not it"

              end if

       end repeat

   

end mouseUp



    
This is better but let's give them a little help by telling them if their guess is "high" or "Low". Let us also make sure they exit the repeat after they guess it. (Exit)

on mouseUp

   local myNum,guess

   put 40 into myNum

   

   repeat until guess = myNum

      ask "Guess my number"

      if it is empty then exit repeat

      put it into guess

      

      if guess < myNum then

         answer "You are too low, guess higher"

      end if


      if guess > myNum then

         answer "You are too high, guess lower"

      end if


      if guess = myNum then

         answer "Your Guessed it"

         exit repeat    

      end if


   end repeat   

end mouseUp



Now let us let the computer pick a random number for us to guess. We will tell it to pick a random number from 1 to 100. (Random)


on mouseUp

   local myNum,guess

   put Random(100) into myNum

   

   repeat until guess = myNum

      ask "Guess my number"

      if it is empty then exit repeat

      put it into guess

      

      if guess < myNum then

         answer "You are too low, guess higher"

      end if


      if guess > myNum then

         answer "You are too high, guess lower"

      end if


      if guess = myNum then

         answer "Your Guessed it"

         exit repeat

      end if


   end repeat   

end mouseUp



It works but we have too many pop-up boxes. Let us change the 'answer' to 'ask' and get rid of the other ask.

(When you write a program, after you get it working you sometimes see a better way to do it).

on mouseUp

   local myNum,guess

   put Random(100) into myNum

   ask "Guess my number"

   put it into guess


   repeat until guess = myNum      

                  if guess < myNum then
                     ask "You are too low, guess higher"
                     if it is empty then exit repeat
                     put it into guess

                  end if

                 if guess > myNum then
                     ask "You are too high, guess lower"
                     if it is empty then exit repeat

                  end if

        if guess = myNum then

           answer "Your Guessed it"

           exit repeat

        end if

   end repeat   

end mouseUp


Good work. We now have a fun guessing game
ċ
Guess.livecode
(1k)
cyril.pruszko@pgcps.org,
Mar 25, 2014, 5:13 PM
Comments