public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return(false); } GridPoint g = (GridPoint)(obj); if (this.x == g.x && this.y == g.y) { return(true); } else { return(false); } }
public MazePath Branch() { if (points.Count < 3) { return(null); } var index = rand.Next(points.Count - 2) + 1; var nx = points[index].x; var ny = points[index].y; while (nx % 2 == 1 || ny % 2 == 1) { index = rand.Next(points.Count - 2) + 1; nx = points[index].x; ny = points[index].y; } var dirNew = Direction.UP; var gUp = new GridPoint(nx, ny - 1); var gDown = new GridPoint(nx, ny + 1); var gLeft = new GridPoint(nx - 1, ny); var gRight = new GridPoint(nx + 1, ny); // Path is vertical if ((gUp.Equals(points[index - 1]) && gDown.Equals(points[index + 1])) || (gUp.Equals(points[index + 1]) && gDown.Equals(points[index - 1]))) { var roll = rand.Next(2); nx = (roll == 0 ? nx + 1 : nx - 1); dirNew = (roll == 0 ? Direction.RIGHT : Direction.LEFT); return(new MazePath(nx, ny, dirNew)); } // Path is horizontal if ((gLeft.Equals(points[index - 1]) && gRight.Equals(points[index + 1])) || (gLeft.Equals(points[index + 1]) && gRight.Equals(points[index - 1]))) { var roll = rand.Next(2); ny = (roll == 0 ? ny + 1 : ny - 1); dirNew = (roll == 0 ? Direction.DOWN : Direction.UP); return(new MazePath(nx, ny, dirNew)); } // Corner, upper right if ((gUp.Equals(points[index - 1]) && gRight.Equals(points[index + 1])) || (gUp.Equals(points[index + 1]) && gRight.Equals(points[index - 1]))) { var roll = rand.Next(2); nx = (roll == 0 ? nx - 1 : nx); ny = (roll == 0 ? ny : ny + 1); dirNew = (roll == 0 ? Direction.LEFT : Direction.DOWN); return(new MazePath(nx, ny, dirNew)); } // Corner, upper left if ((gUp.Equals(points[index - 1]) && gLeft.Equals(points[index + 1])) || (gUp.Equals(points[index + 1]) && gLeft.Equals(points[index - 1]))) { var roll = rand.Next(2); nx = (roll == 0 ? nx + 1 : nx); ny = (roll == 0 ? ny : ny + 1); dirNew = (roll == 0 ? Direction.RIGHT : Direction.DOWN); return(new MazePath(nx, ny, dirNew)); } // Corner, lower right if ((gDown.Equals(points[index - 1]) && gRight.Equals(points[index + 1])) || (gDown.Equals(points[index + 1]) && gRight.Equals(points[index - 1]))) { var roll = rand.Next(2); nx = (roll == 0 ? nx - 1 : nx); ny = (roll == 0 ? ny : ny - 1); dirNew = (roll == 0 ? Direction.LEFT : Direction.UP); return(new MazePath(nx, ny, dirNew)); } // Corner, lower left if ((gDown.Equals(points[index - 1]) && gLeft.Equals(points[index + 1])) || (gDown.Equals(points[index + 1]) && gLeft.Equals(points[index - 1]))) { var roll = rand.Next(2); nx = (roll == 0 ? nx + 1 : nx); ny = (roll == 0 ? ny : ny - 1); dirNew = (roll == 0 ? Direction.RIGHT : Direction.UP); return(new MazePath(nx, ny, dirNew)); } return(null); }
public bool PathIsClear(GridPoint pt, Direction dir) { int checkX = Math.Max(wall_left, pt.x); int checkY = Math.Max(wall_top, pt.y); checkX = Math.Min(wall_right, checkX); checkY = Math.Min(wall_bottom, checkY); if (grid[checkY, checkX] == CH_WALL) { return(false); } switch (dir) { case Direction.UP: { if (grid[checkY, checkX + 1] == CH_WALL || grid[checkY, checkX - 1] == CH_WALL || grid[checkY + 1, checkX + 1] == CH_WALL || grid[checkY + 1, checkX] == CH_WALL || grid[checkY + 1, checkX - 1] == CH_WALL) { return(false); } } break; case Direction.DOWN: { if (grid[checkY, checkX + 1] == CH_WALL || grid[checkY, checkX - 1] == CH_WALL || grid[checkY - 1, checkX + 1] == CH_WALL || grid[checkY - 1, checkX] == CH_WALL || grid[checkY - 1, checkX - 1] == CH_WALL) { return(false); } } break; case Direction.LEFT: { if (grid[checkY + 1, checkX] == CH_WALL || grid[checkY - 1, checkX] == CH_WALL || grid[checkY + 1, checkX + 1] == CH_WALL || grid[checkY, checkX + 1] == CH_WALL || grid[checkY - 1, checkX + 1] == CH_WALL) { return(false); } } break; case Direction.RIGHT: { if (grid[checkY + 1, checkX] == CH_WALL || grid[checkY - 1, checkX] == CH_WALL || grid[checkY + 1, checkX - 1] == CH_WALL || grid[checkY, checkX - 1] == CH_WALL || grid[checkY - 1, checkX - 1] == CH_WALL) { return(false); } } break; default: {} break; } return(true); }