/// <summary> /// Finds a path. Throws PathNotFoundException when no path can be found. /// </summary> /// <returns>A finished path.</returns> private Dictionary <HashSet <KeyColor>, Dictionary <Point, PlayerAction> > FindPath() { Level = new AStarLevel(player.TileField); Level.SolveLevel(); if (Level.OptimalPath.Count == 0) { throw new PathNotFoundException(); } return(Level.OptimalPath); }
/// <summary> /// Solves the level. /// </summary> public void SolveLevel() { while (OpenTiles.Count > 0) { OpenTiles.Sort((t1, t2) => t1.Score.CompareTo(t2.Score)); VisitTile(OpenTiles[0]); } HashSet <AStarLevel> innerFields = new HashSet <AStarLevel>(); AStarLevel optimalField = null; foreach (ColoredTile t in ReachedKeys) { AStarLevel newField = new AStarLevel(this, t, Field[t.TileLocation.X, t.TileLocation.Y].Score); innerFields.Add(newField); newField.SolveLevel(); if (newField.Finish?.Score < (Finish?.Score ?? long.MaxValue)) { Finish = newField.Finish; optimalField = newField; } } if (optimalField != null) { OptimalPath.Add(obtainedKeys, CreateOptimalPath(Start, Field[(int)optimalField?.Start.Location.X, (int)optimalField?.Start.Location.Y])); foreach (HashSet <KeyColor> keys in optimalField.OptimalPath.Keys) { OptimalPath.Add(keys, optimalField.OptimalPath[keys]); } } else if (Finish != null) { OptimalPath.Add(obtainedKeys, CreateOptimalPath(Start, Field[(int)Finish?.Location.X, (int)Finish?.Location.Y])); } }