Commit 29a6269
Changed files (12)
lib
spec
integration
unit
lib/east.rb
@@ -9,7 +9,8 @@ class East
degrees > 0 ? turn_right : turn_left
end
def forward(location)
- location[:x] = location[:x].to_i + 1
+ location[:x] = location[:x] + 1
+ location
end
def to_s
'E'
lib/move_forward.rb
@@ -3,7 +3,9 @@ class MoveForward
@rover = rover
end
def run(instruction)
- @rover.forward if matches(instruction)
+ if matches(instruction)
+ @rover.forward
+ end
end
def matches(item)
'M' == item
lib/north.rb
@@ -10,6 +10,7 @@ class North
end
def forward(location)
location[:y] = location[:y].to_i + 1
+ location
end
def to_s
'N'
lib/plateau.rb
@@ -6,7 +6,7 @@ require "south"
class Plateau
def initialize(x,y)
- @map = {:x => x, :y => y}
+ @map = {:x => x.to_i, :y => y.to_i}
@directions = {:N => North.new, :E => East.new, :W => West.new, :S => South.new}
end
@@ -31,6 +31,6 @@ class Plateau
end
def deploy_rover_to(heading, x, y)
- Rover.new(@directions[heading.to_sym], {:x => x, :y => y}, self)
+ Rover.new(@directions[heading.to_sym], x, y, self)
end
end
lib/rover.rb
@@ -1,19 +1,17 @@
class Rover
attr_reader :location
- def initialize(heading, coordinates, plateau)
+ def initialize(heading, x, y, plateau)
@heading = heading
- @location = coordinates
+ @location = {:x => x, :y => y}
@plateau = plateau
end
def heading
@heading.class.name.downcase.to_sym
end
def rotate(degrees)
- puts "rotate #{degrees}"
@heading = @heading.rotate(degrees)
end
def forward
- puts "move forward"
@plateau.move_forward(@heading, @location)
end
def to_s
lib/south.rb
@@ -9,7 +9,8 @@ class South
degrees > 0 ? turn_right : turn_left
end
def forward(location)
- location[:y] = location[:y].to_i - 1
+ location[:y] = location[:y] - 1
+ location
end
def to_s
'S'
lib/turn_left.rb
@@ -3,7 +3,9 @@ class TurnLeft
@rover = rover
end
def run(instruction)
- @rover.rotate(-90) if matches(instruction)
+ if matches(instruction)
+ @rover.rotate(-90)
+ end
end
def matches(item)
'L' == item
lib/turn_right.rb
@@ -3,7 +3,9 @@ class TurnRight
@rover = rover
end
def run(instruction)
- @rover.rotate(90) if matches(instruction)
+ if matches(instruction)
+ @rover.rotate(90)
+ end
end
def matches(item)
'R' == item
lib/west.rb
@@ -8,8 +8,9 @@ class West
def rotate(degrees)
degrees > 0 ? turn_right : turn_left
end
- def forward(current_location)
- current_location[:x] = current_location[:x].to_i - 1
+ def forward(location)
+ location[:x] = location[:x] - 1
+ location
end
def to_s
'W'
spec/unit/rover_spec.rb
@@ -3,7 +3,7 @@ require 'spec_helper'
describe Rover do
def create_sut(heading, x = 0, y = 0)
directions = {:north => North.new, :east => East.new, :west => West.new, :south => South.new}
- Rover.new(directions[heading],{ :x => x,:y => y }, plateau)
+ Rover.new(directions[heading], x, y , plateau)
end
let(:plateau) { fake }