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:
answer "Wrong, that is not it" 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)
repeat until guess = myNum if it is empty then exit repeat answer "Wrong, that is not it"
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)
repeat until guess = myNum if it is empty then exit repeat
answer "You are too low, guess higher" answer "You are too high, guess lower"
if guess = myNum then
exit repeat
end if
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)
put Random(100) into myNum repeat until guess = myNum if it is empty then exit repeat answer "You are too low, guess higher" answer "You are too high, guess lower" 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).
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 if guess > myNum then ask "You are too high, guess lower" if it is empty then exit repeat
Good work. We now have a fun guessing game |
 Updating...
cyril.pruszko@pgcps.org, Mar 25, 2014, 5:13 PM
|