Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace RobotService
  6. {
  7.     public class RobotSimulator
  8.     {
  9.         public RobotSimulator(Enums.Bearing bearing, Coordinate coordinate)
  10.         {
  11.             Bearing = bearing;
  12.             Coordinate = coordinate;
  13.         }
  14.  
  15.         public Enums.Bearing Bearing { get; private set; }
  16.         public Coordinate Coordinate { get; private set; }
  17.  
  18.         public void TurnRight()
  19.         {
  20.             Bearing++;
  21.             var myEnumMemberCount = Enum.GetNames(typeof(Enums.Bearing)).Length - 1;
  22.             if ((int) Bearing > myEnumMemberCount) Bearing = Enums.Bearing.North;
  23.         }
  24.  
  25.         public void TurnLeft()
  26.         {
  27.             Bearing--;
  28.             if (Bearing < 0) Bearing = Enums.Bearing.West;
  29.         }
  30.  
  31.         public void Advance()
  32.         {
  33.             Coordinate = Coordinate + BearingAdvanceValue()[Bearing];
  34.         }
  35.  
  36.         public void Simulate(string directions)
  37.         {
  38.             foreach (var direction in directions)
  39.             {
  40.                 TranslateAndExecuteDirection(direction);
  41.             }
  42.         }
  43.  
  44.         private void TranslateAndExecuteDirection(char direction)
  45.         {
  46.             var requestedAction = RobotLanguageDic()[direction];
  47.             if (requestedAction == null)
  48.                 throw new ArgumentNullException($"direction of {direction} not found in {nameof(RobotLanguageDic)}");
  49.             requestedAction();
  50.         }
  51.  
  52.         private Dictionary<char, Action> RobotLanguageDic() => new Dictionary<char, Action>
  53.             {
  54.                 {'R', TurnRight},
  55.                 {'L', TurnLeft },
  56.                 {'A', Advance },
  57.             };
  58.  
  59.  
  60.         private Dictionary<Enums.Bearing, Coordinate> BearingAdvanceValue() => new Dictionary<Enums.Bearing, Coordinate>
  61.         {
  62.             {Enums.Bearing.North, new Coordinate(0,1)},
  63.             {Enums.Bearing.South, new Coordinate(0,-1)},
  64.             {Enums.Bearing.East, new Coordinate(1,0)},
  65.             {Enums.Bearing.West, new Coordinate(-1,0)},
  66.         };
  67.     }
  68. }
  69.  
  70. namespace RobotService
  71. {
  72.     public class Coordinate
  73.     {
  74.         public int X { get; }
  75.         public int Y { get; }
  76.  
  77.         public Coordinate(int x, int y)
  78.         {
  79.             X = x;
  80.             Y = y;
  81.         }
  82.  
  83.         public static Coordinate operator +(Coordinate c1, Coordinate c2)
  84.         {
  85.             return new Coordinate(c1.X + c2.X, c1.Y + c2.Y);
  86.         }
  87.     }
  88. }
  89.  
  90.  
  91. namespace RobotService
  92. {
  93.     public static class Enums
  94.     {
  95.         public enum Bearing
  96.         {
  97.             North,
  98.             East,
  99.             South,
  100.             West
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement