Home
Project Folders
Teacher Resources
Student Resources
En ESPAÑOL
How Do I?
Literacy
MWForum
Site Map
Search
Our Team

critter chat 1 2 3 4 5 6 7
Critter Chat 6
Screenshot
Turtle Face Get Web Player
  if you cannot see the project


Goal:   Guess the Critter´s number in this guessing game.




Vocabulary:   random, > (greater than sign), < (less than sign)

Code
Shapes
Project Map

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.  

  1. 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

  2. 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.

  3. 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?




© copyright 2001 - 2008  OpenWorld Learning.   All rights reserved.