Ejemplo n.º 1
0
        public (bool canMove, Obstacle ob) ProcessSingleCommand(char command)
        {
            if (!VALID_COMMANDS.Contains(command))
            {
                throw new BadCommandException("Invalid Command Character");
            }

            RoverCoordinates current = this.Coordinates;

            RoverCoordinates proposed = current;

            if (command.Equals('F'))
            {
                switch (this.Direction)
                {
                case Direction.North:
                    proposed.yPos++;
                    break;

                case Direction.West:
                    proposed.xPos--;
                    break;

                case Direction.South:
                    proposed.yPos--;
                    break;

                case Direction.East:
                    proposed.xPos++;
                    break;

                default:
                    break;
                }
            }
            else if (command.Equals('B'))
            {
                switch (this.Direction)
                {
                case Direction.North:
                    proposed.yPos--;
                    break;

                case Direction.West:
                    proposed.xPos++;
                    break;

                case Direction.South:
                    proposed.yPos++;
                    break;

                case Direction.East:
                    proposed.xPos--;
                    break;

                default:
                    break;
                }
            }
            else if (command.Equals('L'))
            {
                switch (this.Direction)
                {
                case Direction.North:
                case Direction.West:
                case Direction.South:
                    this.Direction++;
                    break;

                case Direction.East:
                    this.Direction = Direction.North;
                    break;

                default:
                    break;
                }
                return(true, null);
            }
            else // command.equals('R')
            {
                switch (this.Direction)
                {
                case Direction.North:
                    this.Direction = Direction.East;
                    break;

                case Direction.West:
                case Direction.South:
                case Direction.East:
                    this.Direction--;
                    break;

                default:
                    break;
                }
                return(true, null);
            }

            // check if proposed move is into an obstacle
            bool canMove = !(ObstacleService.IsPointAnObstacle(proposed.xPos, proposed.yPos));

            if (canMove)
            {
                MoveRover(proposed);
                return(true, null);
            }
            else
            {
                return(false, new Obstacle(proposed.xPos, proposed.yPos));
            }
        }
Ejemplo n.º 2
0
 public Rover()
 {
     this.ObstacleService = new ObstacleService();
     Coordinates          = new RoverCoordinates(0, 0);
     Direction            = Direction.North;
 }
Ejemplo n.º 3
0
 public Rover(RoverCoordinates roverCoordinates, Direction dir)
 {
     this.Coordinates = roverCoordinates;
     this.Direction   = dir;
 }
Ejemplo n.º 4
0
 private void MoveRover(RoverCoordinates proposed)
 {
     this.Coordinates = proposed;
 }