Java StarLogo
2.0
english
`turtle`
turtles-own [code1 [none oh] code2 [none this]  code3 [none gate]  code4 [none key] number]

to hidecodewords   ;color the patches that hide code words
  setxy  ((random 25) - 25) (random 25) stamp lime
  setxy  ((random 25) - 25) ((random 25) - 25) stamp red
  setxy  (random 16) (random 16) stamp blue
  setxy  (random 25) ((random 25) - 25) stamp yellow
end

to initturtles 
  seth random 360 
  setcode1 none      ;start not knowing any code words
  setcode2 none
  setcode3 none
  setcode4 none       
  setnumber 0        ;set number of turtles who know all code words
  setc 119           
end

to go-turtle
  if (not (pc = 35)) [wiggle]           ;move, if not in castle
  if (pc = gray and number < 4)         ;don't enter castle without whole code
    [bk 1 seth random 360]
  if (pc = lime) and (code1 = none) [setnumber number + 1 setcode1 oh]  ;learn code word if on right patch
  if (pc = red) and (code2 = none) [setnumber number + 1 setcode2 this]
  if (pc = blue) and (code3 = none) [setnumber number + 1 setcode3 gate]
  if (pc = yellow) and (code4 = none) [setnumber number + 1 setcode4 key]
  scale-color 115 number 4 0  
   if (number = 4 and (not (pc = 35)))           ;when code known, go to castle
      [setxcor (17 + random 3) setycor (17 + random 3)]
end

to wiggle
  rt random 10
  lt random 10
  fd 1
end


`observer`
globals [time]

to setup
  settime 0
  ask-turtles [die] 
  ask-patches 
      [if ((pc = red) or (pc = blue) or (pc = lime) or (pc = yellow))
        [setpc 61]]         ;clears previous game's code words
  crt 1                     ;create turtle to hide code words
  ask-turtles [hidecodewords die]
  crt 20 
  ask-turtles [initturtles]
end 

to go 
  ask-turtles [go-turtle]
  settime time + 1  
end


`information`
For use with the Adventures in Modeling book,  by Vanessa Colella, Eric Klopfer, and Mitchel Resnick published by Teachers College Press, 2001.  

For more information, see  
<http://education.mit.edu/starlogo/adventures>  

Challenge 7: Castle Invasion  
To see the effects of turtle-turtle communication you will explore and modify the Castle Invasion project. In this project there is a four-word code that the turtles must learn before they can enter the castle. Initially, the turtles do not know any of the four code words. Scattered across the Graphics Canvas are four color-coded patches, each hiding one of the four code words. When the turtles visit one of these patches, they learn the code word associated with that patch. As the turtles learn more words, they turn darker shades of purple. When they successfully learn all four words they turn a very dark shade of purple and are transported inside the castle. Them monitors keep track of the total time that has passed and the number of turtles who have gained access to the castle.  

To start the simulation, click "setup" to create the castle, the special "code" patches, and the turtles on the screen. All turtles start in the center of the screen. When you click on the "go"  button, the turtles will move and gather code words. You can keep track of the total time it takes for the first turtle to gain access to the castle by watching the "time" monitor. You will know when turtles arrive at the castle by looking for them at the castle door and by checking the "# in castle" monitor, which keeps track of the number of turtles inside the castle.   

You can track the progress of individual turtles by opening their  Turtle Monitors and examining which words they know (stored in the  turtle variables code1 to code4). The four code words are variables that take words, instead of numbers, as values.


`interface`
SLCanvas
top-left 16 220

SLMonitor
top-left 111 110
width-height 65 36
name "# in castle"
list-to-run "count-turtles-with [number = 4]"
digits 0
delay 0.5
monitor-number 2
show-name? true
whichip 8
onewhichip 9

SLMonitor
top-left 111 31
width-height 65 36
name "monitor1"
list-to-run "time"
digits 0
delay 0.5
monitor-number 1
show-name? false
whichip 10
onewhichip 11

SLButton
turtle-or-observer? observer
top-left 60 67
width-height 69 30
name "button3"
line-to-run "go"
forever? true
button-number 4
show-name? false
whichip 6

SLButton
turtle-or-observer? observer
top-left 19 67
width-height 69 30
name "button1"
line-to-run "setup"
forever? false
button-number 2
show-name? false
whichip 7


`turtle shapes`
[encoded data, 5330 characters]
qqqqqqqaoR8DO5wb3dkOFQAAFQ7Z

`template`
null

`settings`
patch-size 8
num-shapes 256
screen-half-width 23
screen-half-height 22
interface-window-xcor 296
interface-window-ycor 87
interface-window-size 671 411
output-window-xcor 0
output-window-ycor 0
output-window-width 300
output-window-height 219
info-window-xcor 177
info-window-ycor 275
info-window-width 500
info-window-height 419
control-center-xcor 0
control-center-ycor 85
control-center-width 285
control-center-height 415
turtle-command-center-height 150
observer-command-center-height 150

`string table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`symbol table`
H4sIAAAAAAAAAGNgYGAAABzfRCE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