public static Fitness FindClosestWinner(this NodeGrid grid, IList <Move> movelist) { var distance = 0; var final = movelist.Last().End; var node = grid.GetAt(final); if (node.IsWinner) { return(new Fitness { ClosestNode = node, Distance = distance, Movelist = movelist }); } distance++; for (var i = distance; i < MAX_DISTANCE; i++) { var winner = GetNeighbours(final, i).FirstOrDefault(m => grid.IsValidNode(m) && grid.GetAt(m).IsWinner); if (winner != null) { return(new Fitness { ClosestNode = grid.GetAt(winner), Distance = i, Movelist = movelist }); } } return(new Fitness() { Movelist = movelist }); }
private static void ExtractNextGen(NodeGrid gridz, IList <Vector> moves, int gen) { var nextGenMoves = new List <Vector>(); foreach (var nextMove in moves) { var vectors = nextMove.GenerateAllNextMoves() .Where(z => gridz.IsValidNode(z.End) && !z.HasRepeatedAncestor(new List <Coordinate>())).ToList(); nextGenMoves.AddRange(vectors); } nextGenMoves = nextGenMoves.ToList(); Console.SetCursorPosition(85, 0); Console.WriteLine($"Move: {gen} [{nextGenMoves.Count}]"); nextGenMoves = nextGenMoves.OrderBy(a => Guid.NewGuid()).Take(500).ToList(); foreach (var nextGenMove in nextGenMoves) { Console.SetCursorPosition(nextGenMove.EndX, nextGenMove.EndY); Console.BackgroundColor = ConsoleColor.DarkYellow; Console.Write(" "); } // Console.ReadLine(); Parallel.ForEach(moves, nextMove => { if (gridz.GetAt(nextMove.EndX, nextMove.EndY).TrackType == NodeType.End) { SuccessPaths.Add(nextMove); var parent = nextMove.Parent; do { Console.SetCursorPosition(parent.EndX, parent.EndY); Console.BackgroundColor = ConsoleColor.Magenta; Console.Write(" "); Thread.Sleep(100); parent = parent.Parent; } while (parent != null); } }); ExtractNextGen(gridz, nextGenMoves, ++gen); }