Skip to content

Instantly share code, notes, and snippets.

@ccapndave
Created December 1, 2011 18:47
Show Gist options
  • Save ccapndave/1418913 to your computer and use it in GitHub Desktop.
Save ccapndave/1418913 to your computer and use it in GitHub Desktop.
class Pyramid extends Spine.Model
@configure "Pyramid"
@hasMany "rows", "models/row"
constructor: (rowCount) ->
throw new Error("Row count needs to be more than one") unless rowCount > 1
(@rows().create y, row) for row, y in @createRows(rowCount)
createRows: (rowCount) ->
rows = []
# Construct the bottom row
rows[rowCount - 1] = (@_random 1, 10 for x in [0...rowCount])
# Now go up the pyramid working out the rest of the numbers
getCellValue = (rowIdx, x) -> rows[rowIdx + 1][x] + rows[rowIdx + 1][x + 1]
for rowIdx in [rowCount - 2..0]
rows[rowIdx] = (getCellValue rowIdx, x for x in [0..rowIdx])
rows
class Row extends Spine.Model
@configure "Row", "y"
@hasMany "cells", "models/cell"
@belongsTo "pyramid", "Pyramid"
constructor: (@y, values) ->
(@cells().create(x, value)) for value, x in values
class Cell extends Spine.Model
@configure "Cell", "value", "x"
@belongsTo "pyramid", "Pyramid"
constructor: (@x, @value) ->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment