Java StarLogo
2.0
english
`turtle`
turtles-own [lastx lasty lasth]

;reaction 1: red + blue <-> yellow
;reaction 2: yellow + white <-> green

to go
  fd (temperature / 100)
  rt random 10 lt random 10
  grab one-of-turtles-here 
  [
    if ((color = red) and ((color-of partner) = blue)) and ((random 100) <= reaction-1)
      [setc yellow setc-of partner yellow fd 1 stop]
    if ((color = blue) and ((color-of partner) = red)) and ((random 100) <= reaction-1)
      [setc yellow setc-of partner yellow fd 1 stop]
    if ((color = yellow) and ((color-of partner) = yellow)) and ((random 100) > reaction-1)
      [setc red setc-of partner blue fd 1 stop]
    if ((color = yellow) and ((color-of partner) = white)) and ((random 100) <= reaction-2)
      [setc green setc-of partner green fd 1 stop]
    if ((color = white) and ((color-of partner) = yellow)) and ((random 100) <= reaction-2)
      [setc green setc-of partner green fd 1 stop]
    if ((color = green) and ((color-of partner) = green)) and ((random 100) > reaction-2)
      [setc yellow setc-of partner white fd 1 stop]
  ]
end


`observer`
globals [degred deggreen degyellow degblue degwhite number]
 ;global variables used in drawing pie graph

to startup
 plotid 2
end

to setup
  plotid 2
  clearplot
  setplot-title ""
  ca
  create-and-do reds [setc red]
  create-and-do blues [setc blue]
  create-and-do yellows [setc yellow]
  create-and-do greens [setc green]
  create-and-do whites [setc white]
  ask-turtles [setxy (random screen-width) (random screen-height)]
  setupgraph
end

to plot-colors 
  pp1 plot count-turtles-with [color = red]  
  pp2 plot count-turtles-with [color = blue]  
  pp3 plot count-turtles-with [color = yellow]  
  pp4 plot count-turtles-with [color = green]  
  pp5 plot count-turtles-with [color = white]
end
 
to setupgraph
  pp1 ppreset setppc red ppd
  pp2 ppreset setppc blue ppd
  pp3 ppreset setppc yellow ppd
  pp4 ppreset setppc green ppd
  pp5 ppreset setppc black ppd    ;white turtles graphed in black
  setplot-yrange 0 250 
  setplot-xrange 0 25
  setplot-title "Chemical Counts"  
end



to draw-piechart       ;draw a pie graph of the colors
  setnumber count-turtles
  setbg black
  setdegred (numred * 360 / number) + 1
  setdegyellow (numyellow * 360 / number) + 1
  setdegblue (numblue * 360 / number) + 1
  setdegwhite (numwhite * 360 / number) + 1
  setdeggreen (numgreen * 360 / number) + 1
  ask-turtles 
   [setlastx xcor setlasty ycor setlasth heading
    home
    if (color = red) [seth (random degred) + 1]
    if (color = blue) [seth (random degblue) + degred + 1]
    if (color = green) [seth (random deggreen) + degblue + degred + 1]
    if (color = yellow) [seth (random degyellow) + deggreen + degblue + degred + 1]
    if (color = white) [seth (random degwhite) + degyellow + deggreen + degblue + degred + 1]
    pd fd 20]
end

to go-back
  ask-turtles [ht pu]
  setbg black 
  ask-turtles [setx lastx sety lasty seth lasth st]
end

to start
  startgobutton
  startplotbutton
end

to stopit
  stopgobutton
  stopplotbutton
end

;numblue, numred, numyellow, numwhite, numgreen
;are all used in the blues, reds, yellows, whites, and greens monitors
;these values are not the same as the slider values 
;except immediately after setup

to numblue
 output count-turtles-with [color = blue]
end

to numred
 output count-turtles-with [color = red]
end

to numyellow
 output count-turtles-with [color = yellow]
end

to numwhite
 output count-turtles-with [color = white]
end

to numgreen
 output count-turtles-with [color = green]
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 8: Reaction  
Reaction employs five different turtle colors to simulate the following system of reversible chemical reactions:           
red + blue <-> yellow           
yellow + white <-> green  

You can control the probability of the forward reactions using the  "reaction-1" and "reaction-2" sliders. The probability of the reverse reactions is defined as 1 minus the probability of the forward reactions. Adjusting the "temperature" slider controls the speed of molecular movement. To change the initial conditions, simply reset the color sliders.  Stop the simulation and then click on the "draw-piechart" button to display the relative concentrations of the molecules. You can start the simulation again by pressing "start" or "go-back" and then "start".


`interface`
SLCanvas
top-left 12 258

SLButton
turtle-or-observer? observer
top-left 55 173
width-height 64 32
name "button9"
line-to-run "view-plot"
forever? false
button-number 10
show-name? false
whichip 14

SLSlider
top-left 272 6
width-height 72 25
name "slider3"
variable "greens"
min-value 0
max-value 200
current-value 0
slider-number 2
show-name? false

SLSlider
top-left 235 6
width-height 72 25
name "slider5"
variable "whites"
min-value 0
max-value 200
current-value 80
slider-number 5
show-name? false

SLSlider
top-left 196 6
width-height 72 25
name "slider4"
variable "yellows"
min-value 0
max-value 200
current-value 0
slider-number 3
show-name? false

SLButton
turtle-or-observer? observer
top-left 55 77
width-height 81 32
name "plotbutton"
line-to-run "plot-colors"
forever? true
button-number 4
show-name? false
whichip 17

SLSlider
top-left 123 6
width-height 72 25
name "slider2"
variable "blues"
min-value 0
max-value 200
current-value 200
slider-number 1
show-name? false

SLSlider
top-left 158 6
width-height 72 25
name "slider1"
variable "reds"
min-value 0
max-value 200
current-value 200
slider-number 0
show-name? false

SLSlider
top-left 98 155
width-height 95 25
name "slider5"
variable "temperature"
min-value 1
max-value 100
current-value 35
slider-number 4
show-name? false

SLSlider
top-left 167 166
width-height 78 25
name "slider6"
variable "reaction-1"
min-value 0
max-value 100
current-value 72
slider-number 6
show-name? false

SLSlider
top-left 211 166
width-height 78 25
name "slider7"
variable "reaction-2"
min-value 0
max-value 100
current-value 77
slider-number 7
show-name? false

SLMonitor
top-left 122 96
width-height 51 36
name "blues"
list-to-run "numblue"
digits 0
delay 1.0
monitor-number 1
show-name? true
whichip 22
onewhichip 23

SLMonitor
top-left 169 96
width-height 51 36
name "reds"
list-to-run "numred"
digits 0
delay 1.0
monitor-number 2
show-name? true
whichip 24
onewhichip 25

SLMonitor
top-left 216 96
width-height 51 36
name "yellows"
list-to-run "numyellow"
digits 0
delay 1.0
monitor-number 3
show-name? true
whichip 26
onewhichip 27

SLMonitor
top-left 263 96
width-height 51 36
name "whites"
list-to-run "numwhite"
digits 0
delay 0.5
monitor-number 5
show-name? true
whichip 28
onewhichip 29

SLMonitor
top-left 309 96
width-height 51 36
name "greens"
list-to-run "numgreen"
digits 0
delay 1.0
monitor-number 4
show-name? true
whichip 30
onewhichip 31

SLButton
turtle-or-observer? turtle
top-left 55 22
width-height 40 32
name "gobutton"
line-to-run "go"
forever? true
button-number 3
show-name? false
whichip 18

SLButton
turtle-or-observer? observer
top-left 311 8
width-height 74 30
name "button5"
line-to-run "draw-piechart"
forever? false
button-number 6
show-name? false
whichip 19

SLButton
turtle-or-observer? observer
top-left 365 173
width-height 51 30
name "button6"
line-to-run "go-back"
forever? false
button-number 7
show-name? false
whichip 21

SLLineWidget
top-left 349 5
width-height 125 73
id 2
order 2
type 1
title "Chemical Counts"
delay-string "1.0"
xlabel ""
ylabel ""
grid false
connected true
autoscaletrue
location 0 0
wizard-on false

SLButton
turtle-or-observer? observer
top-left 12 71
width-height 49 30
name "button4"
line-to-run "start"
forever? false
button-number 5
show-name? false
whichip 16

SLButton
turtle-or-observer? observer
top-left 12 130
width-height 49 30
name "stopitbutton"
line-to-run "stopit"
forever? false
button-number 9
show-name? false
whichip 15

SLButton
turtle-or-observer? observer
top-left 12 12
width-height 49 30
name "button1"
line-to-run "setup"
forever? false
button-number 2
show-name? false
whichip 20


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

`template`
null

`settings`
patch-size 4
num-shapes 256
screen-half-width 50
screen-half-height 50
interface-window-xcor 150
interface-window-ycor 122
interface-window-size 673 425
output-window-xcor 0
output-window-ycor 0
output-window-width 300
output-window-height 219
info-window-xcor 215
info-window-ycor 201
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`
H4sIAAAAAAAAAGNgYGBiYGDgd85Izc1MTsxRcM4vzSspBgCtDVbrFwAAAAAAABc=

`symbol table`
H4sIAAAAAAAAAGNgYGAAABzfRCEEAAAAAAAABA==

`double table`
H4sIAAAAAAAAAGNgYOBy0GMAA4coBwjt1gChvaG0CFSeAUpzQGkBCG3/AUo/gNAA
VMK+kFQAAAAAAABU

`list table`
H4sIAAAAAAAAAI2SvRICIQyEk8B5P+Noo8U9h53PY+H7l1YmZ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Gh3D5kGzJSh40pbavc1NxmYNGMQFjsHM5EDqF5If+E6AzO33mIDfYuSzeruhgL
xVgyxgHn9u73i/VdApUNt5fnoZrnA0MKuGAzgW1y9cB4DzwW/zVZr9P7B0p0wGUS
9zer7KGD8LnC9TZJbh+o0AXzOmWt/+3NO79zFEd9S6y3Ir/nd4tpuRy4zfLLkVpK
/6C+26JvdayfML2x0rZ5mxHO0DFevR8Z4VS0V5aovCepPM80i2NvrN67gjkAzf7t
IaY+YQoIU4iYBg1MQ8R0gJhGiClqYBojpkPCNCFMU8T0SDJFGqYjxHSMmB4jplkD
0wlimhOmJ4TpFDEtGubpDDHFiOkpYnrWwPQcMb0gTC8J0yvE9LqBaYmY3iCmt4jp
XQPTe8T0gTB9Q5i+RUzfNXy77xHTD4jpI2L6cY+Jlycg93cPhr/cZVl6+2k+/2OV
sDseu+2DuKbOcgEr7y8ofIpEbMnLDP9RD3fuI2eR6JWvvec7Knyr/zK8IkWJSLOM
48OwCwW/UOB5igb2agPHRSC926rYdtPQ5H9J5Wvs+YYITB26ytva9/6c3q6ydPtp
Xh5bgRyLX8rTe+yZuifm+bpDxhhoVd1mNR4ZQoAjxSlYh+NVBWpxBjpRSkPEeOAQ
hjiwOFPrgDpVjVqggxpdykT0eOwIBjhWHdJ1SH5VohYp0stSIiLHQ8fVUHXo1xF1
qxK1RId6WUpE5Hjo5H+Kt0gltREAAAAAEbU=

`observer world`
H4sIAAAAAAAAAGWNUQ6CMBBEF9CC+uMROIdfHgWbTgpJbUlZbLy962L4cZNJ5mVm
d+k6JLsgv5D7hR+M4U773Og8h8Rlch4sfCATED2P4ntqv9mMqNWLLu9pJzJkHHyG
00InXkDrFZ2E3gghFcGaWkEbVgg02izjxNg+xvVpkcUff6orPfGvD4aUA9HMAAAA
AAAAzA==

`patch world`
[encoded data, 30095 characters]
sp8+IZf99Am57KdPyGU/fUIu++kT8vb+f5Cftl+d+wQAAAT7nQ==

`turtle world`
[encoded data, 7020 characters]
145znfooO6NrX65YH2Vk/A8Wrh06bIAAAAAAgGw=

