Commits

Bryan Head  committed f1915c7 Draft

Added sandpiles example.

  • Participants
  • Parent commits 39165fb

Comments (0)

Files changed (1)

File examples.coffee

-window.examples = {}
-window.examples["flocking"]="""
+@examples = {}
+@examples["flocking"]="""
 world.make_square 70
 world.patch_size=10
 
     @rt turn
 """
 
-examples["tutorial"] = """
+@examples["tutorial"] = """
 world.make_square 32
 world.patch_size = 13
 world.tickrate = 10
 window.regrow_grass = ->
   ask patches, -> if random(100) < 3 then @set_pcolor "green"
 """
+
+@examples["avalanche"] =
+"""
+world.make_square 60
+world.patch_size=10
+
+Patch::sand = 4
+Patch::stable = true
+
+window.setup = ->
+  @world.clear_patches()
+  ask patches, ->@sand=4
+  ask patches, ->@border = not (world.min_pxcor<@pxcor<world.max_pxcor and world.min_pycor<@pycor<world.max_pycor)
+  ask patches, ->@update_color()
+
+window.go = ->
+  ask patches, ->@get_stability()
+  ask patches, ->if (not @stable) and (not @border) then @fire()
+  ask patches, ->if @border then @sand = 0
+  ask patches, ->@update_color()
+  
+Patch::get_stability = -> @stable = @sand<=3
+
+Patch::fire = ->
+  @sand -= 4
+  patch(@pxcor+1, @pycor+1).sand+=1
+  patch(@pxcor-1, @pycor+1).sand+=1
+  patch(@pxcor-1, @pycor-1).sand+=1
+  patch(@pxcor+1, @pycor-1).sand+=1
+
+Patch::update_color = ->
+  if @border
+    @set_pcolor "black"
+  else
+    @set_pcolor "hsl("+@sand*60+",100%,50%)"
+    #@set_pcolor "hsl(0,0%,"+@sand*15+"%)"
+
+# Dumps n grains of sand randomly across the board
+dump_random = (n) ->
+  for i in [1..n]
+    patches[random(patches.length)].sand+=1
+"""