Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace RobotService
- {
- public class RobotSimulator
- {
- public RobotSimulator(Enums.Bearing bearing, Coordinate coordinate)
- {
- Bearing = bearing;
- Coordinate = coordinate;
- }
- public Enums.Bearing Bearing { get; private set; }
- public Coordinate Coordinate { get; private set; }
- public void TurnRight()
- {
- Bearing++;
- var myEnumMemberCount = Enum.GetNames(typeof(Enums.Bearing)).Length - 1;
- if ((int) Bearing > myEnumMemberCount) Bearing = Enums.Bearing.North;
- }
- public void TurnLeft()
- {
- Bearing--;
- if (Bearing < 0) Bearing = Enums.Bearing.West;
- }
- public void Advance()
- {
- Coordinate = Coordinate + BearingAdvanceValue()[Bearing];
- }
- public void Simulate(string directions)
- {
- foreach (var direction in directions)
- {
- TranslateAndExecuteDirection(direction);
- }
- }
- private void TranslateAndExecuteDirection(char direction)
- {
- var requestedAction = RobotLanguageDic()[direction];
- if (requestedAction == null)
- throw new ArgumentNullException($"direction of {direction} not found in {nameof(RobotLanguageDic)}");
- requestedAction();
- }
- private Dictionary<char, Action> RobotLanguageDic() => new Dictionary<char, Action>
- {
- {'R', TurnRight},
- {'L', TurnLeft },
- {'A', Advance },
- };
- private Dictionary<Enums.Bearing, Coordinate> BearingAdvanceValue() => new Dictionary<Enums.Bearing, Coordinate>
- {
- {Enums.Bearing.North, new Coordinate(0,1)},
- {Enums.Bearing.South, new Coordinate(0,-1)},
- {Enums.Bearing.East, new Coordinate(1,0)},
- {Enums.Bearing.West, new Coordinate(-1,0)},
- };
- }
- }
- namespace RobotService
- {
- public class Coordinate
- {
- public int X { get; }
- public int Y { get; }
- public Coordinate(int x, int y)
- {
- X = x;
- Y = y;
- }
- public static Coordinate operator +(Coordinate c1, Coordinate c2)
- {
- return new Coordinate(c1.X + c2.X, c1.Y + c2.Y);
- }
- }
- }
- namespace RobotService
- {
- public static class Enums
- {
- public enum Bearing
- {
- North,
- East,
- South,
- West
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement