src/main/java/de/uapcore/sudoku/Solver.java

changeset 12
1c62c6009161
parent 10
369903afbb29
equal deleted inserted replaced
11:f7433671fec5 12:1c62c6009161
146 * @return true if a solution could be found, false if the field is unsolvable 146 * @return true if a solution could be found, false if the field is unsolvable
147 */ 147 */
148 public boolean solve(Field f) { 148 public boolean solve(Field f) {
149 149
150 // Calculate initial candidates 150 // Calculate initial candidates
151 List<Integer> candidates[][] = new List[9][9]; 151 List<Integer>[][] candidates = new List[9][9];
152 for (int x = 0 ; x < 9 ; x++) { 152 for (int x = 0 ; x < 9 ; x++) {
153 for (int y = 0 ; y < 9 ; y++) { 153 for (int y = 0 ; y < 9 ; y++) {
154 candidates[x][y] = new ArrayList<>(9); 154 candidates[x][y] = new ArrayList<>(9);
155 if (f.getCellValue(x, y) == 0) { 155 if (f.getCellValue(x, y) == 0) {
156 // All numbers are candidates 156 // All numbers are candidates
187 * 187 *
188 * @param f the field to check 188 * @param f the field to check
189 * @return true, if the check succeeds, false otherwise 189 * @return true, if the check succeeds, false otherwise
190 */ 190 */
191 public boolean check(Field f) { 191 public boolean check(Field f) {
192 int line[]; 192 int[] line;
193 for (int i = 0 ; i < 9 ; i++) { 193 for (int i = 0 ; i < 9 ; i++) {
194 line = f.getRow(i); 194 line = f.getRow(i);
195 if (!valid(line)) { 195 if (!valid(line)) {
196 return false; 196 return false;
197 } 197 }
199 if (!valid(line)) { 199 if (!valid(line)) {
200 return false; 200 return false;
201 } 201 }
202 } 202 }
203 203
204 int square[][]; 204 int[][] square;
205 for (int x = 0 ; x < 3 ; x++) { 205 for (int x = 0 ; x < 3 ; x++) {
206 for (int y = 0 ; y < 3 ; y++) { 206 for (int y = 0 ; y < 3 ; y++) {
207 square = f.getSquare(x, y); 207 square = f.getSquare(x, y);
208 if (!valid(square)) { 208 if (!valid(square)) {
209 return false; 209 return false;
213 213
214 return true; 214 return true;
215 } 215 }
216 216
217 private boolean valid(int[] line) { 217 private boolean valid(int[] line) {
218 int numbers[]; 218 int[] numbers;
219 numbers = new int[9]; 219 numbers = new int[9];
220 for (int i = 0 ; i < 9 ; i++) { 220 for (int i = 0 ; i < 9 ; i++) {
221 int l = line[i]-1; 221 int l = line[i]-1;
222 if (l >= 0) { 222 if (l >= 0) {
223 if ((++numbers[l]) > 1) { 223 if ((++numbers[l]) > 1) {

mercurial