public void CellStateChanged(object sender, CellStateChangedEventArgs e) { Cell firstCell = null; bool completed = true; for (int c = 0; c < NumCellsPerAxis; c++) { for (int r = 0; r < NumCellsPerAxis; r++) { Cell cell = Playfield.GetCell(c + Column, r + Row); if (firstCell == null) { firstCell = cell; } if (!cell.IsOccupied || !firstCell.IsSameColor(cell)) { completed = false; break; } } } if (completed && State != States.Completing) { foreach (var c in new Cell[] { UpperLeft, UpperRight, LowerLeft, LowerRight }) { if (!c.IsRemove) { c.RemoveState = Cell.RemoveStates.WillBeRemoved; } } } else if (!completed) { foreach (var c in new Cell[] { UpperLeft, UpperRight, LowerLeft, LowerRight }) { bool clearRemoveState = true; foreach (var s in GetOtherSquares(c)) { if (s?.State == States.Completing) { clearRemoveState = false; break; } } if (clearRemoveState && !c.IsJewelRemove) { c.RemoveState = Cell.RemoveStates.NotRemoved; } } } if (_prevState != State) { _prevState = State; StateChanged?.Invoke(this, EventArgs.Empty); } }
internal void ProcessJewel(Cell cell, Cell jeweled, bool removingSpecialJewelCells, bool[,] marker) { // prevent stack overflow if (marker[cell.Column, cell.Row]) { return; } marker[cell.Column, cell.Row] = true; if (cell == jeweled || cell.IsSameColor(jeweled)) { InProcessOfRemovingJewelCells = true; bool isJewelRemoving = cell.RemoveState == Cell.RemoveStates.JewelWillBeRemoved || cell.RemoveState == Cell.RemoveStates.JewelRemoving; if (!isJewelRemoving && !cell.IsRemove && !cell.IsJewelRemove) { cell.RemoveState = Cell.RemoveStates.JewelWillBeRemoved; cell.RemoveStateColumnAbs = Timeline.TotalColumnAbs; } if (Timeline.Column == cell.Column && cell.RemoveState == Cell.RemoveStates.JewelWillBeRemoved && removingSpecialJewelCells) { cell.RemoveState = Cell.RemoveStates.JewelRemoving; cell.RemoveStateColumnAbs = Timeline.TotalColumnAbs; } Cell um, ml, mr, lm; if (jeweled.IsJeweledVert) { if ((um = AdjacentCell(cell, Position.Up)) != null) { ProcessJewel(um, jeweled, removingSpecialJewelCells, marker); } if ((lm = AdjacentCell(cell, Position.Down)) != null) { ProcessJewel(lm, jeweled, removingSpecialJewelCells, marker); } } if (jeweled.IsJeweledHorz) { if ((ml = AdjacentCell(cell, Position.Left)) != null) { ProcessJewel(ml, jeweled, removingSpecialJewelCells, marker); } if ((mr = AdjacentCell(cell, Position.Right)) != null) { ProcessJewel(mr, jeweled, removingSpecialJewelCells, marker); } } } }