Skip to content

Instantly share code, notes, and snippets.

@valeriofarias
Created July 1, 2010 22:20
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 valeriofarias/460651 to your computer and use it in GitHub Desktop.
Save valeriofarias/460651 to your computer and use it in GitHub Desktop.
My solution for RPCFN #11: The Game of Life
require 'rubygems'
class GameOfLife
attr_accessor :state, :tmp_state
def initialize(size)
@size = size
@state = Array.new(@size){ Array.new @size }
@size.times{ |row| @size.times{ |column| @state[row][column] = rand(2) } }
@tmp_state = @state.clone
end
def evolve
@state.each_index do |x|
@state[x].each_index do |y|
minus_x = x - 1
minus_y = y - 1
plus_x = x + 1
plus_y = y + 1
plus_y = 0 if plus_y >= @size
plus_x = 0 if plus_x >= @size
live_neighbours = 0
live_neighbours += 1 if @state[minus_x][minus_y] == 1
live_neighbours += 1 if @state[minus_x][y] == 1
live_neighbours += 1 if @state[minus_x][plus_y] == 1
live_neighbours += 1 if @state[x][minus_y] == 1
live_neighbours += 1 if @state[x][plus_y] == 1
live_neighbours += 1 if @state[plus_x][minus_y] == 1
live_neighbours += 1 if @state[plus_x][y] == 1
live_neighbours += 1 if @state[plus_x][plus_y] == 1
case live_neighbours
when 0..1 then @tmp_state[x][y] = 0
when 3 then @tmp_state[x][y] = 1
when 4..90 then @tmp_state[x][y] = 0
end
end
end
@state = @tmp_state.clone
end
end
require File.join(File.dirname(__FILE__), 'game_of_life')
require 'rubygems'
require 'test/unit'
class GameOfLifeTest < Test::Unit::TestCase
def setup
@game = GameOfLife.new(3)
end
def test_should_kill_with_no_neighbours
@game.state = [[1,0,0],[0,0,0],[0,0,0]]
@game.tmp_state = [[1,0,0],[0,0,0],[0,0,0]]
after = @game.evolve
assert_equal after[0][0], 0
end
def test_should_kill_with_just_one_neighbour
@game.state = [[0,0,0],[1,0,0],[1,0,0]]
@game.tmp_state = [[0,0,0],[1,0,0],[1,0,0]]
after = @game.evolve
assert_equal after[1][0], 0
assert_equal after[2][0], 0
end
def test_should_kill_with_more_than_3_neighbours
@game.state = [[1,1,1],[1,1,1],[1,1,1]]
@game.tmp_state = [[1,1,1],[1,1,1],[1,1,1]]
after = @game.evolve
assert_equal after, [[0,0,0],[0,0,0],[0,0,0]]
end
def test_should_give_birth_if_3_neighbours
@game.state = [[1,0,0],[1,1,0],[0,0,0]]
@game.tmp_state = [[1,0,0],[1,1,0],[0,0,0]]
after = @game.evolve
assert_equal after, [[1,1,1],[1,1,1],[1,1,1]]
end
def test_should_keep_alive_if_2_neighbours
@game2 = GameOfLife.new(4)
@game2.state = [[0,0,0,0],[0,0,1,0],[0,1,1,0],[0,0,0,0]]
@game2.tmp_state = [[0,0,0,0],[0,0,1,0],[0,1,1,0],[0,0,0,0]]
after = @game2.evolve
assert_equal after[1][2], 1
assert_equal after[2][1], 1
assert_equal after[2][2], 1
assert_equal after, [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]
end
def test_should_initiate_with_rand_numbers_0_or_1
group1 = @game.state.clone
@game = GameOfLife.new(3)
group2 = @game.state.clone
assert_not_equal group1, group2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment