public void ClaimAny() { foreach (var cell in PossCells.Where(cell => cell.IsOnlyClue(this) && cell.cell.Value == 1)) { cell.Claim(this); } }
public void UpdateSections() { CompleteAny(); //if (PossSections.Any(l => l.Count < clue.Value) || PossCells.Any(c => c.cell.Value == -1 || c.IsAvaliable(this) == false)) { var sections = new List <List <CellSolver> >(); var section = new List <CellSolver>(); foreach (var cell in OwnerLine.cellSolvers) { if (PossCells.Contains(cell) && cell.cell.Value != -1 && cell.IsAvaliable(this)) { section.Add(cell); } else if (section.Count > 0) { if (section.Count >= clue.Value) { sections.Add(section); } section = new List <CellSolver>(); } } if (section.Count >= clue.Value) { sections.Add(section); } if (sections.Count == 0) { throw new InvalidPuzzleException(String.Format("No room for clue: {0} {1}", clue.OwnerLine.Key, clue.OwnerLine.IsRow)); } for (int i = 0; i < sections.Count; i++) { if (sections.Count != PossSections.Count) { OwnerLine.OwnerPuzzle.Changed = true; break; } if (!sections[i].SequenceEqual(PossSections[i])) { OwnerLine.OwnerPuzzle.Changed = true; break; } } PossSections = sections; UpdateEnds(); } }
private void FillSection(List <CellSolver> section) { //Normal Fill foreach (var cell in section) { if (clue.Key(cell.cell) < clue.Key(section.First().cell) + clue.Value && clue.Key(cell.cell) > clue.Key(section.Last().cell) - clue.Value) { cell.UpdateCell(1); cell.Claim(this); } } //Extend existing cells //TODO: backwards? and update var anchorCell = PossCells.FirstOrDefault(c => c.cell.Value == 1 && c.IsOnlyClue(this)); if (anchorCell != null) { foreach (var cell in PossCells) { if (clue.Key(cell.cell) > clue.Key(anchorCell.cell) && clue.Key(cell.cell) < Start + clue.Value) { cell.UpdateCell(1); cell.Claim(this); } if (clue.Key(cell.cell) >= clue.Key(anchorCell.cell) + clue.Value && cell.IsOnlyClue(this)) { cell.UpdateCell(-1); } } } var anchorCell2 = PossCells.LastOrDefault(c => c.cell.Value == 1 && c.IsOnlyClue(this)); if (anchorCell2 != null) { foreach (var cell in PossCells) { if (clue.Key(cell.cell) < clue.Key(anchorCell2.cell) && clue.Key(cell.cell) > End - clue.Value) { cell.UpdateCell(1); cell.Claim(this); } if (clue.Key(cell.cell) <= clue.Key(anchorCell2.cell) - clue.Value && cell.IsOnlyClue(this)) { cell.UpdateCell(-1); } } } }
public void CompleteAny() { //right number of poss cells if (End - Start == clue.Value) { Complete(PossCells); return; } //Find filled confirmed clue + all filled consecutive var x = PossCells.FirstOrDefault(c => (c.cell.Value == 1 && c.IsAvaliable(this) && c.IsOnlyClue(this))); if (x == null) { return; } var cells = new List <CellSolver>(); cells.Add(x); //Consequetive cells to left for (int i = clue.Key(x.cell) - 1; i > 0; i--) { var cell = PossCells.SingleOrDefault(c => clue.Key(c.cell) == i); if (cell != null && cell.cell.Value == 1) { if (!cell.IsAvaliable(this)) { throw new Exception("Trying to claim unavailiable cell"); } cells.Add(cell); } else { break; } } //Consequetive cells to right for (int i = clue.Key(x.cell) + 1; i < OwnerLine.line.Length; i++) { var cell = PossCells.SingleOrDefault(c => clue.Key(c.cell) == i); if (cell != null && cell.cell.Value == 1) { if (!cell.IsAvaliable(this)) { throw new Exception("Trying to claim unavailiable cell"); } cells.Add(cell); } else { break; } } foreach (var cell in cells) { cell.Claim(this); } Start = cells.Last().cell.GetKey(OwnerLine.line) - clue.Value; End = cells.First().cell.GetKey(OwnerLine.line) + clue.Value; //Complete if right number of filled cells if (cells.Count == clue.Value) { Complete(cells); } }