Ejemplo n.º 1
0
        public void Initialize(int mazeStateWidth, int mazeStateHeight, int mazeStatePonyLocation, int mazeStateExitLocation, List <List <string> > data)
        {
            this.mazeWidth    = mazeStateWidth;
            this.mazeHeight   = mazeStateHeight;
            this.exitLocation = new Coordinates(Coordinates.GetXCoordinate(mazeStateExitLocation, mazeWidth), Coordinates.GetYCoordinate(mazeStateExitLocation, mazeWidth));

            Chambers = new Dictionary <Coordinates, Chamber>();

            for (int masterIndex = 0; masterIndex < data.Count; masterIndex++)
            {
                int xIndex      = Coordinates.GetXCoordinate(masterIndex, mazeWidth);
                int yIndex      = Coordinates.GetYCoordinate(masterIndex, mazeWidth);
                var coordinates = new Coordinates(xIndex, yIndex);
                Chambers.Add(coordinates, new Chamber(coordinates));

                var reportedWalls = data[masterIndex];

                if (!reportedWalls.Contains(Direction.North.ToString().ToLower()))
                {
                    Chambers[coordinates].AddConnectedChamber(Direction.North);
                }

                if (xIndex < mazeWidth - 1 && !data[masterIndex + 1].Contains(Direction.West.ToString().ToLower()))
                {
                    Chambers[coordinates].AddConnectedChamber(Direction.East);
                }

                if (yIndex < mazeHeight - 1 && !data[masterIndex + mazeWidth].Contains(Direction.North.ToString().ToLower()))
                {
                    Chambers[coordinates].AddConnectedChamber(Direction.South);
                }

                if (!reportedWalls.Contains(Direction.West.ToString().ToLower()))
                {
                    Chambers[coordinates].AddConnectedChamber(Direction.West);
                }
            }
        }
Ejemplo n.º 2
0
        public Direction GetNextMove(IMazeMap mazeMap, MazeState mazeState)
        {
            Direction   returnValue     = Direction.None;
            Coordinates domokunLocation = new Coordinates(Coordinates.GetXCoordinate(mazeState.DomokunLocation, mazeState.width), Coordinates.GetYCoordinate(mazeState.DomokunLocation, mazeState.width));
            Coordinates ponyLocation    = new Coordinates(Coordinates.GetXCoordinate(mazeState.PonyLocation, mazeState.width), Coordinates.GetYCoordinate(mazeState.PonyLocation, mazeState.width));

            if (mazeMap.CurrentPathToExit == null)
            {
                mazeMap.FindShortestPathToExit(ponyLocation);
            }

            Direction nextPotentialMove     = mazeMap.CurrentPathToExit.Peek();
            var       nextPotentialLocation = ponyLocation.Move(nextPotentialMove);

            if (mazeMap.IsDomokunClose(domokunLocation, nextPotentialLocation))
            {
                //see if we can escape
                var possibleDodge = Enum.GetValues(typeof(Direction)).Cast <Direction>().AsEnumerable().FirstOrDefault(direction => direction != Direction.None && direction != nextPotentialMove && mazeMap.IsMoveLegal(ponyLocation, direction));
                if (possibleDodge != default(Direction))
                {
                    //escape
                    mazeMap.CurrentPathToExit.Push(possibleDodge.Inverse());
                    returnValue = possibleDodge;
                }
                else
                {
                    //accept our fate
                    returnValue = mazeMap.CurrentPathToExit.Pop();
                }
            }
            else
            {
                returnValue = mazeMap.CurrentPathToExit.Pop();
            }
            return(returnValue);
        }