-
-
Save QaDeS/2fefaf84664908c32f41 to your computer and use it in GitHub Desktop.
This file contains 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
# Provides a Game Of Life playfield. Initialize with the desired size | |
# and call +evolve+ to advance the game. You can always access the +state+ | |
# attribute to set or get the current game state. | |
class GameOfLife | |
# The current game state. | |
attr_accessor :state | |
# Initializes a playfield of the given size. | |
def initialize(width, height = width) | |
@state = Array.new(height) { Array.new(width) } | |
end | |
# Performs an evolution step on the playfield, applying the game rules: | |
# live cell has 2 neighbours -> stays alive | |
# dead cell has 3 neighbours -> a new cell gets born | |
# all other cases -> the cell dies or stays dead | |
def evolve | |
new_state = [] | |
state.each_with_index do |line, y| | |
new_line = [] | |
line.each_with_index do |current, x| | |
new_line << case neighbours(x, y) | |
when 2 # no change to alive state | |
current | |
when 3 # a new cell gets born | |
1 | |
else # too little or too many neighbours -> the cell dies | |
0 | |
end | |
end | |
new_state << new_line | |
end | |
@state = new_state | |
end | |
# Returns the number of alive neighbors for a given cell. | |
def neighbours(x, y) | |
result = -state[y][x] # the current cell is not to be counted as a neighbour | |
# now sum up all the alive cells in the patch | |
(y-1..y+1).each do |py| | |
line = state[py % state.size] | |
(x-1..x+1).each do |px| | |
result += line[px % line.size] # just add the alive state | |
end | |
end | |
result | |
end | |
end |
This file contains 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
require 'game_of_life' | |
describe GameOfLife do | |
before(:each) do | |
@game = GameOfLife.new(3) | |
end | |
it "should have a 3 x 3 playfield after initialization" do | |
@game.state.size.should == 3 | |
@game.state.each do |line| | |
line.size.should == 3 | |
end | |
end | |
it "should kill with no neighbours" do | |
@game.state = [ | |
[1,0,0], | |
[0,0,0], | |
[0,0,0]] | |
after = @game.evolve | |
after.should == [ | |
[0,0,0], | |
[0,0,0], | |
[0,0,0]] | |
end | |
it "should kill with just one neighbour" do | |
@game.state = [ | |
[0,0,0], | |
[1,0,0], | |
[1,0,0]] | |
after = @game.evolve | |
after.should == [ | |
[0,0,0], | |
[0,0,0], | |
[0,0,0]] | |
end | |
it "should kill with more than 3 neighbours" do | |
@game.state = [ | |
[1,1,1], | |
[1,1,1], | |
[1,1,1]] | |
after = @game.evolve | |
after.should == [ | |
[0,0,0], | |
[0,0,0], | |
[0,0,0]] | |
end | |
it "should give birth if 3 neighbours" do | |
@game.state = [ | |
[1,0,0], | |
[1,1,0], | |
[0,0,0]] | |
after = @game.evolve | |
after.should == [ | |
[1,1,1], | |
[1,1,1], | |
[1,1,1]] | |
end | |
it "should stay dead if 2 neighbours" do | |
@game.state = [ | |
[0,0,0], | |
[1,0,1], | |
[0,0,0]] | |
after = @game.evolve | |
after.should == [ | |
[0,0,0], | |
[0,0,0], | |
[0,0,0]] | |
end | |
it "should stay alife if 2 neighbours" do | |
@game.state = [ | |
[0,0,0], | |
[1,1,1], | |
[0,0,0]] | |
after = @game.evolve | |
after.should == [ | |
[1,1,1], | |
[1,1,1], | |
[1,1,1]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment