Exemple #1
0
        public Position Start(Position position, string commands)
        {
            Console.WriteLine($"Current location : {LocationInfo(position)}");

            ICommand command   = new RoverCommand();
            var      direction = Direction.GetDirection(position.Direction);

            foreach (var cmd in commands)
            {
                switch (cmd)
                {
                case Commands.Left:
                    direction = command.TurnLeft((Directions)direction.Id);
                    position.SetDirection((Directions)direction.Id);
                    break;

                case Commands.Right:
                    direction = command.TurnRight((Directions)direction.Id);
                    position.SetDirection((Directions)direction.Id);
                    break;

                case Commands.Move:
                {
                    if (IsThereAnyDeathHere(position))
                    {
                        Console.WriteLine("You cannot go further. Try another direction please..");
                        var newCommand = Console.ReadLine();
                        this.Start(position, newCommand);
                    }

                    position = command.Move(position);

                    if (CheckPosition(position) == RoverStatus.Dead)
                    {
                        Console.WriteLine("Rest in peace!");
                        commands = string.Empty;
                    }
                }
                break;

                default:
                    throw new Exception($"command : {command} is not invalid.");
                }
                Console.WriteLine(LocationInfo(position));
            }

            return(position);
        }