Permalink
Browse files

resize_to

  • Loading branch information...
1 parent 4a7033d commit a1ab48c0465f513c46e9a069e9f1a120b198f1a1 @raganwald committed Feb 5, 2012
Showing with 64 additions and 1 deletion.
  1. +11 −1 lib/cafeaulife.coffee
  2. +53 −0 spec/time_and_space.spec.coffee
View
@@ -106,7 +106,7 @@ class Cell
toString: ->
'' + @hash
-# The two canonical cells. No more should ever be created. In C++ terms, `new` is private.
+# The two canonical cells.
Cell.Alive = new Cell(1)
Cell.Dead = new Cell(0)
@@ -296,6 +296,16 @@ class Square
sw: empty_quadrant
.inflate_by(extant - 1)
+ # Resize to a given level
+ resize_to: (level) ->
+ this_level = @level()
+ if level > this_level
+ @inflate_by(level - this_level)
+ else if level < this_level
+ @deflate_by(this_level - level)
+ else
+ this
+
# ### The Speed of Light
#
# In Life, the "Speed of Light" or "*c*" is one cell vertically, horizontally, or diagonally in any direction. Meaning, that cause and effect cannot travel faster than *c*.
@@ -0,0 +1,53 @@
+_ = require('underscore')
+require 'UnderscoreMatchersForJasmine'
+
+Life = require('../lib/cafeaulife')
+
+describe 'time and space', ->
+
+ beforeEach ->
+ Life.generate_seeds_from_rule()
+
+ describe 'space', ->
+
+ it 'should not change an r_pentomino from level 2', ->
+
+ expect( (Life.Square.find_or_create [
+ [0, 0, 0, 0]
+ [0, 1, 0, 0]
+ [1, 1, 1, 0]
+ [0, 0, 1, 0]
+ ]).resize_to(2) ).toEqual( Life.Square.find_or_create [
+ [0, 0, 0, 0]
+ [0, 1, 0, 0]
+ [1, 1, 1, 0]
+ [0, 0, 1, 0]
+ ] )
+
+ it 'should downsize an r_pentomino to level 1', ->
+
+ expect( (Life.Square.find_or_create [
+ [0, 0, 0, 0]
+ [0, 1, 0, 0]
+ [1, 1, 1, 0]
+ [0, 0, 1, 0]
+ ]).resize_to(1) ).toEqual( Life.Square.find_or_create [
+ [1, 0]
+ [1, 1]
+ ] )
+
+ it 'should upsize an r_pentomino to level 3', ->
+
+ expect( (Life.Square.find_or_create [
+ [1, 0]
+ [0, 1]
+ ]).resize_to(3) ).toEqual( Life.Square.find_or_create [
+ [0, 0, 0, 0, 0, 0, 0, 0]
+ [0, 0, 0, 0, 0, 0, 0, 0]
+ [0, 0, 0, 0, 0, 0, 0, 0]
+ [0, 0, 0, 1, 0, 0, 0, 0]
+ [0, 0, 0, 0, 1, 0, 0, 0]
+ [0, 0, 0, 0, 0, 0, 0, 0]
+ [0, 0, 0, 0, 0, 0, 0, 0]
+ [0, 0, 0, 0, 0, 0, 0, 0]
+ ] )

0 comments on commit a1ab48c

Please sign in to comment.