Ejemplo n.º 1
0
        public bool DoAction(MazeExplorer explorer, ExplorerAction action)
        {
            IExplorerActionStrategy strategy = null;
            switch (action)
            {
                case ExplorerAction.TurnLeft:
                    strategy = new TurnLeftStrategy();
                    break;
                case ExplorerAction.TurnRight:
                    strategy = new TurnRightStrategy();
                    break;
                case ExplorerAction.MoveForward:
                    strategy = new MoveForwardStrategy();
                    break;
                case ExplorerAction.LookForward:
                    strategy = new LookForwardStrategy();
                    break;
                case ExplorerAction.LookAround:
                    strategy = new LookAroundStrategy();
                    break;
            }

            if (strategy != null)
                return strategy.DoAction(explorer);

            return false;
        }
Ejemplo n.º 2
0
        public bool DoAction(MazeExplorer explorer)
        {
            int currentDirection = (int)explorer.CurrentDirection;
            int newDirection = (currentDirection == Enum.GetValues(typeof(ExplorerDirection)).Length) ? 0 : currentDirection + 1;

            explorer.CurrentDirection = (ExplorerDirection)newDirection;
            Console.WriteLine($"You turn right. ({explorer.CurrentDirection})");
            return true;
        }
Ejemplo n.º 3
0
        public void AutoComplete(MazeExplorer explorer)
        {
            Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(r =>
            {

                DoAction(explorer, ExplorerAction.TurnRight);
                if (DoAction(explorer, ExplorerAction.LookForward))
                {
                    Console.WriteLine("true");
                    DoAction(explorer, ExplorerAction.MoveForward);
                }
                else
                {
                    DoAction(explorer, ExplorerAction.TurnLeft);
                    if (!DoAction(explorer, ExplorerAction.MoveForward))
                    {
                        DoAction(explorer, ExplorerAction.TurnLeft);
                    }
                }
                DoAction(explorer, ExplorerAction.LookAround);
            });
        }
Ejemplo n.º 4
0
        public bool DoAction(MazeExplorer explorer)
        {
            var itemsAround = explorer.CurrentMaze.Items.Where(i =>
                i.XCord >= explorer.CurrentXCord - 1 &&
                i.XCord <= explorer.CurrentXCord + 1 &&
                i.YCord >= explorer.CurrentYCord - 1 &&
                i.YCord <= explorer.CurrentYCord + 1).ToList();

            int count = 0;
            string line = "";
            itemsAround.ForEach(i =>
            {
                if (count == 3)
                {
                    Console.WriteLine(line);
                    count = 0;
                    line = "";
                }
                line += MazeItemToText(i.Type);
                count++;
            });
            Console.WriteLine(line);
            return true;
        }
Ejemplo n.º 5
0
        public bool DoAction(MazeExplorer explorer)
        {
            int xCord = explorer.CurrentXCord;
            int yCord = explorer.CurrentYCord;
            switch (explorer.CurrentDirection)
            {
                case ExplorerDirection.North:
                    yCord--;
                    break;
                case ExplorerDirection.East:
                    xCord++;
                    break;
                case ExplorerDirection.South:
                    yCord++;
                    break;
                case ExplorerDirection.West:
                    xCord--;
                    break;
            }
            var item = explorer.CurrentMaze.Items.FirstOrDefault(i => i.XCord == xCord && i.YCord == yCord);

            Console.WriteLine($"Ahead of you is a {item.Type}");
            return item.Type != MazeItemType.Wall;
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            // Instantiate classes until we impliment some kind of DI.
            MazeService mazeService = new MazeService();
            MazeStructure maze = new MazeStructure
            {
                Items = mazeService.ReadMapFile("ExampleMaze.txt")
            };
            MazeExplorer explorer = new MazeExplorer(maze);

            ExplorerService explorerService = new ExplorerService();

            Console.WriteLine("Commands");
            Console.WriteLine("ViewMaze (vm)");
            Console.WriteLine("stat");
            Console.WriteLine("autocomplete (ac)");
            Console.WriteLine("turnleft (l)");
            Console.WriteLine("turnright (r)");
            Console.WriteLine("moveforward (mf)");
            Console.WriteLine("lookforward (lf)");
            Console.WriteLine("lookaround (la)");
            while (true)
            {
                string inputText = Console.ReadLine();
                string[] inputSplit = inputText.Split(' ');
                if (inputSplit.Length == 1)

                    switch (inputText.ToLower())
                    {
                        case "viewmaze":
                        case "vm":
                            mazeService.ViewMaze(maze);
                            break;
                        case "stats":
                            mazeService.Stats(maze);
                            break;
                        case "autocomplete":
                        case "ac":
                            explorerService.AutoComplete(explorer);
                            break;
                        case "turnleft":
                        case "l":
                            explorerService.DoAction(explorer, ExplorerAction.TurnLeft);
                            break;
                        case "turnright":
                        case "r":
                            explorerService.DoAction(explorer, ExplorerAction.TurnRight);
                            break;
                        case "moveforward":
                        case "f":
                            explorerService.DoAction(explorer, ExplorerAction.MoveForward);
                            break;
                        case "lookforward":
                        case "lf":
                            explorerService.DoAction(explorer, ExplorerAction.LookForward);
                            break;
                        case "lookaround":
                        case "la":
                            explorerService.DoAction(explorer, ExplorerAction.LookAround);
                            break;
                    }

                if (inputText.ToLower().StartsWith("viewitem"))
                {

                    if (inputSplit.Length != 3)
                    {
                        Console.WriteLine("Viewitem accepts 2 paramaters");
                    }
                    else
                    {
                        int xCord;
                        int yCord;
                        if (int.TryParse(inputSplit[1], out xCord) &&
                            int.TryParse(inputSplit[2], out yCord))
                        {
                            mazeService.ViewItem(maze, xCord, yCord);
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
 public void SetUp()
 {
     _explorerService = new ExplorerService();
     _maze = new MazeStructure
     {
         Items = new List<MazeItem>
         {
             new MazeItem(1,1,"s"),
             new MazeItem(2,1," "),
             new MazeItem(3,1,"x")
         }
     };
      _explore = new MazeExplorer(_maze);
 }