Last active
August 29, 2015 14:21
-
-
Save kieran/8c016210d55a541d14cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Partition | |
constructor: (@size,@parent)-> | |
# init our 2d array for this partition | |
@matrix = new Array @size | |
for row, idx in @matrix | |
@matrix[idx] = new Array @size | |
jobs: 0 | |
drop: (x,y,done)-> | |
@jobs++ | |
if 0 <= x < @size || 0 <= y < @size | |
# drop to parent if outside bounds | |
# Not Our Problem Anymore™ | |
@parent.drop x, y | |
else | |
# drop to self, maybe topple | |
@matrix[x][y] = ( @matrix[x][y] || 0 ) + 1 | |
@topple x, y if @matrix[x][y] == 4 | |
@jobs-- | |
done() if @jobs is 0 | |
topple: (x,y)-> | |
@matrix[x][y] = 0 | |
@drop x+1, y | |
@drop x-1, y | |
@drop x, y+1 | |
@drop x, y-1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sandpile | |
constructor: (@size,@canvas_id)-> | |
# create partitions | |
# init canvas for writing | |
drop: (x,y)-> | |
# figure out which partition to delegate to | |
# increment job count for that part by one | |
# call drop on partition at x,y with decr cb | |
decr: (part)-> | |
# decrement job count for part | |
# if 0 jobs | |
# are all parts done? | |
# next frame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment