Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Coordinates are refactored to use point primitive.

  • Loading branch information...
commit 163652afb522612223a2110f5cc8bf6800597b26 1 parent 72502c0
@joshwalsh authored
View
4 app/primitives/point.rb
@@ -5,4 +5,8 @@ def initialize(x, y)
@x = x
@y = y
end
+
+ def ==(comparison)
+ (@x == comparison.x) && (@y == comparison.y)
+ end
end
View
35 app/services/cell_in_direction.rb
@@ -3,44 +3,43 @@ def initialize(planet)
@planet = planet
end
- def left(x,y)
- if x == 0
+ def left(point)
+ if point.x == 0
new_x = @planet.width - 1
else
- new_x = x - 1
+ new_x = point.x - 1
end
- [new_x, y]
+ Point.new(new_x, point.y)
end
- def right(x,y)
- if x == (@planet.width - 1)
+ def right(point)
+ if point.x == (@planet.width - 1)
new_x = 0
else
- new_x = x + 1
+ new_x = point.x + 1
end
- [new_x, y]
+ Point.new(new_x, point.y)
end
- def down(x,y)
- if y == (@planet.height - 1)
+ def down(point)
+ if point.y == (@planet.height - 1)
new_y = 0
else
- new_y = y + 1
+ new_y = point.y + 1
end
- [x, new_y]
+ Point.new(point.x, new_y)
end
- def up(x,y)
- if y == 0
+ def up(point)
+ if point.y == 0
new_y = @planet.height - 1
else
- new_y = y - 1
- end
-
- [x, new_y]
+ new_y = point.y - 1
end
+ Point.new(point.x, new_y)
+ end
end
View
8 app/services/run_game.rb
@@ -35,10 +35,12 @@ def run_organism(organism, planet)
x = organism.x
y = organism.y
- new_coordinate = occupancy.select_random_adjacent_coordinate(x, y)
+ point = Point.new(x,y)
- occupancy.move_organism(organism, new_coordinate[0], new_coordinate[1])
+ new_coordinate = occupancy.select_random_adjacent_coordinate(point)
- @game.log "PLANET: #{planet.name}, ORGANISM: #{organism.name}, moved from #{x}, #{y} to #{organism.x}, #{organism.y}"
+ occupancy.move_organism(organism, point)
+
+ @game.log "PLANET: #{planet.name}, ORGANISM: #{organism.name}, moved from #{x}, #{y} to #{new_coordinate.x}, #{new_coordinate.y}"
end
end
View
4 features/steps/organisms_steps.rb
@@ -3,8 +3,8 @@
steps_for :organisms do
step "the system will navigate the organism at :from_x, :from_y to :to_x, :to_y" do |from_x, from_y, to_x, to_y|
@planet_occupancy = PlanetOccupancy.new @planet
- @organism = @planet_occupancy.find_by_coordinate(from_x, from_y)
+ @organism = @planet_occupancy.find_by_coordinate(Point.new(from_x, from_y))
- PlanetOccupancy.any_instance.stub(:select_random_adjacent_coordinate) { [to_x, to_y] }
+ PlanetOccupancy.any_instance.stub(:select_random_adjacent_coordinate) { Point.new(to_x, to_y) }
end
end
View
17 spec/services/cell_in_direction_spec.rb
@@ -1,5 +1,6 @@
require 'lean_spec_helper'
require 'app/services/cell_in_direction'
+require 'app/primitives/point'
describe CellInDirection do
let(:planet) { mock() }
@@ -11,34 +12,34 @@
end
it 'gets cell at the left' do
- subject.left(3, 3).should == [2,3]
+ subject.left(Point.new(3,3)).should == Point.new(2, 3)
end
it 'moves an organism to the right' do
- subject.right(3, 3).should == [4,3]
+ subject.right(Point.new(3, 3)).should == Point.new(4,3)
end
it 'moves an organism down' do
- subject.down(3, 3).should == [3,4]
+ subject.down(Point.new(3, 3)).should == Point.new(3,4)
end
it 'moves an organism up' do
- subject.up(3, 3).should == [3,2]
+ subject.up(Point.new(3, 3)).should == Point.new(3,2)
end
it 'wraps off the left side of the screen' do
- subject.left(0, 3).should == [9,3]
+ subject.left(Point.new(0, 3)).should == Point.new(9,3)
end
it 'wraps off the right side of the screen' do
- subject.right(9, 3).should == [0,3]
+ subject.right(Point.new(9, 3)).should == Point.new(0,3)
end
it 'wraps off the top of the screen' do
- subject.up(3, 0).should == [3,9]
+ subject.up(Point.new(3, 0)).should == Point.new(3,9)
end
it 'wraps off the bottom of the screen' do
- subject.down(3, 9).should == [3,0]
+ subject.down(Point.new(3, 9)).should == Point.new(3,0)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.