private void ExecuteMovementCommands(RoverState roverState, string movementCommands) { var moveResult = MoveResult.Ok; foreach (var command in movementCommands) { switch (command) { case 'M': moveResult = MoveForward(roverState); break; case 'R': RotateRight(roverState); break; case 'L': RotateLeft(roverState); break; default: throw new InvalidCommandException(); } if (moveResult != MoveResult.Ok) { roverState.MoveErrors.Add(moveResult); break; } } }
private void RotateRight(RoverState roverState) { switch (roverState.CurrentDirection) { case Direction.Undefined: throw new InitialDirectionNotDefinedException(); case Direction.North: roverState.CurrentDirection = Direction.East; break; case Direction.East: roverState.CurrentDirection = Direction.South; break; case Direction.South: roverState.CurrentDirection = Direction.West; break; case Direction.West: roverState.CurrentDirection = Direction.North; break; default: throw new ArgumentOutOfRangeException(nameof(roverState.CurrentDirection)); } }
public RoverState CreateCopy() { var roverState = new RoverState(); roverState.CurrentLocation.X = CurrentLocation.X; roverState.CurrentLocation.Y = CurrentLocation.Y; roverState.CurrentDirection = CurrentDirection; roverState.MaxExtent.X = MaxExtent.X; roverState.MaxExtent.Y = MaxExtent.Y; return(roverState); }
private MoveResult MoveForward(RoverState roverState) { var moveResult = roverState.ValidateMove(); if (moveResult != MoveResult.Ok) { return(moveResult); } switch (roverState.CurrentDirection) { case Direction.Undefined: throw new InitialDirectionNotDefinedException(); case Direction.North: roverState.CurrentLocation.Y++; break; case Direction.East: roverState.CurrentLocation.X++; break; case Direction.South: roverState.CurrentLocation.Y--; break; case Direction.West: roverState.CurrentLocation.X--; break; default: throw new ArgumentOutOfRangeException(nameof(moveResult)); } return(moveResult); }