Commit 7cb159f

mo k <mo@mokhan.ca>
2012-11-05 04:48:56
replace public :location and :heading accessors.
1 parent b2b7c87
lib/location.rb
@@ -1,6 +1,4 @@
 class Location
-  attr_reader :location, :heading
-
   def initialize(x, y, heading)
     @location = {:x => x, :y => y}
     @heading = heading
@@ -18,6 +16,14 @@ class Location
     @heading.represents? direction
   end
 
+  def current(axis)
+    return @location[axis]
+  end
+
+  def move_to(axis, coordinate)
+    @location[axis] = coordinate
+  end
+
   def to_s
     "#{@location[:x]} #{@location[:y]} #{@heading}"
   end
lib/plateau.rb
@@ -19,20 +19,20 @@ class Plateau
   end
 
   def increment(axis, location)
-    next_location = location.location[axis] + 1
-    if(next_location > @map[axis])
-      location.location[axis] = 0
+    next_position = location.current(axis) + 1
+    if(next_position > @map[axis])
+      location.move_to(axis, 0)
     else
-      location.location[axis] = next_location
+      location.move_to(axis, next_position)
     end
   end
 
   def decrement(axis, location)
-    next_location = location.location[axis] - 1
-    if (next_location < 0)
-      location.location[axis] = @map[axis]
+    next_position = location.current(axis) - 1
+    if (next_position < 0)
+      location.move_to(axis, @map[axis])
     else
-      location.location[axis] = next_location
+      location.move_to(axis, next_position)
     end
   end
 
spec/unit/east_spec.rb
@@ -6,7 +6,7 @@ describe East do
   context "when moving forward" do
     let(:plateau) { Plateau.new(5, 5) }
     it "should move to the next position" do
-      @location.location[:x].should == 1
+      @location.current(:x).should == 1
     end
     before do
       @location = Location.new(0, 0, sut)
spec/unit/north_spec.rb
@@ -5,7 +5,7 @@ describe North do
   context "when moving forward" do
     let(:plateau) { Plateau.new(5, 5) }
     it "should move to the next position" do
-      @location.location[:y].should == 4
+      @location.current(:y).should == 4
     end
     before do
       @location = Location.new(0, 3, sut)
spec/unit/plateau_spec.rb
@@ -6,8 +6,8 @@ describe Plateau do
   context "when moving forward" do
     context "when the next position is to far east" do
       it "should move to the other side of the board" do
-        @location.location[:x].should == 0
-        @location.location[:y].should == 0
+        @location.current(:x).should == 0
+        @location.current(:y).should == 0
       end
       before do
         @location = Location.new(3, 0, East.new)
@@ -17,8 +17,8 @@ describe Plateau do
 
     context "when the next position is to far west" do
       it "should move to the other side of the board" do
-        @location.location[:x].should == 3
-        @location.location[:y].should == 0
+        @location.current(:x).should == 3
+        @location.current(:y).should == 0
       end
       before do
         @location = Location.new(0, 0, West.new)
@@ -28,8 +28,8 @@ describe Plateau do
 
     context "when the next position is to far north" do
       it "should move to the other side of the board" do
-        @location.location[:x].should == 0
-        @location.location[:y].should == 0
+        @location.current(:x).should == 0
+        @location.current(:y).should == 0
       end
       before do
         @location = Location.new(0, 3, North.new)
@@ -39,8 +39,8 @@ describe Plateau do
 
     context "when the next position is to far south" do
       it "should move to the other side of the board" do
-        @location.location[:x].should == 0
-        @location.location[:y].should == 3
+        @location.current(:x).should == 0
+        @location.current(:y).should == 3
       end
       before do
         @location = Location.new( 0,  0, South.new)
@@ -50,8 +50,8 @@ describe Plateau do
 
     context "when the next position is just fine" do
       it "should move position forward" do
-        @location.location[:x].should == 1
-        @location.location[:y].should == 1
+        @location.current(:x).should == 1
+        @location.current(:y).should == 1
       end
       before do
         @location = Location.new( 1,  0, North.new)
spec/unit/south_spec.rb
@@ -5,7 +5,7 @@ describe South do
   context "when driving forward" do
     let(:plateau) { Plateau.new(5, 5) }
     it "should move forward" do
-      @location.location[:y].should == 0
+      @location.current(:y).should == 0
     end
     before do
       @location = Location.new(0, 1, sut)
spec/unit/west_spec.rb
@@ -6,7 +6,7 @@ describe West do
   context "when moving forward" do
     let(:plateau) { Plateau.new(5, 5) }
     it "should move to the next position" do
-      @location.location[:x].should == 0
+      @location.current(:x).should == 0
     end
     before do
       @location = Location.new(1, 0, sut)