public Grid(GridLocation[,] locations, int sizeX, int sizeY) { this.sizeX = sizeX; this.sizeY = sizeY; this.locations = locations; this.zeroNeighbors = new List<GridLocation>(); this.oneNeighbor = new List<GridLocation>(); this.originalOneNeighbor = new List<GridLocation>(); this.removalList = new List<GridLocation>(); }
//Returns true if it does not result in a dead-end public bool DecrementAdjacentNeighbors(GridLocation location) { bool good = true; Location loc = location.Location; GridLocation currentLoc = locations[loc.x - 1, loc.y]; for (int i = 0; i < 4; i++) { if (good) { if (i == 0) currentLoc = locations[loc.x - 1, loc.y]; else if (i == 1) currentLoc = locations[loc.x + 1, loc.y]; else if (i == 2) currentLoc = locations[loc.x, loc.y - 1]; else if (i == 3) currentLoc = locations[loc.x, loc.y + 1]; if (!DecrementNeighborCount(currentLoc)) { if (BrokenGrid(currentLoc)) { good = false; while (i >= 0) { if (i == 0) currentLoc = locations[loc.x - 1, loc.y]; else if (i == 1) currentLoc = locations[loc.x + 1, loc.y]; else if (i == 2) currentLoc = locations[loc.x, loc.y - 1]; else if (i == 3) currentLoc = locations[loc.x, loc.y + 1]; IncrementNeighborCount(currentLoc); i--; } } } } } return good; }
//Returns true if it does not result in a dead-end private bool DecrementNeighborCount(GridLocation loc) { if(loc.Status != GridLocationStatus.StaticWall) { if (--loc.NeighborCount < 2) return false; } return true; }
private void IncrementNeighborCount(GridLocation loc) { bool b; if (loc.Location.x == 15 && loc.Location.y == 14) { b = true; } if (loc.Status != GridLocationStatus.StaticWall) if (++loc.NeighborCount == 2) oneNeighbor.Remove(loc); }
//Returns true if valid public bool StepOver(GridLocation loc) { Location l = loc.Location; return DecrementNeighborCount(locations[l.x + 1, l.y]); }
//Checks if the new dead end breaks the grid //Returns false if grid is not broken //Returns true if grid is broken private bool BrokenGrid(GridLocation location) { removalList.Clear(); for(int i = 0; i < oneNeighbor.Count; i++) { if (oneNeighbor[i].Status != GridLocationStatus.FreeSquare) { removalList.Add(oneNeighbor[i]); } } foreach (GridLocation removal in removalList) { oneNeighbor.Remove(removal); } if (oneNeighbor.Count < maxDeadEnd) { if (!oneNeighbor.Contains(location)) { oneNeighbor.Add(location); } return false; } else { if (location.Status == GridLocationStatus.FreeSquare) { return true; } return false; } }
public void IncrementAdjacentNeighbors(GridLocation location) { Location loc = location.Location; IncrementNeighborCount(locations[loc.x - 1, loc.y]); IncrementNeighborCount(locations[loc.x + 1, loc.y]); IncrementNeighborCount(locations[loc.x, loc.y - 1]); IncrementNeighborCount(locations[loc.x, loc.y + 1]); }
private void IncrementNeighborCount(GridLocation loc) { if (loc.Status != GridLocationStatus.StaticWall) if (++loc.NeighborCount == 2) oneNeighbor.Remove(loc); }
//Returns false and does not move if wall is hit //Returns true and makes a single step in that direction if possible bool TryStep(int direction, out GridLocation newLocation) { newLocation = null; /* if (currentMoveList.Count > 0 && currentMoveList.Peek.x == 8 && currentMoveList.Peek.y == 3) { solved = true; return false; } */ switch(direction) { case 0: if (levelDataArray[CurrentX - 1, CurrentY] == 1) { CurrentX--; Step(out newLocation); return true; } return false; case 1: if (levelDataArray[CurrentX + 1, CurrentY] == 1) { CurrentX++; Step(out newLocation); return true; } return false; case 2: if (levelDataArray[CurrentX, CurrentY + 1] == 1) { CurrentY++; Step(out newLocation); return true; } return false; case 3: if (levelDataArray[CurrentX, CurrentY - 1] == 1) { CurrentY--; Step(out newLocation); return true; } return false; default: return false; } }
//Returns true if the step does not create a second dead-end private bool Step(out GridLocation newLocation) { newLocation = grid.Locations[CurrentX, CurrentY]; newLocation.Status = GridLocationStatus.CurrentMoverLocation; levelDataArray[CurrentX, CurrentY] = nextFootPrint++; currentMoveList.PushStep(newLocation.Location); greenCount++; return true; // grid.DecrementAdjacentNeighbors(); /* grid.Locations[loc.Location.x + 1, loc.Location.y].NeighborCount--; grid.Locations[loc.Location.x - 1, loc.Location.y].NeighborCount--; grid.Locations[loc.Location.x, loc.Location.y + 1].NeighborCount--; grid.Locations[loc.Location.x, loc.Location.y - 1].NeighborCount--; * */ }
/// <summary> /// Defines a location in the grid as passable or impassable. /// Creates a graphical representation of the location. /// Should only be used during the initial level creation. /// </summary> /// <param name="x">The horizontal offset to define in the grid</param> /// <param name="y">The vertical offset to define in the grid (Zero is the bottom-most position)</param> /// <param name="barrier">True if the location represents an impassable barrier</param> private void SetWall(int x, int y, bool barrier) { Texture2D texture; if(barrier) { levelDataArray[x, y] = 2; gridLocations[x, y] = new GridLocation(new Location(x, y)); gridLocations[x, y].Status = GridLocationStatus.StaticWall; texture = greenBox; } else { levelDataArray[x,y] = 1; gridLocations[x, y] = new GridLocation(new Location(x, y)); gridLocations[x, y].Status = GridLocationStatus.FreeSquare; texture = greyBox; } testGrid.AddSprite(new FACE_System.Rectangle((float)x, (float)x + boxSizeX, (float)y, (float)y + boxSizeY), texture); originalLevelDataArray[x, y] = levelDataArray[x, y]; }