public bool HasSameCandidates(SudokuItem item) { bool rtVal = false; if (item != null) { List <int> lst = new List <int>(); for (int k = 1; k < Candidates.Length; k++) { if (Candidates[k]) { lst.Add(k); } } for (int k = 1; k < item.Candidates.Length; k++) { if (item.Candidates[k]) { if (lst.Contains(k)) { lst.Remove(k); } else { return(rtVal); } } } rtVal = lst.Count == 0; } return(rtVal); }
public Sudoku(int n) { this.n = n; this.n2 = n * n; Items = new SudokuItem[n2][]; for (int j = 0; j < Items.Length; j++) { Items[j] = new SudokuItem[n2]; for (int i = 0; i < Items[j].Length; i++) { Items[j][i] = new SudokuItem(this, n, i, j, 0); } } Blocks = new SudokuBlock[n][]; for (int j = 0; j < Blocks.Length; j++) { Blocks[j] = new SudokuBlock[n]; for (int i = 0; i < Blocks[j].Length; i++) { Blocks[j][i] = new SudokuBlock(this, n, i, j); } } for (int bj = 0; bj < Blocks.Length; bj++) { for (int bi = 0; bi < Blocks[bj].Length; bi++) { SudokuBlock block = Blocks[bj][bi]; for (int k = 0; k < block.Items.Length; k++) { int i = k % n; int j = k / n; block.Items[k] = Items[bj * n + j][bi * n + i]; } } } Rows = new SudokuRow[n2]; for (int j = 0; j < n2; j++) { Rows[j] = new SudokuRow(this, n, j); for (int i = 0; i < Rows[j].Items.Length; i++) { Rows[j].Items[i] = Items[j][i]; } } Columns = new SudokuColumn[n2]; for (int i = 0; i < n2; i++) { Columns[i] = new SudokuColumn(this, n, i); for (int j = 0; j < Columns[i].Items.Length; j++) { Columns[i].Items[j] = Items[j][i]; } } }
public bool ClearCandidate(int c) { bool rtVal = false; for (int i = 0; i < Items.Length; i++) { SudokuItem item = Items[i]; rtVal |= item.ClearCandidate(c); } return(rtVal); }
private void btnGo_Click(object sender, EventArgs e) { try { int i = Convert.ToInt32(txtI.Text); int j = Convert.ToInt32(txtJ.Text); SudokuItem item = _sudoku.GetItem(i, j); if (item != null) { txtCandidates.Text = string.Join(",", item.GetCandidateNumbers()); } } catch (Exception) { // } }
public bool TryHiddenSingles() { bool rtVal = false; Dictionary <int, List <SudokuItem> > candidates = new Dictionary <int, List <SudokuItem> >(); for (int k = 1; k <= n2; k++) { candidates[k] = new List <SudokuItem>(); } foreach (SudokuItem item in Items) { if (item.value > 0) { continue; } if (item.HasSingleCandidate(out int c)) { item.SetValue(c); rtVal = true; return(rtVal); } for (int k = 1; k < item.Candidates.Length; k++) { if (item.Candidates[k] && !candidates[k].Contains(item)) { candidates[k].Add(item); } } } foreach (var pair in candidates) { if (pair.Value.Count == 1) { rtVal = true; SudokuItem item = pair.Value[0]; item.SetValue(pair.Key); } } return(rtVal); }
public bool ClearCandidates(IEnumerable <int> lst, IEnumerable <SudokuItem> excludeList) { bool rtVal = false; for (int i = 0; i < Items.Length; i++) { SudokuItem item = Items[i]; if (item.value > 0) { continue; } if (excludeList == null) { rtVal |= item.ClearCandidates(lst); } else if (!excludeList.Contains(item)) { rtVal |= item.ClearCandidates(lst); } } return(rtVal); }
public bool TryPointing() { bool rtVal = false; // Row for (int k = 0; k < n; k++) { List <SudokuItem> blockRow = new List <SudokuItem>(); HashSet <int> candidatesRow = new HashSet <int>(); for (int l = 0; l < n; l++) { SudokuItem item = Items[k * n + l]; blockRow.Add(item); int[] numbers = item.GetCandidateNumbers(); foreach (int n in numbers) { candidatesRow.Add(n); } } List <SudokuItem> blockRestItems = Items.Except(blockRow).ToList(); HashSet <int> candidatesRestItems = new HashSet <int>(); foreach (SudokuItem item in blockRestItems) { int[] numbers = item.GetCandidateNumbers(); foreach (int n in numbers) { candidatesRestItems.Add(n); } } IEnumerable <int> result = candidatesRow.Except(candidatesRestItems); if (result.Any()) { SudokuRow row = Sudoku.Rows[blockRow[0].j]; row.ClearCandidates(result, blockRow); } } // Column for (int k = 0; k < n; k++) { List <SudokuItem> blockCol = new List <SudokuItem>(); HashSet <int> candidatesCol = new HashSet <int>(); for (int l = 0; l < n; l++) { SudokuItem item = Items[k + l * n]; blockCol.Add(item); int[] numbers = item.GetCandidateNumbers(); foreach (int n in numbers) { candidatesCol.Add(n); } } List <SudokuItem> blockRestItems = Items.Except(blockCol).ToList(); HashSet <int> candidatesRestItems = new HashSet <int>(); foreach (SudokuItem item in blockRestItems) { int[] numbers = item.GetCandidateNumbers(); foreach (int n in numbers) { candidatesRestItems.Add(n); } } IEnumerable <int> result = candidatesCol.Except(candidatesRestItems); if (result.Any()) { SudokuColumn col = Sudoku.Columns[blockCol[0].i]; col.ClearCandidates(result, blockCol); } } return(rtVal); }