Commit 04bdb19

mo_khan <mo@mokhan.ca>
2009-05-24 15:52:23
changed tostring implementation on headings to just return the letter it represents instead of the full name of the heading
1 parent 79e0e51
product/project/common/EnumerableExtensions.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+
+namespace mars.rover.common
+{
+    static public class EnumerableExtensions
+    {
+        static public void each<T>(this IEnumerable<T> items, Action<T> command)
+        {
+            foreach (var item in items) command(item);
+        }
+    }
+}
\ No newline at end of file
product/project/common/ParameterizedCommand.cs
@@ -2,6 +2,6 @@ namespace mars.rover.common
 {
     public interface ParameterizedCommand<T>
     {
-        void run_with(T item);
+        void run_against(T item);
     }
 }
\ No newline at end of file
product/project/common/Registry.cs
@@ -5,6 +5,5 @@ namespace mars.rover.common
     public interface Registry<T> : IEnumerable<T>
     {
         IEnumerable<T> all();
-        void Add(T item);
     }
 }
\ No newline at end of file
product/project/domain/East.cs
@@ -33,7 +33,7 @@ namespace mars.rover.domain
 
         public override string ToString()
         {
-            return "East";
+            return "E";
         }
 
         public override bool Equals(object obj)
product/project/domain/North.cs
@@ -43,7 +43,7 @@ namespace mars.rover.domain
 
         public override string ToString()
         {
-            return "North";
+            return "N";
         }
     }
 }
\ No newline at end of file
product/project/domain/South.cs
@@ -33,7 +33,7 @@ namespace mars.rover.domain
 
         public override string ToString()
         {
-            return "South";
+            return "S";
         }
 
         public override bool Equals(object obj)
product/project/domain/West.cs
@@ -33,7 +33,7 @@ namespace mars.rover.domain
 
         public override string ToString()
         {
-            return "West";
+            return "W";
         }
 
         public override bool Equals(object obj)
product/project/presentation/CaptureUserInstructionsConsoleView.cs
@@ -18,11 +18,14 @@ namespace mars.rover.presentation
             writer.WriteLine("Enter upper right coordinates:");
             presenter.provide_upper_right_coordinates(reader.ReadLine());
 
-            writer.WriteLine("enter coordinates to deploy a rover to:");
-            presenter.deploy_rover_to(reader.ReadLine());
+            for (int i = 0; i < 2; i++)
+            {
+                writer.WriteLine("enter coordinates to deploy a rover to:");
+                presenter.deploy_rover_to(reader.ReadLine());
 
-            writer.WriteLine("enter commands to navigate rover:");
-            presenter.navigate_rover_using(reader.ReadLine());
+                writer.WriteLine("enter commands to navigate rover:");
+                presenter.navigate_rover_using(reader.ReadLine());
+            }
         }
 
         public void display(uint x, uint y, string heading)
product/project/presentation/CaptureUserInstructionsPresenter.cs
@@ -41,9 +41,7 @@ namespace mars.rover.presentation
 
         public virtual void navigate_rover_using(string line)
         {
-            foreach (var command in line)
-                navigations.Single(x => x.is_satisfied_by(command)).navigate(rover);
-
+            line.each(x => navigations.Single(y => y.is_satisfied_by(x)).run_against(rover));
             view.display(rover.x, rover.y, rover.heading.ToString());
         }
 
product/project/presentation/DefaultHeadingFactory.cs
@@ -1,27 +0,0 @@
-using System;
-using mars.rover.domain;
-
-namespace mars.rover.presentation
-{
-    public class DefaultHeadingFactory : HeadingFactory
-    {
-        readonly string code;
-        readonly Func<Plateau, Heading> factory;
-
-        public DefaultHeadingFactory(string code, Func<Plateau, Heading> factory)
-        {
-            this.code = code;
-            this.factory = factory;
-        }
-
-        public virtual bool is_satisfied_by(string item)
-        {
-            return string.Equals(code, item, StringComparison.OrdinalIgnoreCase);
-        }
-
-        public virtual Heading create(Plateau plateau)
-        {
-            return factory(plateau);
-        }
-    }
-}
\ No newline at end of file
product/project/presentation/HeadingFactory.cs
@@ -1,10 +1,28 @@
+using System;
 using mars.rover.common;
 using mars.rover.domain;
 
 namespace mars.rover.presentation
 {
-    public interface HeadingFactory : Specification<string>
+    public class HeadingFactory : Specification<string>
     {
-        Heading create(Plateau plateau);
+        readonly string code;
+        readonly Func<Plateau, Heading> factory;
+
+        public HeadingFactory(string code, Func<Plateau, Heading> factory)
+        {
+            this.code = code;
+            this.factory = factory;
+        }
+
+        public virtual bool is_satisfied_by(string item)
+        {
+            return string.Equals(code, item, StringComparison.OrdinalIgnoreCase);
+        }
+
+        public virtual Heading create(Plateau plateau)
+        {
+            return factory(plateau);
+        }
     }
 }
\ No newline at end of file
product/project/presentation/Navigation.cs
@@ -4,7 +4,7 @@ using mars.rover.domain;
 
 namespace mars.rover.presentation
 {
-    public class Navigation : Specification<char>
+    public class Navigation : Specification<char>, ParameterizedCommand<Rover>
     {
         readonly char command_text;
         readonly Action<Rover> navigation;
@@ -15,7 +15,7 @@ namespace mars.rover.presentation
             this.navigation = navigation;
         }
 
-        public virtual void navigate(Rover rover)
+        public virtual void run_against(Rover rover)
         {
             navigation(rover);
         }
product/project/Program.cs
@@ -16,7 +16,7 @@ namespace mars.rover
             this.presenter = presenter;
         }
 
-        public void run_with(IEnumerable<CommandLineArgument> item)
+        public void run_against(IEnumerable<CommandLineArgument> item)
         {
             presenter.run();
         }
@@ -28,10 +28,10 @@ namespace mars.rover
                     new CaptureUserInstructionsConsoleView(Console.In, Console.Out),
                     new DefaultRegistry<HeadingFactory>
                         {
-                            new DefaultHeadingFactory("N", x => new North(x)),
-                            new DefaultHeadingFactory("E", x => new East(x)),
-                            new DefaultHeadingFactory("W", x => new West(x)),
-                            new DefaultHeadingFactory("S", x => new South(x)),
+                            new HeadingFactory("N", x => new North(x)),
+                            new HeadingFactory("E", x => new East(x)),
+                            new HeadingFactory("W", x => new West(x)),
+                            new HeadingFactory("S", x => new South(x)),
                         },
                     new DefaultRegistry<Navigation>
                         {
@@ -40,7 +40,7 @@ namespace mars.rover
                             new Navigation('M', x => x.move_forward()),
                         }
                     ));
-            program.run_with(args.Select(x => (CommandLineArgument) x));
+            program.run_against(args.Select(x => (CommandLineArgument) x));
         }
     }
 }
\ No newline at end of file
product/project/project.csproj
@@ -46,14 +46,14 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="common\DefaultRegistry.cs" />
+    <Compile Include="common\EnumerableExtensions.cs" />
     <Compile Include="common\Registry.cs" />
-    <Compile Include="presentation\DefaultHeadingFactory.cs" />
+    <Compile Include="presentation\HeadingFactory.cs" />
     <Compile Include="common\CallbackCommand.cs" />
     <Compile Include="presentation\CaptureUserInstructionsConsoleView.cs" />
     <Compile Include="presentation\CaptureUserInstructionsView.cs" />
     <Compile Include="common\Command.cs" />
     <Compile Include="presentation\CommandLineArgument.cs" />
-    <Compile Include="presentation\HeadingFactory.cs" />
     <Compile Include="presentation\infrastructure\CommandProcessor.cs" />
     <Compile Include="domain\Coordinate.cs" />
     <Compile Include="domain\East.cs" />
product/project.specifications/ProgramSpecs.cs
@@ -24,7 +24,7 @@ namespace specifications
     {
         it should_wait_for_instructions_from_nasa = () => presenter.received(x => x.run());
 
-        because b = () => sut.run_with(args);
+        because b = () => sut.run_against(args);
 
         static readonly IEnumerable<CommandLineArgument> args = new List<CommandLineArgument> {""};
     }