// returns null if the system is considered "stable" // on the other hand, if settling is necessary, returns the // offending script after making one move towards a stable state public CandyScript SettleStep() { KeyValuePair <Vector2Int, Vector2Int> move = FindStablizingMove(); if (move.Key.x == -1) { return(null); } candy_grid[move.Value.x][move.Value.y] = candy_grid[move.Key.x][move.Key.y]; candy_grid[move.Key.x][move.Key.y] = null; CandyScript script = candy_grid[move.Value.x][move.Value.y]; float dx = new int[] { -1, 1 }[move.Value.y - move.Key.y + parity_map[move.Key.x]] *mainRadius; float dy = PolyPieceGenerator.CalculateVStackDistance(6, mainRadius); script.transform.position += new Vector3(dx, -dy, 0); CandyScriptIndex id = candy_indexer[script]; id.row = move.Value.x; id.column = move.Value.y; return(script); }
List <CandyScript> FetchNeighbours(CandyScript script) { CandyScriptIndex id = candy_indexer[script]; int x = id.column; int y = id.row; int p = parity_map[y]; List <Vector2Int> neighbour_indices = new List <Vector2Int>(); List <CandyScript> neighbours = new List <CandyScript>(); if (p == 1) { if (id.column != 0) { neighbour_indices.Add(new Vector2Int(x - 1, y)); neighbour_indices.Add(new Vector2Int(x - 1, y + 1)); neighbour_indices.Add(new Vector2Int(x - 1, y - 1)); } if (id.column != cols) { neighbour_indices.Add(new Vector2Int(x + 1, y)); neighbour_indices.Add(new Vector2Int(x, y + 1)); neighbour_indices.Add(new Vector2Int(x, y - 1)); } } else { neighbour_indices.Add(new Vector2Int(x + 1, y + 1)); neighbour_indices.Add(new Vector2Int(x + 1, y - 1)); neighbour_indices.Add(new Vector2Int(x, y + 1)); neighbour_indices.Add(new Vector2Int(x, y - 1)); if (id.column != 0) { neighbour_indices.Add(new Vector2Int(x - 1, y)); } if (id.column != cols - 1) { neighbour_indices.Add(new Vector2Int(x + 1, y)); } } foreach (Vector2Int ni in neighbour_indices) { Vector2Int gc = ConvertToGridIndices(ni); CandyScript neighbour = SafeGridFetch(gc.x, gc.y); if (neighbour != null) { neighbours.Add(neighbour); } } return(neighbours); }
// a candy is stable if either: // - it's on the bottom row // - it has two blocks supporting it // - it is of odd parity and on an edge, with 1 block supporting it bool IsStable(CandyScript c) { CandyScriptIndex id = candy_indexer[c]; return((id.row == candy_grid.Count - 1) || (parity_map[id.row] == 0 && // even parity (SafeGridFetch(id.row + 1, id.column) != null && SafeGridFetch(id.row + 1, id.column + 1) != null)) // has 2 under it || (parity_map[id.row] == 1 && // odd parity (SafeGridFetch(id.row + 1, id.column) != null && SafeGridFetch(id.row + 1, id.column - 1) != null) || // has 2 under it ((id.column == 0 || id.column == cols) && // is an edge piece (SafeGridFetch(id.row + 1, id.column) != null || SafeGridFetch(id.row + 1, id.column - 1) != null)) )); // one supporting piece }