/// <summary> /// Gets the distance between two GridFields. /// </summary> /// <param name="first">The first GridField.</param> /// <param name="second">The second GridField.</param> /// <returns>Int32</returns> public int GetDistance(GridField first, GridField second) { int dX = (int)first.Position.X - (int)second.Position.X; int dY = (int)first.Position.Y - (int)second.Position.Y; return((int)MathHelper.Sqrt(dX * dX + dY * dY)); }
/// <summary> /// Initializes a new Grid class. /// </summary> /// <param name="width">The Width.</param> /// <param name="height">The Height.</param> public Grid(int width, int height) { _fields = new GridField[width, height]; GridWidth = width; GridHeight = height; for (int x = 0; x <= width - 1; x++) { for (int y = 0; y <= height - 1; y++) { //detect neighbors var gridField = new GridField { Neighbors = GetNeighbors(x, y), Position = new Vector2(x, y) }; _fields[x, y] = gridField; } } }
/// <summary> /// Trys to solve a path. /// </summary> /// <param name="startField">The Startfield.</param> /// <param name="targetField">The Targetfield.</param> /// <param name="path">Out of Positions.</param> /// <param name="grid">The Grid.</param> /// <param name="algorithm">The Search-Algorithm.</param> /// <returns>True on success</returns> public static bool TrySolve(Grid grid, GridField startField, GridField targetField, IAlgorithm algorithm, out Stack <GridField> path) { return(algorithm.TrySolve(grid, startField, targetField, out path)); }