Commit 976c985
Changed files (8)
spec
integration
unit
lib/east.rb
@@ -5,6 +5,9 @@ class East
def turn_left
North.new
end
+ def turn(degrees)
+ degrees > 0 ? turn_right : turn_left
+ end
def forward(location)
location[:x] = location[:x].to_i + 1
end
lib/north.rb
@@ -5,6 +5,9 @@ class North
def turn_left
West.new
end
+ def turn(degrees)
+ degrees > 0 ? turn_right : turn_left
+ end
def forward(location)
location[:y] = location[:y].to_i + 1
end
lib/rover.rb
@@ -8,11 +8,8 @@ class Rover
def heading
@heading.class.name.downcase.to_sym
end
- def turn_right
- @heading = @heading.turn_right
- end
- def turn_left
- @heading = @heading.turn_left
+ def turn(direction)
+ @heading = direction.turn_from(@heading)
end
def forward
@plateau.move_forward(@heading, @location)
@@ -21,4 +18,3 @@ class Rover
"#{@location[:x]} #{@location[:y]} #{@heading}"
end
end
-
lib/south.rb
@@ -5,6 +5,9 @@ class South
def turn_left
East.new
end
+ def turn(degrees)
+ degrees > 0 ? turn_right : turn_left
+ end
def forward(location)
location[:y] = location[:y].to_i - 1
end
lib/west.rb
@@ -5,6 +5,9 @@ class West
def turn_left
South.new
end
+ def turn(degrees)
+ degrees > 0 ? turn_right : turn_left
+ end
def forward(current_location)
current_location[:x] = current_location[:x].to_i - 1
end
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
+ sut.turn(Right.new)
sut.heading.should == :east
end
end
context "when turning left" do
it "should face west" do
- sut.turn_left
+ sut.turn(Left.new)
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
+ sut.turn(Right.new)
sut.heading.should == :west
end
end
context "when turning left" do
it "should face east" do
- sut.turn_left
+ sut.turn(Left.new)
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
+ sut.turn(Right.new)
sut.heading.should == :south
end
end
context "when turning left" do
it "should face north" do
- sut.turn_left
+ sut.turn(Left.new)
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
+ sut.turn(Right.new)
sut.heading.should == :north
end
end
context "when turning left" do
it "should face south" do
- sut.turn_left
+ sut.turn(Left.new)
sut.heading.should == :south
end
end