コード例 #1
0
 private void MoveForward(MartianRobotUpdateable martianRobot)
 {
     if (martianRobot.Direction.Equals("N"))
     {
         if (HasLimits(martianRobot) && martianRobot.YPosition + 1 > martianRobot.YLimit)
         {
             martianRobot.IsLost = true;
         }
         else
         {
             martianRobot.YPosition += 1;
         }
     }
     else if (martianRobot.Direction.Equals("E"))
     {
         if (HasLimits(martianRobot) && martianRobot.XPosition + 1 > martianRobot.XLimit)
         {
             martianRobot.IsLost = true;
         }
         else
         {
             martianRobot.XPosition += 1;
         }
     }
     else if (martianRobot.Direction.Equals("S"))
     {
         if (HasLimits(martianRobot) && martianRobot.YPosition - 1 < 0)
         {
             martianRobot.IsLost = true;
         }
         else
         {
             martianRobot.YPosition -= 1;
         }
     }
     else if (martianRobot.Direction.Equals("W"))
     {
         if (HasLimits(martianRobot) && martianRobot.XPosition - 1 < 0)
         {
             martianRobot.IsLost = true;
         }
         else
         {
             martianRobot.XPosition -= 1;
         }
     }
 }
コード例 #2
0
        private void CalculateCommand(MartianRobotUpdateable martianRobot, string command)
        {
            if (!martianRobot.IsLost)
            {
                switch (command)
                {
                case "F":
                    MoveForward(martianRobot);
                    break;

                case "L":
                    MoveLeft(martianRobot);
                    break;

                case "R":
                    MoveRight(martianRobot);
                    break;
                }
            }
        }
コード例 #3
0
        private void MoveRight(MartianRobotUpdateable martianRobot)
        {
            switch (martianRobot.Direction)
            {
            case "N":
                martianRobot.Direction = "E";
                break;

            case "E":
                martianRobot.Direction = "S";
                break;

            case "S":
                martianRobot.Direction = "W";
                break;

            case "W":
                martianRobot.Direction = "N";
                break;
            }
        }
コード例 #4
0
        private List <MartianRobotDTO> CalculateCommands(MartianRobotUpdateable martianRobot)
        {
            List <MartianRobotDTO> response = new List <MartianRobotDTO>();
            var commandsAsArray             = martianRobot.Commands.ToStringArray();

            foreach (var item in commandsAsArray)
            {
                CalculateCommand(martianRobot, item);
                var output = martianRobot.XPosition.ToString() + " " + martianRobot.YPosition.ToString() + " " + martianRobot.Direction;
                var result = new MartianRobotDTO()
                {
                    Result = output, X = martianRobot.XPosition, Y = martianRobot.YPosition, Direction = martianRobot.Direction, IsValid = true
                };
                if (martianRobot.IsLost)
                {
                    output         = output + " LOST";
                    result.IsValid = false;
                }
                response.Add(result);
            }
            return(response);
        }
コード例 #5
0
 public bool HasLimits(MartianRobotUpdateable martianRobot)
 {
     return(martianRobot.XLimit > 0 && martianRobot.YLimit > 0);
 }