 |
Critter Chat 6 |
|
|
Goal: Guess the Critter´s number in this guessing game.
Vocabulary: random, > (greater than sign), < (less than sign)
to startup
set "question "pos [-200 140]
set "announce "pos [-200 140]
setbg 0
t1, setsh "critter
conversation
end
to conversation
make "mynumber 1 + random 100
question [Hi! Let's play a guessing game! What is your name?]
if empty? answer [stop]
make "yourname answer
question (se :yourname [, guess a number between 1 and 100.] )
guessing-game
end
to guessing-game
if empty? answer [stop]
if answer > :mynumber [question (se answer [is too high. Guess again!] ) ]
if answer < :mynumber [question (se answer [is too low. Guess again!] ) ]
if answer = :mynumber [announce (se "Good, :yourname "! answer [is the right number!] ) stop]
guessing-game
end
to Let's_play_again!
startup
end
Notes We can create a guessing game in a conversation.
- In the conversation procedure, ask the computer to choose a random number between 1 and 100 and to remember it by creating a new variable with this command:
make "mynumber 1 + random 100
- In the guessing-game procedure, compare each answer to mynumber (using >, <, and =) and tell the visitor if the guess is too high, too low, or just right. Since we want the user to keep guessing, it is be a good idea to create a recursive procedure just for the guessing. This procedure will keep running until the visitor has guessed the number.
- A note about parentheses and brackets:
The guessing-game procedure uses a lot of brackets and parentheses! Here is why:
1) When we use if then we need to put brackets around the command or commands which the computer should do "if" the condition is true.
2) Since we are including each answer in the next question, we need to use se (sentence) to insert that answer, and we need to put parentheses around everything that gets included in the sentence.
3) Since we are using a group of words inside the sentence, we put a bracket around those words.
So when you see this at the end of a line: ] ) ],
the first bracket closes the cluster of words in the sentence,
the parenthesis closes the sentence,
and the last bracket closes the if command.
When you see this at the end of a line: ] ) stop ],
stop is still inside of the if command, and so if this condition is true, then after the computer announces that this is the right answer, the game will stop.
What If?
Try a different number after random. Try 50... Try 999. How many tries does it take you, on average, to guess the number now?
Try giving the mynumber variable a different name, throughout the project. Does it still work?
Challenge
Can you set up a different range of numbers for the guessing game so that, for instance, the number will always be between 100 and 200? or between 250 and 500? or between -100 and 100?
On Your Own
Can you make the Critter move higher or lower (or get bigger or smaller) if the guess is too high or too low?
Can you create two variables at the beginning of the guessing game procedure, each with a range between 1 and 10, and revise the game so that the user needs to multiply the two numbers and find out if the answer is too high, too low, or just right? (Use the * symbol when asking the computer to multiply.)
Can you set up the game so that after the user gets the right answer, another multiplication problem will be displayed?
|