|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace RobotWars.App |
| 7 | +{ |
| 8 | + public class Robot : IRobot |
| 9 | + { |
| 10 | + private readonly IRobotCommandMarshaller _commandMarshaller; |
| 11 | + private readonly IGrid _grid; |
| 12 | + public int X { get; set; } |
| 13 | + public int Y { get; set; } |
| 14 | + public OrientationEnum Orientation { get; set; } |
| 15 | + |
| 16 | + public Robot(int initialX, int initialY, OrientationEnum orientation, IGrid grid) |
| 17 | + : this(initialX, initialY, orientation, grid, new RobotCommandMarshaller()){} |
| 18 | + |
| 19 | + public Robot(int initialX, int initialY, OrientationEnum orientation, IGrid grid, IRobotCommandMarshaller commandMarshaller) |
| 20 | + { |
| 21 | + _commandMarshaller = commandMarshaller; |
| 22 | + _commandMarshaller.SetRobot(this); |
| 23 | + |
| 24 | + _grid = grid; |
| 25 | + X = initialX; |
| 26 | + Y = initialY; |
| 27 | + Orientation = orientation; |
| 28 | + } |
| 29 | + |
| 30 | + public Robot(string setupString, IGrid grid) : this(setupString, grid, new RobotCommandMarshaller()){} |
| 31 | + public Robot(string setupString, IGrid grid, IRobotCommandMarshaller commandMarshaller) |
| 32 | + { |
| 33 | + _commandMarshaller = commandMarshaller; |
| 34 | + commandMarshaller.SetRobot(this); |
| 35 | + |
| 36 | + _grid = grid; |
| 37 | + string[] setupParts = setupString.Split(' '); |
| 38 | + if (setupParts.Length != 3) |
| 39 | + throw new ArgumentException("To set up a robot provide X Y [N|E|S|W] values seperated by spaces\ne.g: 1 1 N"); |
| 40 | + |
| 41 | + int tryX = 0, tryY = 0; |
| 42 | + if (!int.TryParse(setupParts[0], out tryX)) |
| 43 | + throw new ArgumentException(string.Format("{0} is not a valid value for X", setupParts[0]), "setupString"); |
| 44 | + if (!int.TryParse(setupParts[1], out tryY)) |
| 45 | + throw new ArgumentException(string.Format("{0} is not a valid value for Y", setupParts[1]), "setupString"); |
| 46 | + if (tryX < 0 || tryX >= grid.Width || tryY < 0 || tryY >= grid.Height) |
| 47 | + throw new ArgumentException(string.Format("The coordinates {0},{1} are outside the grid", setupParts[0], |
| 48 | + setupParts[1]), "setupString"); |
| 49 | + |
| 50 | + X = tryX; |
| 51 | + Y = tryY; |
| 52 | + switch(setupParts[2]) |
| 53 | + { |
| 54 | + case "N": |
| 55 | + Orientation = OrientationEnum.North; |
| 56 | + break; |
| 57 | + case "E": |
| 58 | + Orientation = OrientationEnum.East; |
| 59 | + break; |
| 60 | + case "S": |
| 61 | + Orientation = OrientationEnum.South; |
| 62 | + break; |
| 63 | + case "W": |
| 64 | + Orientation = OrientationEnum.West; |
| 65 | + break; |
| 66 | + default: |
| 67 | + throw new ArgumentException(string.Format("{0} is not a valid value for Orientation", setupParts[2]), "setupString"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public void CommandLeft() |
| 72 | + { |
| 73 | + --Orientation; |
| 74 | + if (!Enum.IsDefined(typeof(OrientationEnum), (int)Orientation)) |
| 75 | + Orientation = Enum.GetValues(typeof(OrientationEnum)).Cast<OrientationEnum>().Last(); |
| 76 | + } |
| 77 | + |
| 78 | + public void CommandRight() |
| 79 | + { |
| 80 | + ++Orientation; |
| 81 | + if (!Enum.IsDefined(typeof(OrientationEnum), (int)Orientation)) |
| 82 | + Orientation = 0; |
| 83 | + } |
| 84 | + |
| 85 | + public void CommandMove() |
| 86 | + { |
| 87 | + switch(Orientation) |
| 88 | + { |
| 89 | + case OrientationEnum.North: |
| 90 | + if (Y+1==_grid.Height) throw new InvalidOperationException("Cannot move outside the grid!"); |
| 91 | + ++Y; |
| 92 | + break; |
| 93 | + case OrientationEnum.East: |
| 94 | + if (X+1 == _grid.Width) throw new InvalidOperationException("Cannot move outside the grid!"); |
| 95 | + ++X; |
| 96 | + break; |
| 97 | + case OrientationEnum.West: |
| 98 | + if (X == 0) throw new InvalidOperationException("Cannot move outside the grid!"); |
| 99 | + --X; |
| 100 | + break; |
| 101 | + case OrientationEnum.South: |
| 102 | + if (Y == 0) throw new InvalidOperationException("Cannot move outside the grid!"); |
| 103 | + --Y; |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public void ExecuteInstructions(string instructions) |
| 109 | + { |
| 110 | + _commandMarshaller.ExecuteInstructions(instructions); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments