private void PerformCombo(Func <bool> completion) { var positions = new List <List <Vector2Int> >(); foreach (var termButton in termButtons) { var isOperandButton = termButton is OperandButton; if (!isOperandButton) { continue; } var y = termButton.GetPosition().y; var formula = new Formula(); for (var x = termButton.GetPosition().x; x < grid.Count; ++x) { var t = grid[x][y]; if (!formula.CanAppendTermButton(t)) { break; } formula.AppendTermButton(t); if (!formula.IsCorrect) { continue; } var group = new List <Vector2Int>(); for (var x2 = termButton.GetPosition().x; x2 <= x; ++x2) { var position = new Vector2Int(x2, y); group.Add(position); } var removedGroups = new List <List <Vector2Int> >(); var ok = true; foreach (var g in positions) { foreach (var position in g) { if (group.Contains(position)) { if (group.Count <= g.Count) { ok = false; } else { removedGroups.Add(g); } break; } } if (!ok) { break; } } if (ok) { foreach (var removedGroup in removedGroups) { positions.Remove(removedGroup); } positions.Add(group); } } } if (positions.Count == 0) { completion(); } else { RemoveTermButtons(positions, completion, true); } }