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 void ClearRowColumnBlockCandidates(int v) { SudokuRow row = Sudoku.Rows[j]; row.ClearCandidate(v); SudokuColumn col = Sudoku.Columns[i]; col.ClearCandidate(v); SudokuBlock block = Sudoku.GetBlock(i, j); block.ClearCandidate(v); }
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); }