Created
November 16, 2019 15:32
-
-
Save DivineDominion/f325d5b8aa459d704e322f755a4fba23 to your computer and use it in GitHub Desktop.
A weird implementation idea for Game of Life
This file contains hidden or 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
#!/usr/bin/env ruby | |
require "rspec" | |
class God | |
def kill!(cell) | |
cell.sacrifice_to self | |
end | |
def surrender(process) | |
process.call | |
end | |
end | |
class Cell | |
attr_reader :liveliness | |
def initialize(liveliness) | |
@liveliness = liveliness | |
end | |
def sacrifice_to(god) | |
raise unless god.is_a?(God) | |
seppuku = ->() { | |
@liveliness = :dead | |
} | |
god.surrender(seppuku) | |
end | |
def die?(neighbor_amount) | |
return neighbor_amount != 2 && neighbor_amount != 3 | |
end | |
end | |
describe "Game of Life" do | |
describe "A fair God" do | |
subject(:god) { God.new } | |
describe "#kill!" do | |
subject(:cell) { Cell.new(:alive) } | |
it "sacrifices cell to self via killing" do | |
god.kill!(cell) | |
expect(cell.liveliness).to eq(:dead) | |
end | |
end | |
describe "#surrender" do | |
it "calls the parameter" do | |
did_call = false | |
process = ->() { did_call = true } | |
god.surrender(process) | |
expect(did_call).to eq(true) | |
end | |
end | |
end | |
describe "Cell" do | |
context "is alive" do | |
subject(:cell) { Cell.new(:alive) } | |
{ | |
0 => true, | |
1 => true, | |
2 => false, | |
3 => false, | |
4 => true, | |
5 => true, | |
6 => true, | |
7 => true, | |
8 => true | |
}.each do |neighbor_amount, dies| | |
context "has #{neighbor_amount} neighbors" do | |
it (dies ? "dies" : "stays alive") do | |
expect(cell.die?(neighbor_amount)).to eq(dies) | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment