public bool CollapseLocation(int x, int y) { Debug.Assert(y < solution.Count); List <TilePossibilities> row = solution[y]; Debug.Assert(x < row.Count); TilePossibilities location = row[x]; UpdateFromNeighbours(x, y); if (!location.HasPossibilities) { return(false); } location.Collapse(); Debug.Assert(location.HasCollapsed, "Invalid number of possible tiles after collapse"); Debug.Log(string.Format("Location {0}, {1} collapsed to {2}", x, y, location.possibleTiles[0])); UpdateNeighbours(x, y, location.possibleTiles[0]); // If any of them end up with no possibilities this was a bad choice // OPTIMIZATION: Possibility to backtrack here and do another choice return(DoAllNeighboursHavePossibilities(x, y)); }
private void UpdateFromNeighbours(int x, int y) { Debug.Assert(y < solution.Count); List <TilePossibilities> row = solution[y]; Debug.Assert(x < row.Count); TilePossibilities location = row[x]; // Check left { if (x == 0) { location.RemoveUnsupportedPossibilitiesFor(null, Direction.Left); } else if (row[x - 1].HasCollapsed) { location.RemoveUnsupportedPossibilitiesFor(row[x - 1].possibleTiles[0], Direction.Left); } } // Check Right { if (x == row.Count - 1) { location.RemoveUnsupportedPossibilitiesFor(null, Direction.Right); } else if (row[x + 1].HasCollapsed) { location.RemoveUnsupportedPossibilitiesFor(row[x + 1].possibleTiles[0], Direction.Right); } } // Check Bottom { if (y == 0) { location.RemoveUnsupportedPossibilitiesFor(null, Direction.Bottom); } else if (solution[y - 1][x].HasCollapsed) { location.RemoveUnsupportedPossibilitiesFor(solution[y - 1][x].possibleTiles[0], Direction.Bottom); } } // Check Top { if (y == solution.Count - 1) { location.RemoveUnsupportedPossibilitiesFor(null, Direction.Top); } else if (solution[y + 1][x].HasCollapsed) { location.RemoveUnsupportedPossibilitiesFor(solution[y + 1][x].possibleTiles[0], Direction.Top); } } }
private int CollapsedLocationCount() { int count = 0; for (int row = 0; row < rows; ++row) { List <TilePossibilities> rowPossibilities = solution[row]; for (int column = 0; column < columns; ++column) { TilePossibilities location = rowPossibilities[column]; if (location.HasCollapsed) { ++count; } } } return(count); }
private Vector2 NextLocation() { int x = 0, y = 0; int currentPossibilityCount = int.MaxValue; for (int row = 0; row < rows; ++row) { List <TilePossibilities> rowPossibilities = solution[row]; for (int column = 0; column < columns; ++column) { TilePossibilities location = rowPossibilities[column]; if (!location.HasCollapsed && location.HasPossibilities && location.possibleTiles.Count < currentPossibilityCount) { x = column; y = row; currentPossibilityCount = location.possibleTiles.Count; } } } return(new Vector2(x, y)); }