public static void RenderField(XYButton b, int FieldType) { switch (FieldType) { case (int)Fields.MARKED: Helpers.Render.MarkedField(b, START, END); break; case (int)Fields.END_POINT: Helpers.Render.EndField(b); break; case (int)Fields.WALL: Helpers.Render.WallField(b); break; case (int)Fields.PATH: Helpers.Render.PathField(b); break; case (int)Fields.EMPTY: Helpers.Render.EmptyField(b); break; } }
// Traverse path in reverse order from end to start - finds one of the possibilities for shortest path between start and end public static void RetracePath(XYButton[,] ButtonGrid, Point start, Point end) { // Reverse list of vertices in shortest path from start to end List <Point> SptStartEnd = new List <Point>() { end }; Point currentVertex = end; XYButton temp = ButtonGrid[currentVertex.X, currentVertex.Y]; while (currentVertex != start) { currentVertex = temp.adjacentFields.Find(f => ButtonGrid[f.X, f.Y].shortestPathLength == temp.shortestPathLength - 1); temp = ButtonGrid[currentVertex.X, currentVertex.Y]; RenderField(temp, (int)Form1.Fields.MARKED); // make sure last vertex is marked visually if (currentVertex != start) { SptStartEnd.Add(currentVertex); } } SptStartEnd.RemoveAt(0); SptStartEnd.Reverse(); DrawPath(SptStartEnd, ButtonGrid); }
public static void MarkStartEnd(object sender, MouseEventArgs e) { XYButton b = sender as XYButton; bool changed = false; if (b.index == START) { changed = true; START = new Point(-1, -1); } if (b.index == END) { changed = true; END = new Point(-1, -1); } if (START.X == -1 && START.Y == -1 && !changed) { START = b.index; } else if (END.X == -1 && END.Y == -1 && !changed) { END = b.index; } b.shortestPathLength = b.index == START ? 0 : int.MaxValue; //b.shortestPathLength = b.index == START || b.index == END ? 0 : int.MaxValue; int fieldType = b.index == START || b.index == END ? (int)Fields.END_POINT : (int)Fields.EMPTY; Console.WriteLine($"b.index == START: {b.index == START} | START: {START} END: {END}"); RenderField(b, fieldType); }
public static void MarkedField(XYButton b, Point START, Point END) { if (b.index != START && b.index != END && b.BackColor != MARKED_COLOR) { Form1.ChangeBackColor(b, MARKED_COLOR); b.Update(); } }
private static void RenderField(XYButton b, int FieldType) { Form1.RenderField(b, FieldType); }
public static void ChangeBackColor(XYButton b, Color color) { b.BackColor = color; }
public static void MarkWall(object sender, MouseEventArgs e) { XYButton b = sender as XYButton; b.ToggleWall(); }
public static void EmptyField(XYButton b) { Form1.ChangeBackColor(b, EMPTY_COLOR); b.Update(); }
public static void PathField(XYButton b) { Form1.ChangeBackColor(b, PATH_COLOR); b.Update(); }
public static void EndField(XYButton b) { Form1.ChangeBackColor(b, END_COLOR); b.Update(); }
public static void WallField(XYButton b) { Form1.ChangeBackColor(b, WALL_COLOR); b.Update(); }