Skip to content

Commit

Permalink
Coordinates are refactored to use point primitive.
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Walsh committed Mar 31, 2012
1 parent 72502c0 commit 163652a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
4 changes: 4 additions & 0 deletions app/primitives/point.rb
Expand Up @@ -5,4 +5,8 @@ def initialize(x, y)
@x = x
@y = y
end

def ==(comparison)
(@x == comparison.x) && (@y == comparison.y)
end
end
35 changes: 17 additions & 18 deletions app/services/cell_in_direction.rb
Expand Up @@ -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
8 changes: 5 additions & 3 deletions app/services/run_game.rb
Expand Up @@ -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
4 changes: 2 additions & 2 deletions features/steps/organisms_steps.rb
Expand Up @@ -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
17 changes: 9 additions & 8 deletions 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() }
Expand All @@ -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

0 comments on commit 163652a

Please sign in to comment.