Commit d5c9457
Changed files (10)
lib/east.rb
@@ -5,7 +5,7 @@ class East
def turn_left
North.new
end
- def turn(degrees)
+ def rotate(degrees)
degrees > 0 ? turn_right : turn_left
end
def forward(location)
lib/move_forward.rb
@@ -0,0 +1,11 @@
+class MoveForward
+ def initialize(rover)
+ @rover = rover
+ end
+ def run(instruction)
+ @rover.forward if matches(instruction)
+ end
+ def matches(item)
+ 'M' == item
+ end
+end
lib/north.rb
@@ -5,7 +5,7 @@ class North
def turn_left
West.new
end
- def turn(degrees)
+ def rotate(degrees)
degrees > 0 ? turn_right : turn_left
end
def forward(location)
lib/rover.rb
@@ -8,8 +8,8 @@ class Rover
def heading
@heading.class.name.downcase.to_sym
end
- def turn(direction)
- @heading = direction.turn_from(@heading)
+ def rotate(degrees)
+ @heading = @heading.rotate(degrees)
end
def forward
@plateau.move_forward(@heading, @location)
lib/south.rb
@@ -5,7 +5,7 @@ class South
def turn_left
East.new
end
- def turn(degrees)
+ def rotate(degrees)
degrees > 0 ? turn_right : turn_left
end
def forward(location)
lib/turn_left.rb
@@ -0,0 +1,11 @@
+class TurnLeft
+ def initialize(rover)
+ @rover = rover
+ end
+ def run(instruction)
+ @rover.rotate(-90) if matches(instruction)
+ end
+ def matches(item)
+ 'L' == item
+ end
+end
lib/turn_right.rb
@@ -0,0 +1,11 @@
+class TurnRight
+ def initialize(rover)
+ @rover = rover
+ end
+ def run(instruction)
+ @rover.rotate(90) if matches(instruction)
+ end
+ def matches(item)
+ 'R' == item
+ end
+end
lib/west.rb
@@ -5,7 +5,7 @@ class West
def turn_left
South.new
end
- def turn(degrees)
+ def rotate(degrees)
degrees > 0 ? turn_right : turn_left
end
def forward(current_location)
spec/unit/rover_spec.rb
@@ -11,13 +11,13 @@ describe Rover do
let(:sut) { create_sut :north, 0, 0 }
context "when turning right" do
it "should face east" do
- sut.turn(Right.new)
+ sut.rotate(90)
sut.heading.should == :east
end
end
context "when turning left" do
it "should face west" do
- sut.turn(Left.new)
+ sut.rotate(-90)
sut.heading.should == :west
end
end
@@ -27,13 +27,13 @@ describe Rover do
let(:sut) { create_sut :south, 0, 3 }
context "when turning right" do
it "should face west" do
- sut.turn(Right.new)
+ sut.rotate(90)
sut.heading.should == :west
end
end
context "when turning left" do
it "should face east" do
- sut.turn(Left.new)
+ sut.rotate(-90)
sut.heading.should == :east
end
end
@@ -44,13 +44,13 @@ describe Rover do
context "when turning right" do
it "should face south" do
- sut.turn(Right.new)
+ sut.rotate(90)
sut.heading.should == :south
end
end
context "when turning left" do
it "should face north" do
- sut.turn(Left.new)
+ sut.rotate(-90)
sut.heading.should == :north
end
end
@@ -61,13 +61,13 @@ describe Rover do
context "when turning right" do
it "should face north" do
- sut.turn(Right.new)
+ sut.rotate(90)
sut.heading.should == :north
end
end
context "when turning left" do
it "should face south" do
- sut.turn(Left.new)
+ sut.rotate(-90)
sut.heading.should == :south
end
end