private static int DFS(Node Start) { if (Start.getGoal()) { MessageBox.Show("Stigli smo do cilja, ura!"); return(1); } else { int k = 0; Start.mark(); Start.getButton().Visible = false; Start.getButton().Visible = true; System.Threading.Thread.Sleep(10); Start.getButton().BackColor = Color.Blue; Node[] Neighbours = getUnmarkedNeighbours(Start, false); while (Neighbours[k] != null) { if (!Neighbours[k].isMarked() && !Neighbours[k].isBarrier()) { if (DFS(Neighbours[k]) == 1) { // Neighbours[k].getButton().BackColor = Color.Orange; return(1); } } k++; } return(0); } }
private static int ReconstructAzvezda(Node n) { while (n != null) { n.getButton().BackColor = Color.Orange; n = n.CameFrom; } return(1); }
private static int Astar(Node Start) { Node[] Neighbours = getUnmarkedNeighbours(Start, true).Where(c => c != null && !c.isMarked()).ToArray(); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Graph[i, j].calcDistanceToGoal(); } } // MessageBox.Show(""); Start.gScore = 0; Start.getButton().Text = "0"; Neighbours[0] = Start; while (Neighbours.Length > 0) { Neighbours = Neighbours.OrderBy(x => x.fScore).ToArray(); if (Neighbours[0].getGoal()) //reached the goal? { if (Neighbours[0].CameFrom == null) //failed to reconstruct path? { MessageBox.Show("Failed to find path, report to Boki"); } else { MessageBox.Show("Shortest path of length " + Math.Round(Neighbours[0].gScore, 2) + " found"); } ReconstructAzvezda(Neighbours[0].CameFrom); return(2); } else { if (!Neighbours[0].isMarked() && !Neighbours[0].isBarrier()) { Node[] backUp = getUnmarkedNeighbours(Neighbours[0], true).Where(c => c != null).ToArray(); for (int i = 0; i < backUp.Length; i++) { if (true) { int x1 = Neighbours[0].getJ(), x2 = backUp[i].getJ(); int y1 = Neighbours[0].getI(), y2 = backUp[i].getI(); double dist = Neighbours[0].gScore + Math.Sqrt(((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))); if (dist < backUp[i].gScore) { backUp[i].CameFrom = Neighbours[0]; backUp[i].gScore = dist; backUp[i].fScore = dist + backUp[i].getDist(); } backUp[i].getButton().Text = backUp[i].gScore.ToString(); } } Neighbours[0].mark(); Neighbours[0].getButton().Visible = false; Neighbours[0].getButton().Visible = true; Neighbours = Neighbours.Concat(backUp).ToArray(); } Neighbours = Neighbours.Skip(1).ToArray(); } } MessageBox.Show("ne postoji put"); return(0); }