public bool IsMoveLegal(Coordinates ponyLocation, Direction direction) { return(Chambers[ponyLocation].ChamberConnections.Contains(direction)); }
public Stack <Direction> FindShortestPathToExit(Coordinates ponyLocation) { if (CurrentPathToExit != null) { foreach (Chamber cham in Chambers.Values) { cham.ClearPathData(); } } List <Coordinates> chambersWithNeighboursToExploreStartingFromPony = new List <Coordinates>() { ponyLocation }; Chambers[ponyLocation].SetPathFromPony(new List <Direction>()); List <Coordinates> chambersWithNeighboursToExploreStartingFromExit = new List <Coordinates>() { this.exitLocation }; Chambers[this.exitLocation].SetPathToExit(new List <Direction>()); while (true) { List <Coordinates> chambersToExploreStartingFromPonyNext = new List <Coordinates>(); //find chambers in range of pony foreach (Coordinates location in chambersWithNeighboursToExploreStartingFromPony) { var currentChamber = Chambers[location]; foreach (Direction dir in currentChamber.ChamberConnections) { var neighrouringChamber = Chambers[location.Move(dir)]; if (neighrouringChamber.PathFromPony == null) { if (neighrouringChamber.SetPathFromPony(currentChamber.PathFromPony, dir)) { neighrouringChamber.PathToExit.Reverse(); var shortestPathFound = neighrouringChamber.PathFromPony.Concat(neighrouringChamber.PathToExit).ToList(); shortestPathFound.Reverse(); CurrentPathToExit = new Stack <Direction>(shortestPathFound); return(CurrentPathToExit); } chambersToExploreStartingFromPonyNext.Add(neighrouringChamber.Coordinates); } } } chambersWithNeighboursToExploreStartingFromPony = chambersToExploreStartingFromPonyNext; var chambersToExploreStartingFromExitNext = new List <Coordinates>(); foreach (Coordinates location in chambersWithNeighboursToExploreStartingFromExit) { var currentChamber = Chambers[location]; foreach (Direction dir in currentChamber.ChamberConnections) { var neighrouringChamber = Chambers[location.Move(dir)]; if (neighrouringChamber.PathToExit == null) { if (neighrouringChamber.SetPathToExit(currentChamber.PathToExit, dir)) { neighrouringChamber.PathToExit.Reverse(); var shortestPathFound = neighrouringChamber.PathFromPony.Concat(neighrouringChamber.PathToExit).ToList(); shortestPathFound.Reverse(); CurrentPathToExit = new Stack <Direction>(shortestPathFound); return(CurrentPathToExit); } chambersToExploreStartingFromExitNext.Add(neighrouringChamber.Coordinates); } } } chambersWithNeighboursToExploreStartingFromExit = chambersToExploreStartingFromExitNext; } ; }
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); } } }