Created
September 17, 2010 07:16
-
-
Save yannlugrin/583865 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
class LifeGame | |
attr_reader :matrix | |
def initialize(string) | |
@matrix = string_to_matrix(string) | |
end | |
def iterate | |
old_matrix = Marshal.load(Marshal.dump(matrix)) | |
matrix.each_index do |y| | |
matrix[y].each_index do |x| | |
matrix[y][x] = alive?(old_matrix, x, y) | |
end | |
end | |
self | |
end | |
def to_s | |
matrix.map {|line| line.map {|el| el ? '1' : '0' }.join(' ') }.join("\n") | |
end | |
private | |
def string_to_matrix(string) | |
string.split("\n").map{|line| line.split(" ").map {|el| el.to_b} } | |
end | |
def alive?(matrix, x, y) | |
count = Move.new(matrix, x, y).up.left.down.down.right.right.up.up.count | |
(matrix[y][x] && count == 2) || count == 3 | |
end | |
end | |
class Move | |
attr_reader :count, :x, :y | |
def initialize(matrix, x, y) | |
@count = 0 | |
@matrix, @x, @y = matrix, x, y | |
end | |
def up | |
@y = @y == 0 ? @matrix.length - 1 : @y - 1 | |
incr | |
end | |
def left | |
@x = @x == 0 ? @matrix[@y].length - 1 : @x - 1 | |
incr | |
end | |
def down | |
@y = @y == @matrix.length - 1 ? 0 : @y + 1 | |
incr | |
end | |
def right | |
@x = @x == @matrix[@y].length - 1 ? 0 : @x + 1 | |
incr | |
end | |
def incr | |
@count += 1 if @matrix[@y][@x] | |
self | |
end | |
end | |
class String | |
def to_b | |
['true', '1'].include?(self) | |
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 'life_game' | |
l = LifeGame.new "0 1 0 0 0 1\n0 1 0 1 0 1\n0 0 0 1 0 1\n0 1 1 1 0 0" | |
puts l | |
puts '-----------' | |
puts l.iterate | |
puts '-----------' | |
puts l.iterate | |
puts '-----------' | |
puts l.iterate |
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 'rspec' | |
require 'life_game' | |
describe LifeGame do | |
it "should parse a string" do | |
game = LifeGame.new(valid_string) | |
game.matrix.should be_kind_of Array | |
game.matrix.first.should be_kind_of Array | |
game.matrix.first.first.should be_false | |
game.matrix.first[1].should be_true | |
end | |
def valid_string | |
"0 1 0 1 0 1\n0 1 0 1 0 1\n0 1 0 1 0 1" | |
end | |
end | |
describe Move do | |
context 'center' do | |
it 'should move up' do | |
Move.new(valid_matrix, 1, 1).up.x == 1 | |
Move.new(valid_matrix, 1, 1).up.y == 0 | |
end | |
it 'should move down' do | |
Move.new(valid_matrix, 1, 1).down.x == 1 | |
Move.new(valid_matrix, 1, 1).down.y == 2 | |
end | |
it 'should move right' do | |
Move.new(valid_matrix, 1, 1).right.x == 2 | |
Move.new(valid_matrix, 1, 1).right.y == 1 | |
end | |
it 'should move left' do | |
Move.new(valid_matrix, 1, 1).left.x == 0 | |
Move.new(valid_matrix, 1, 1).left.y == 1 | |
end | |
end | |
context 'top left' do | |
it 'should move up' do | |
Move.new(valid_matrix, 0, 0).up.x == 0 | |
Move.new(valid_matrix, 0, 0).up.y == 2 | |
end | |
it 'should move left' do | |
Move.new(valid_matrix, 0, 0).up.x == 2 | |
Move.new(valid_matrix, 0, 0).up.y == 0 | |
end | |
end | |
context 'bottom right' do | |
it 'should move down' do | |
Move.new(valid_matrix, 2, 2).down.x == 2 | |
Move.new(valid_matrix, 2, 2).down.y == 0 | |
end | |
it 'should move right' do | |
Move.new(valid_matrix, 2, 2).right.x == 0 | |
Move.new(valid_matrix, 2, 2).right.y == 2 | |
end | |
end | |
context 'count' do | |
it 'should equal to zero after initializatzion' do | |
Move.new(valid_matrix, 1, 1).count.should == 0 | |
end | |
it 'should be incr when move up if new position is set to true' do | |
Move.new(valid_matrix, 0, 1).up.count.should == 1 | |
end | |
it 'should not be incr when move up if new position is set to false' do | |
Move.new(valid_matrix, 1, 1).up.count.should == 0 | |
end | |
it 'should be incr when move down if new position is set to true' do | |
Move.new(valid_matrix, 1, 1).down.count.should == 1 | |
end | |
it 'should not be incr when move down if new position is set to false' do | |
Move.new(valid_matrix, 1, 2).down.count.should == 0 | |
end | |
it 'should be incr when move right if new position is set to true' do | |
Move.new(valid_matrix, 1, 1).right.count.should == 1 | |
end | |
it 'should not be incr when move right if new position is set to false' do | |
Move.new(valid_matrix, 0, 0).right.count.should == 0 | |
end | |
it 'should be incr when move left if new position is set to true' do | |
Move.new(valid_matrix, 1, 1).left.count.should == 1 | |
end | |
it 'should not be incr when move left if new position is set to false' do | |
Move.new(valid_matrix, 2, 0).left.count.should == 0 | |
end | |
end | |
def valid_matrix | |
[ | |
[true, false, true], | |
[true, true, true], | |
[true, true, true] | |
] | |
end | |
end | |
describe String do | |
it 'should be true' do | |
"1".to_b.should be_true | |
end | |
it 'should be false' do | |
"0".to_b.should be_false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment