Commit d8ec6a9

mo_khan <mo@mokhan.ca>
2009-05-24 07:08:13
updated each heading to check the bounds of the plateau before attempting to advance one grid point.
1 parent 93ebb49
product/project/Coordinate.cs
@@ -22,6 +22,16 @@ namespace mars.rover
             coordinate--;
         }
 
+        public Coordinate plus(uint other)
+        {
+            return new Coordinate(coordinate + other);
+        }
+
+        public Coordinate minus(uint other)
+        {
+            return new Coordinate(coordinate - other);
+        }
+
         static public implicit operator Coordinate(uint coordinate)
         {
             return new Coordinate(coordinate);
product/project/East.cs
@@ -2,19 +2,27 @@ namespace mars.rover
 {
     public class East : Heading
     {
+        readonly Plateau plateau;
+
+        public East(Plateau plateau)
+        {
+            this.plateau = plateau;
+        }
+
         public Heading turn_left()
         {
-            return new North();
+            return new North(plateau);
         }
 
         public Heading turn_right()
         {
-            return new South();
+            return new South(plateau);
         }
 
         public void move_forward_from(Coordinate x, Coordinate y)
         {
-            x.increment();
+            if (plateau.within_x_axis(x.plus(1)))
+                x.increment();
         }
 
         public bool Equals(Heading other)
product/project/Headings.cs
@@ -2,9 +2,16 @@ namespace mars.rover
 {
     static public class Headings
     {
-        static public Heading North = new North();
-        static public Heading East = new East();
-        static public Heading West = new West();
-        static public Heading South = new South();
+        static public Heading North = new North(new DefaultPlateau());
+        static public Heading East = new East(new DefaultPlateau());
+        static public Heading West = new West(new DefaultPlateau());
+        static public Heading South = new South(new DefaultPlateau());
+    }
+
+    public class DefaultPlateau : Plateau
+    {
+        public DefaultPlateau() : base(5, 5)
+        {
+        }
     }
 }
\ No newline at end of file
product/project/North.cs
@@ -2,19 +2,27 @@ namespace mars.rover
 {
     public class North : Heading
     {
+        readonly Plateau plateau;
+
+        public North(Plateau plateau)
+        {
+            this.plateau = plateau;
+        }
+
         public Heading turn_left()
         {
-            return new West();
+            return new West(plateau);
         }
 
         public Heading turn_right()
         {
-            return new East();
+            return new East(plateau);
         }
 
         public void move_forward_from(Coordinate x, Coordinate y)
         {
-            y.increment();
+            if (plateau.within_y_axis(y.plus(1)))
+                y.increment();
         }
 
         public bool Equals(Heading other)
product/project/Plateau.cs
@@ -11,9 +11,14 @@ namespace mars.rover
             this.top_y_coordinate = top_y_coordinate;
         }
 
-        public virtual bool within_boundary(Coordinate x, Coordinate y)
+        public virtual bool within_x_axis(Coordinate x)
         {
-            return top_y_coordinate.CompareTo(y) >= 0 && top_x_coordinate.CompareTo(x) >= 0;
+            return top_x_coordinate.CompareTo(x) >= 0;
+        }
+
+        public virtual bool within_y_axis(Coordinate y)
+        {
+            return top_y_coordinate.CompareTo(y) >= 0;
         }
     }
 }
\ No newline at end of file
product/project/South.cs
@@ -1,22 +1,28 @@
-using System;
-
 namespace mars.rover
 {
     public class South : Heading
     {
+        readonly Plateau plateau;
+
+        public South(Plateau plateau)
+        {
+            this.plateau = plateau;
+        }
+
         public Heading turn_left()
         {
-            return new East();
+            return new East(plateau);
         }
 
         public Heading turn_right()
         {
-            return new West();
+            return new West(plateau);
         }
 
         public void move_forward_from(Coordinate x, Coordinate y)
         {
-            y.decrement();
+            if (plateau.within_y_axis(y.minus(1)))
+                y.decrement();
         }
 
         public bool Equals(Heading other)
product/project/West.cs
@@ -2,19 +2,27 @@ namespace mars.rover
 {
     public class West : Heading
     {
+        readonly Plateau plateau;
+
+        public West(Plateau plateau)
+        {
+            this.plateau = plateau;
+        }
+
         public Heading turn_left()
         {
-            return new South();
+            return new South(plateau);
         }
 
         public Heading turn_right()
         {
-            return new North();
+            return new North(plateau);
         }
 
         public void move_forward_from(Coordinate x, Coordinate y)
         {
-            x.decrement();
+            if (plateau.within_x_axis(x.minus(1)))
+                x.decrement();
         }
 
         public bool Equals(Heading other)
product/project.specifications/CoordinateSpecs.cs
@@ -14,7 +14,37 @@ namespace specifications
         it should_tell_which_is_greater = () => new Coordinate(2).CompareTo(1).should_be_greater_than(0);
         it should_tell_which_is_lower = () => new Coordinate(1).CompareTo(2).should_be_less_than(0);
         it should_tell_when_they_are_equal = () => new Coordinate(1).CompareTo(1).should_be_equal_to(0);
+    }
+
+    public class when_one_coordinate_is_added_to_another_coordinate :
+        observations_for_a_sut_without_a_contract<Coordinate>
+    {
+        it should_return_a_new_coordinate_with_the_proper_grid_point = () => result.should_be_equal_to<Coordinate>(2);
+
+        context c = () =>
+                        {
+                            uint initial = 1;
+                            provide_a_basic_sut_constructor_argument(initial);
+                        };
+
+        because b = () => { result = sut.plus(1); };
+
+        static Coordinate result;
+    }
+
+    public class when_one_coordinate_is_subtracted_from_another_coordinate :
+        observations_for_a_sut_without_a_contract<Coordinate>
+    {
+        it should_return_a_new_coordinate_with_the_proper_grid_point = () => result.should_be_equal_to<Coordinate>(0);
+
+        context c = () =>
+                        {
+                            uint initial = 1;
+                            provide_a_basic_sut_constructor_argument(initial);
+                        };
+
+        because b = () => { result = sut.minus(1); };
 
-        static int result;
+        static Coordinate result;
     }
 }
\ No newline at end of file
product/project.specifications/EastSpecs.cs
@@ -0,0 +1,37 @@
+using developwithpassion.bdd.contexts;
+using developwithpassion.bdd.mbunit;
+using developwithpassion.bdd.mbunit.standard.observations;
+using mars.rover;
+
+namespace specifications
+{
+    public class EastSpecs
+    {
+    }
+
+    public class when_at_the_most_eastern_point_on_the_plateau_and_attempting_to_move_forward_one_grid_point :
+        observations_for_a_sut_with_a_contract<Heading, East>
+    {
+        it should_not_move_from_its_current_position = () =>
+                                                           {
+                                                               y.should_be_equal_to<Coordinate>(5);
+                                                               x.should_be_equal_to<Coordinate>(5);
+                                                           };
+
+        context c = () =>
+                        {
+                            y = new Coordinate(5);
+                            x = new Coordinate(5);
+                        };
+
+        because b = () => sut.move_forward_from(x, y);
+
+        public override Heading create_sut()
+        {
+            return new East(new Plateau(5, 5));
+        }
+
+        static Coordinate y;
+        static Coordinate x;
+    }
+}
\ No newline at end of file
product/project.specifications/NorthSpecs.cs
@@ -0,0 +1,37 @@
+using developwithpassion.bdd.contexts;
+using developwithpassion.bdd.mbunit;
+using developwithpassion.bdd.mbunit.standard.observations;
+using mars.rover;
+
+namespace specifications
+{
+    public class NorthSpecs
+    {
+    }
+
+    public class when_at_the_most_northern_point_on_the_plateau_and_attempting_to_move_forward_one_grid_point :
+        observations_for_a_sut_with_a_contract<Heading, North>
+    {
+        it should_not_move_from_its_current_position = () =>
+                                                           {
+                                                               y.should_be_equal_to<Coordinate>(5);
+                                                               x.should_be_equal_to<Coordinate>(5);
+                                                           };
+
+        context c = () =>
+                        {
+                            y = new Coordinate(5);
+                            x = new Coordinate(5);
+                        };
+
+        because b = () => sut.move_forward_from(x, y);
+
+        public override Heading create_sut()
+        {
+            return new North(new Plateau(5, 5));
+        }
+
+        static Coordinate y;
+        static Coordinate x;
+    }
+}
\ No newline at end of file
product/project.specifications/PlateauSpecs.cs
@@ -9,11 +9,25 @@ namespace specifications
     {
     }
 
-    public class when_within_the_boundary : observations_for_a_sut_without_a_contract<Plateau>
+    public class when_within_the_x_boundary : observations_for_a_sut_without_a_contract<Plateau>
     {
         it should_return_true = () => result.should_be_true();
 
-        because b = () => { result = sut.within_boundary(3, 3); };
+        because b = () => { result = sut.within_x_axis(3); };
+
+        public override Plateau create_sut()
+        {
+            return new Plateau(3, 3);
+        }
+
+        static bool result;
+    }
+
+    public class when_within_the_y_boundary : observations_for_a_sut_without_a_contract<Plateau>
+    {
+        it should_return_true = () => result.should_be_true();
+
+        because b = () => { result = sut.within_y_axis(3); };
 
         public override Plateau create_sut()
         {
@@ -27,7 +41,7 @@ namespace specifications
     {
         it should_return_false = () => result.should_be_false();
 
-        because b = () => { result = sut.within_boundary(3, 4); };
+        because b = () => { result = sut.within_y_axis(4); };
 
         public override Plateau create_sut()
         {
@@ -41,7 +55,7 @@ namespace specifications
     {
         it should_return_false = () => result.should_be_false();
 
-        because b = () => { result = sut.within_boundary(3, 4); };
+        because b = () => { result = sut.within_x_axis(4); };
 
         public override Plateau create_sut()
         {
product/project.specifications/project.specifications.csproj
@@ -66,6 +66,8 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="CoordinateSpecs.cs" />
+    <Compile Include="EastSpecs.cs" />
+    <Compile Include="NorthSpecs.cs" />
     <Compile Include="PlateauSpecs.cs" />
     <Compile Include="Class1.cs" />
     <Compile Include="NASAPresenterSpecs.cs" />
@@ -74,6 +76,8 @@
     <Compile Include="ProgramSpecs.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="RoverSpecs.cs" />
+    <Compile Include="SouthSpecs.cs" />
+    <Compile Include="WestSpecs.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\project\project.csproj">
product/project.specifications/SouthSpecs.cs
@@ -0,0 +1,37 @@
+using developwithpassion.bdd.contexts;
+using developwithpassion.bdd.mbunit;
+using developwithpassion.bdd.mbunit.standard.observations;
+using mars.rover;
+
+namespace specifications
+{
+    public class SouthSpecs
+    {
+        
+    }
+    public class when_at_the_most_southern_point_on_the_plateau_and_attempting_to_move_forward_one_grid_point :
+        observations_for_a_sut_with_a_contract<Heading, South>
+    {
+        it should_not_move_from_its_current_position = () =>
+                                                           {
+                                                               y.should_be_equal_to<Coordinate>(0);
+                                                               x.should_be_equal_to<Coordinate>(0);
+                                                           };
+
+        context c = () =>
+                        {
+                            y = new Coordinate(0);
+                            x = new Coordinate(0);
+                        };
+
+        because b = () => sut.move_forward_from(x, y);
+
+        public override Heading create_sut()
+        {
+            return new South(new Plateau(5, 5));
+        }
+
+        static Coordinate y;
+        static Coordinate x;
+    }
+}
\ No newline at end of file
product/project.specifications/WestSpecs.cs
@@ -0,0 +1,37 @@
+using developwithpassion.bdd.contexts;
+using developwithpassion.bdd.mbunit;
+using developwithpassion.bdd.mbunit.standard.observations;
+using mars.rover;
+
+namespace specifications
+{
+    public class WestSpecs
+    {
+    }
+
+    public class when_at_the_most_western_point_on_the_plateau_and_attempting_to_move_forward_one_grid_point :
+        observations_for_a_sut_with_a_contract<Heading, West>
+    {
+        it should_not_move_from_its_current_position = () =>
+                                                           {
+                                                               y.should_be_equal_to<Coordinate>(0);
+                                                               x.should_be_equal_to<Coordinate>(0);
+                                                           };
+
+        context c = () =>
+                        {
+                            y = new Coordinate(0);
+                            x = new Coordinate(0);
+                        };
+
+        because b = () => sut.move_forward_from(x, y);
+
+        public override Heading create_sut()
+        {
+            return new West(new Plateau(5, 5));
+        }
+
+        static Coordinate y;
+        static Coordinate x;
+    }
+}
\ No newline at end of file