public Grid2D(int width, int height, int wallPercentage, int startX, int startY, int goalX, int goalY) { var rand = new Random(); Start = new GridNode(this, startX, startY, false); Goal = new GridNode(this, goalX, goalY, false); Grid = new GridNode[width][]; for (var i = 0; i < width; i++) Grid[i] = new GridNode[height]; Grid[Start.X][Start.Y] = Start; Grid[Goal.X][Goal.Y] = Goal; for (var i = 0; i < width; i++) { for (var j = 0; j < height; j++) { // don't overwrite start/goal nodes if (Grid[i][j] != null) continue; Grid[i][j] = new GridNode(this, i, j, rand.Next(100) < wallPercentage); } } }
public string Print(GridNode start, GridNode goal, IEnumerable<INode> path) { if (IsWall) { return "W"; } else if (IsEqual(start)) { return "s"; } else if (IsEqual(goal)) { return "g"; } else if (IsInPath(path)) { return "."; } else { return " "; } }
/// <summary> /// Two nodes are equal if they share the same spot in the grid. /// </summary> /// <param name="node"></param> /// <returns></returns> public bool IsEqual(GridNode node) { return (this == node) || (this.X == node.X && this.Y == node.Y); }
public Grid2D(GridNode[][] grid, GridNode start, GridNode goal) { Grid = grid; Start = start; Goal = goal; }