Goal: We try to guess how many ants have come out of the anthill.
Vocabulary: everyone, remove, who, make, newturtle, word, setinstruction, random, if, question, answer, announce
everyone [remove who]
make "no 1
sethowmany 10 + random 25
repeat howmany [
newturtle word "t :no
setsize 13 st
setinstruction [forever [fd 2]]
seth random 360
make "no :no + 1
setcounter counter + 1
if counter = 6 [announce [GAME OVER!] stopall]
question [How many ants?]
if answer = howmany [announce [You are a genius!] stopall]
if answer < howmany [announce [too low]]
if answer > howmany [announce [too high]]
This guessing game anthill project is very different from the other anthill projects, so all of the procedures are different. The computer will create a batch of new ants, and the user will try to guess how many ants are running about on the project page.
- Create a text box named howmany and make it invisible. This text box will hold a random number chosen by the computer. The computer will hatch that many new turtles. Later on, the computer will compare the user's guesses to the number in the howmany text box to determine if the guess is too high, too low, or just right.
- Draw some thick colored lines on your green background. Program these colors with instructions such as rt 180 or lt 125, so that each turtle will turn away from the color after touching it. In this way, the lines act as walls.
- We need to get rid of all the existing turtles before hatching a new batch of turtles. In the killturtles procedure:
Everyone talks to each turtle in turn.
Remove followed by a name removes an object from a project. (You can also use remove "page2, for instance, to remove a page from a project.)
Remove who means, "Remove the current turtle." After it is removed, another turtle will be the current turtle. With the command everyone [remove who], each turtle in turn is removed from the page.
- The hatchturtles procedure creates a random number of new turtles.
Sethowmany 10 + random 25 places a random number in the hidden howmany text box. The number is at least 10 but fewer than 35.
- Repeat howmany uses the number in the hidden howmany text box to hatch that many new turtles.
- When we hatch new turtles, we need to give each of them a name.
Make "no 1 creates a variable called no and sets its value to 1.
- Newturtle word "t :no creates a new turtle and names it with the letter t followed by a number, the current value of no.
- Make "no :no + 1 takes the current value of no and adds one to it.
In this way, if 20 new turtles are hatched, they will have the names t1 through t20.
- Each new turtle starts out at the home position [0 0] in the center of the page. Draw or stamp an anthill there so that all of the new ants appear to swarm out of the anthill.
- Setsize 13 st gives the new turtles an ant's size and makes them visible.
- Each new turtle needs to be given an instruction so that it can be set in motion.
Setinstruction [forever [fd 2] ] is the same as opening each turtle's dialog box, writing fd 2, and setting it to many times, but now the instruction is given to each new turtle automatically as it is created.
- Each new turtle also needs to be given a starting direction in which to move. Seth random 360 means, "Set your heading to a random number which is at least 0 and is less than 360." So each turtle begins by setting its heading to a random direction.
- The guess procedure opens a question dialog box with the question, "How many ants?" and the user types an answer. The procedure then compares the answer to the number in the hidden howmany text box which holds the number of ants (turtles) on the page. A different announcement is made, depending on whether the answer is greater than, less than, or equal to the number in the howmany text box.
- Make a text box and name it counter to keep track of how many guesses have been made. The user is allowed no more than five guesses.
- The start procedure clears the text box, kills any existing turtles, runs the hatchturtles procedure to create a random number of ants, and sets the ants into motion.
- Make three buttons and name them start, guess, and stopall, set their instructions accordingly. Try to guess how many turtles come out of the anthill!
What happens if you use a different number in the command seth random 360
What happens if you use different numbers in the command sethowmany 10 + random 25
Program the colors with different instructions.
Find out the maximum number of new turtles which can be hatched without causing an error message.
On Your Own - Project Ideas
Create a random number of animated characters with newturtle
that are automatically given a different shape and assigned a heading.
Design a number guessing game with: large numbers, positive and negative numbers, fractions.