Skip to content

Instantly share code, notes, and snippets.

@samklr
Created December 8, 2012 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samklr/4240733 to your computer and use it in GitHub Desktop.
Save samklr/4240733 to your computer and use it in GitHub Desktop.
Code Retreat
require "test/unit"
class World
def initialize
@cells = [[false, false, true],
[false, true, false],
[false, false, false],
]
end
def countNeighbours(x, y)
count = 0
for i in (x -1)..(x + 1)
for j in (y -1)..(y+1)
if (i != x) && (j != y) && @cells[i][j]
count +=1
end
end
end
count
end
def nextState(isAlive, nbNeighbors)
return false if isAlive && (nbNeighbors < 2|| nbNeighbors >3)
return true if isAlive && (nbNeighbors ===2 || nbNeighbors === 3)
return true if !isAlive && (nbNeighbors == 3 )
end
end
class MyTest < Test::Unit::TestCase
def test_live_cell_dies_with_less_than_two_neighbors
@world = World.new
assert_equal @world.nextState(true, 1), false
end
def test_live_cell_dies_with_more_than_three_neighbors
@world = World.new
assert_equal @world.nextState(true, 4), false
end
def test_live_cell_survives_with_two_or_three_neighbors
@world = World.new
assert_equal @world.nextState(true, 3), true
end
def test_dead_cell_resuscitates_with_three_neighbors
@world = World.new
assert_equal @world.nextState(false, 3), true
end
def test_count_neighbours
@world = World.new
assert_equal @world.countNeighbours(1,1), 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment