Pico, Fermi, Bagels: A Guess My Number Game


game2.jpg

Bagels or Pico, Fermi, Bagels is a number game. The computer thinks of a number and you try to guess what it is. Along the way you are given hints as to whether your guess contains any of the digits of the computer’s number and if they are in the proper place.screenstart2.jpg


In the above screen image the player has guessed the digits that make up the computer’s number but the digits are in the wrong places.

If you click on the HOW TO button you are transported to a page that shows the rules of the game. The page is named how.to.

to instructions
how.to
end

how.to.screen.png


Starting Up the Game


Clicking on the play button starts the game. Turtle t4 is a bee and is a decoration. Turtles d1, d2, and d3 are the digits you select. The t5 turtle remains hidden throughout the game and carries the background image. You will see later why this turtle is necessary.

to play
ask [t6] [setsh 92 wait 2 setsh 93]
ask [t4] [clickon]
ask [d1 d2 d3] [ht]
ask "t5 [ht setpos [0 0] setsh 70 stamp]
ask [n1 n2 n3 clue1 clue2 clue3][sety 135 ht seth 0]
make "num get.num
make "tries 0
make "guess "
make "click 0
end


Play sets up four variables.
:num is the computer’s three-digit number
:tries is a counter for the number of attempts you make
:guess is your number
:click is a counter to know when you have selected three digits.

The procedure, getnum, makes up the computer’s number. This procedure uses get.nozero, which uses random to generate a digit from 1 thru 9 and get.digit, which can generate a digit from 0 thru 9. Notice get.nozero is used to generate the first digit, which is not allowed to be 0. Thus 008 is not a number nor is 097.

If identical digits are generated, get.num makes up a new number.


to get.num
make "hundreds get.nozero
make "tens get.digit
make "units get.digit
ifelse (or :hundreds = :tens :hundreds = :units :tens = :units)
[op get.num]
[op (word :hundreds :tens :units)]
end

to get.nozero
op 1 + random 9
end

to get.digit
op random 10
end


Now the game is set and nothing visible happens, except the bee flies, until you click on three digits. Whenever you click on a digit turtle, the calc procedure is run.
The digit turtle blinks off and on to show you clicked on it. Then calc turns the job over to putnum.


to calc
blink putnum
end

to blink
repeat 1 [ht wait 1 st wait 1]
end



Here is putnum. It gets help from checknum and compare.guess. First putnum uses checknum to make sure 0 isn’t the first digit. Then putnum checks to see if you have clicked on three digits. If you have clicked on three digit turtles putnum assigns the digit shapes to three turtles and then displays the turtles in the box above the keyboard.

to putnum
make "click :click + 1
checknum last who
If 3 > count :guess [stop]
ask [n1][setsh word "reply first :guess]
ask [n2][setsh word "reply first bf :guess]
ask[n3] [setsh word "reply last :guess ]
ask [n1 n2 n3] [st]
make "click 0
compare.guess
end

The procedure, checknum, makes sure that the number you are constructing is legal. If the first digit of your number is zero it is rejected (by the if statement).

to checknum :digit
If and :click

1 :digit

0 [set.click stop]
make "guess word :guess :digit
ask (se word "d :click) [setsh word "n :digit st]
end

If the digit is okay it is attached to the other digits in your guess and displayed in the top left box.

Checking to see if your number matches the computer’s is handled by compare.guess. The number is a word and so it is taken apart using first, bf (butfirst) and last.

to compare.guess
let [fermi 0 pico 0 bagels "false]
make "tries :tries + 1
if equal? first :guess first :num [make "fermi :fermi + 1]
if member? first :guess bf :num [make "pico :pico + 1]
if (first bf :guess) = first bf :num [make "fermi :fermi + 1]
if member? first bf :guess word first :num last :num [make "pico :pico + 1]
if (last :guess) = last :num [make "fermi :fermi + 1]
if member? last :guess bl :num [make "pico :pico + 1]
ifelse 0 = (:pico + :fermi) [make "bagels "true][make "bagels "false]
print.clues
if :fermi = 3 [congrats stop]
if :tries = 20 [sorry stop]
make "guess " make "click 0
ask [d1 d2 d3][ht]
end



The Whole Thing

to play
ask [t4] [clickon]
ask [d1 d2 d3] [ht]
ask "t5 [ht setpos [0 0] setsh 70 stamp]
make "num get.num
make "tries 0
make "guess "
make "click 0
setup.start
end

to instructions
how.to
end

to get.num
make "hundreds get.nozero
make "tens get.digit
make "units get.digit
ifelse (or :hundreds = :tens :hundreds = :units :tens = :units)
[op get.num]
[op (word :hundreds :tens :units)]
end

to get.nozero
op 1 + random 9
end

to get.digit
op random 10
end

to set.click
make "click 0
make "guess "
end

to calc
blink putnum
end

to putnum
if not name? "click [stop]
make "click :click + 1
checknum last who
If 3 > count :guess [stop]
ask [n1][setsh word "reply first :guess]
ask [n2][setsh word "reply first bf :guess]
ask[n3] [setsh word "reply last :guess ]
ask [n1 n2 n3] [stamp]
make "click 0
make "cluenum 1
compare.guess
ask [n1 n2 n3 clue1 clue2 clue3] [bk 20]
end

to checknum :digit
If and :click

1 :digit

0 [set.click stop]
make "guess word :guess :digit
ask (se word "d :click) [setsh word "n :digit st]
end

to compare.guess
let [fermi 0 pico 0 bagels "false]
make "tries :tries + 1
if equal? first :guess first :num [make "fermi :fermi + 1]
if member? first :guess bf :num [make "pico :pico + 1]
if (first bf :guess) = first bf :num [make "fermi :fermi + 1]
if member? first bf :guess word first :num last :num [make "pico :pico + 1]
if (last :guess) = last :num [make "fermi :fermi + 1]
if member? last :guess bl :num [make "pico :pico + 1]
ifelse 0 = (:pico + :fermi) [make "bagels "true][make "bagels "false]
print.clues
if :fermi = 3 [congrats stop]
if :tries = 20 [sorry stop]
make "guess " make "click 0
ask [d1 d2 d3][ht]
end

to print.clues
if :bagels [ask [clue1] [setsh 72 stamp stop]]
repeat :fermi [ask word "clue :cluenum [setsh 73 stamp]make "cluenum :cluenum + 1]
repeat :pico [ask word "clue :cluenum [setsh 71 stamp] make "cluenum :cluenum + 1]
end

to blink
repeat 1 [ht wait 1 st wait 1]
end

to undo
carefully [ask word "d :click [ht]][]
carefully [make "guess bl :guess][stop]
make "click :click - 1
end

to congrats
wait 5
hooray
ask [try1 try2][ht]
ask [digit1] [ setsh word "n first :num]
ask [digit2] [setsh word "n first bf :num]
ask [digit3][setsh word "n last :num ]
ask [try1] [setsh word "n first :tries st]
if 1 = count :tries [stop]
ask [try2] [setsh word "n last :tries st]
end

to sorry
wait 5
toobad
ask [t1 t2 t3] [ht sety -60]
ask [t1] [ setsh word "n first :num]
ask [t2] [setsh word "n first bf :num]
ask [t3][setsh word "n last :num ]
ask [t1 t2 t3][st]
end

If the clue board gets messed up this procedure will help.

to setup.start
ask "t2 [ht setpos [195 135]]
ask [n1 n2 n3 clue1 clue2 clue3][sety 135 ht seth 0]
n1, setx 100 setsh 81
n2, setx 125 setsh 83
n3, setx 150 setsh 89
clue1, setx 195
clue2, setx 235
clue3, setx 270
end

if you start from scratch these two procedures will help.

to make.numbers :number
if :number < 0 [stop]
newturtle word "c :number
set word "c :number "rule [launch [calc]]
setsh word "n :number
st
make.numbers :number - 1
end

to setup.nums
c0, setpos [-168 40]
c1, setpos [-258 -20]
c2, setpos [-214 -20]
c3, setpos [-168 -20]
c4, setpos [-258 -80]
c5, setpos [-214 -80]
c6, setpos [-168 -80]
c7, setpos [-258 -140]
c8, setpos [-214 -140]
c9, setpos [-168 -140]
end