Ejemplo n.º 1
0
        /// <summary>
        /// Reads the command input from a user and decides which action to take
        /// </summary>
        /// <param name="boardName"></param>
        /// <param name="robotService"></param>
        /// <returns></returns>
        private static Action ReadCommand(string boardName, IRobotService robotService)
        {
            var command = Console.ReadLine().ToLower();

            switch (command)
            {
            case string comm when comm.Contains("place"):
                var commandVariables = command.Substring(6, command.Length - 6);

                var variables = commandVariables.Split(',');
                if (variables.Length != 3)
                {
                    break;
                }

                var xSuccess = int.TryParse(variables[0], out var x);
                var ySuccess = int.TryParse(variables[1], out var y);
                var fSuccess = Enum.TryParse <Direction>(variables[2].ToUpper(), out var direction);

                if (!xSuccess || !ySuccess || !fSuccess)
                {
                    break;
                }
                return(() => robotService.PlaceRobot(boardName, new Vector2(x, y), direction));

            case "move":
                return(() => robotService.MoveForward(boardName));

            case "left":
                return(() => robotService.Rotate(boardName, RotateDirection.LEFT));

            case "right":
                return(() => robotService.Rotate(boardName, RotateDirection.RIGHT));

            case "report":
                return(() => { Console.WriteLine(robotService.ReportPosition(boardName)); });
            }
            return(() => Console.WriteLine("Invalid input please try again"));
        }