Commit d68d82e
Changed files (7)
bin/gof
@@ -0,0 +1,3 @@
+#!/bin/env ruby
+
+game = Game.new
lib/cell.rb
@@ -1,8 +1,9 @@
class Cell
- attr_accessor :x, :y
+ attr_reader :x, :y
- def initialize(populated: false)
+ def initialize(populated: false, x: 1, y: 1)
@populated = populated
+ @x, @y = x, y
end
def spawn(world)
lib/game.rb
@@ -0,0 +1,18 @@
+class Game
+ def initialize(printer)
+ end
+
+ def play(max_x, max_y)
+ World.new
+ end
+
+ def create_cells(max_x, max_y)
+ items = []
+ (1..max_x).each do |x|
+ (1..max_y).each do |y|
+ items.push(Cell.new(x: x, y: y))
+ end
+ end
+ items
+ end
+end
spec/lib/cell_spec.rb
@@ -112,10 +112,7 @@ describe Cell do
end
def create_cell(x, y)
- Cell.new.tap do |cell|
- cell.x = x
- cell.y = y
- end
+ Cell.new(x: x, y: y)
end
end
end
spec/lib/game_spec.rb
@@ -0,0 +1,36 @@
+describe Game do
+ subject { Game.new(printer) }
+ let(:printer) { double }
+
+ xcontext "#start" do
+ it "prints the output of the world" do
+ subject.play(3, 3)
+ expect(world).to have_received(:print_each_using).with(printer)
+ end
+ end
+
+ context "#create_cells" do
+ context "for a 2x2 grid" do
+ let(:results) { subject.create_cells(2, 2) }
+
+ it "returns the correct number of cells" do
+ expect(results.count).to eql(4)
+ end
+
+ it "returns a cell for each coordinate" do
+ expect(find(1, 1).x).to eql(1)
+ expect(find(1, 1).y).to eql(1)
+ expect(find(1, 2).x).to eql(1)
+ expect(find(1, 2).y).to eql(2)
+ expect(find(2, 1).x).to eql(2)
+ expect(find(2, 1).y).to eql(1)
+ expect(find(2, 2).x).to eql(2)
+ expect(find(2, 2).y).to eql(2)
+ end
+
+ def find(x, y)
+ results.find { |cell| cell.x == x && cell.y == y }
+ end
+ end
+ end
+end
spec/lib/world_spec.rb
@@ -1,8 +1,8 @@
describe World do
subject { World.new(cells) }
let(:cells) { [neighbor, other_cell] }
- let(:neighbor) { Object.new }
- let(:other_cell) { Object.new }
+ let(:neighbor) { double }
+ let(:other_cell) { double }
context "#neighbors_for" do
let(:cell) { Object.new }
spec/spec_helper.rb
@@ -16,6 +16,7 @@
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require "world"
require "cell"
+require 'game'
RSpec.configure do |config|
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.