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

Sat, 25 Jul 2020 14:01:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 25 Jul 2020 14:01:28 +0200
changeset 9
576e7a2861ae
parent 8
src/de/uapcore/sudoku/Solver.java@e70a0e3555fb
child 10
369903afbb29
permissions
-rw-r--r--

converts to maven project

3
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
1 /*
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
2 * Copyright 2013 Mike Becker. All rights reserved.
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
3 *
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
4 * Redistribution and use in source and binary forms, with or without
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
5 * modification, are permitted provided that the following conditions are met:
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
6 *
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
7 * 1. Redistributions of source code must retain the above copyright
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
8 * notice, this list of conditions and the following disclaimer.
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
9 *
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
10 * 2. Redistributions in binary form must reproduce the above copyright
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
11 * notice, this list of conditions and the following disclaimer in the
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
12 * documentation and/or other materials provided with the distribution.
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
13 *
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
24 * POSSIBILITY OF SUCH DAMAGE.
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
25 */
ed931970b4ac added license and main menu
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
26
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 package de.uapcore.sudoku;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28
7
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
29 import java.util.ArrayList;
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
30 import java.util.List;
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
31
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 /**
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 *
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 * @author mike
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 */
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 public final class Solver {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 public Solver() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40
7
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
41 private Integer fillInCandidate(Field f, List<Integer>[][] candidates, int x, int y) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
42 Integer c = candidates[x][y].remove(0);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
43 f.setCellValue(x, y, c);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
44 f.setCellModified(x, y, true);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
45 for (int i = 0 ; i < 9 ; i++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
46 candidates[x][i].remove(c);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
47 candidates[i][y].remove(c);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
48 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
49 for (int i = 0 ; i < 3 ; i++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
50 for (int j = 0 ; j < 3 ; j++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
51 candidates[x-x%3+i][y-y%3+j].remove(c);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
52 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
53 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
54 return c;
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
55 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
56
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
57 private void makeBackup(List<Integer>[][] candidates, List<Integer>[][] candidatesBackup) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
58 for (int x = 0 ; x < 9 ; x++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
59 for (int y = 0 ; y < 9 ; y++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
60 candidatesBackup[x][y] = new ArrayList<>(9);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
61 candidatesBackup[x][y].addAll(candidates[x][y]);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
62 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
63 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
64 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
65
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
66 private void makeBackup(Field f, int[][] fieldBackup) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
67 for (int x = 0 ; x < 9 ; x++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
68 for (int y = 0 ; y < 9 ; y++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
69 fieldBackup[x][y] = f.getCellValue(x, y);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
70 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
71 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
72 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
73
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
74 private void restoreBackup(Field f, int[][] fieldBackup) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
75 for (int x = 0 ; x < 9 ; x++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
76 for (int y = 0 ; y < 9 ; y++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
77 f.setCellValue(x, y, fieldBackup[x][y]);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
78 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
79 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
80 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
81
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
82 private void restoreBackup(List<Integer>[][] candidates, List<Integer>[][] candidatesBackup) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
83 for (int x = 0 ; x < 9 ; x++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
84 for (int y = 0 ; y < 9 ; y++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
85 candidates[x][y].clear();
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
86 candidates[x][y].addAll(candidatesBackup[x][y]);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
87 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
88 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
89 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
90
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
91 private boolean solve(Field f, List<Integer>[][] candidates) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
92
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
93 // Make backup
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
94 List<Integer>[][] candidatesBackup = new List[9][9];
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
95 int[][] fieldBackup = new int[9][9];
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
96 makeBackup(candidates, candidatesBackup);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
97 makeBackup(f, fieldBackup);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
98
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
99 // Fill in distinct solutions
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
100 boolean fillDistinct;
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
101 do {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
102 fillDistinct = false;
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
103 for (int x = 0 ; x < 9 ; x++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
104 for (int y = 0 ; y < 9 ; y++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
105 if (f.isCellEmpty(x, y) && candidates[x][y].size() == 1) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
106 fillInCandidate(f, candidates, x, y);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
107 fillDistinct = true;
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
108 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
109 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
110 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
111 } while (fillDistinct);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
112
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
113 // Try out remaining candidates
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
114 for (int x = 0 ; x < 9 ; x++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
115 for (int y = 0 ; y < 9 ; y++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
116 if (f.isCellEmpty(x, y)) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
117 while (candidates[x][y].size() > 0) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
118 List<Integer>[][] cb = new List[9][9];
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
119 makeBackup(candidates, cb);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
120 Integer c = fillInCandidate(f, candidates, x, y);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
121 if (solve(f, candidates)) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
122 break;
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
123 } else {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
124 f.clearCellValue(x, y);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
125 restoreBackup(candidates, cb);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
126 // Remove current candidate anyway
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
127 candidates[x][y].remove(c);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
128 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
129 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
130 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
131 if (f.isCellEmpty(x, y)) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
132 restoreBackup(f, fieldBackup);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
133 restoreBackup(candidates, candidatesBackup);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
134 return false;
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
135 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
136 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
137 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
138
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
139 return true;
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
140 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
141
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
142 public boolean solve(Field f) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
143
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
144 // Calculate initial candidates
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
145 List<Integer> candidates[][] = new List[9][9];
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
146 for (int x = 0 ; x < 9 ; x++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
147 for (int y = 0 ; y < 9 ; y++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
148 candidates[x][y] = new ArrayList<>(9);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
149 if (f.getCellValue(x, y) == 0) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
150 // All numbers are candidates
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
151 for (int c = 1 ; c <= 9 ; c++) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
152 candidates[x][y].add(c);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
153 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
154 // Remove row duplicates
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
155 int[] line = f.getRow(y);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
156 for (Integer c : line) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
157 candidates[x][y].remove(c);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
158 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
159 // Remove column duplicates
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
160 line = f.getColumn(x);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
161 for (Integer c : line) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
162 candidates[x][y].remove(c);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
163 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
164 // Remove square duplicates
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
165 int[][] square = f.getSquare(x/3, y/3);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
166 for (int[] sq : square) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
167 for (Integer c : sq) {
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
168 candidates[x][y].remove(c);
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
169 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
170 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
171 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
172 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
173 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
174
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
175 // Backtrack
8
e70a0e3555fb removed completeness test - the solver automatically returns false when the field is incomplete
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
176 return solve(f, candidates);
7
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
177 }
2c0a2766461c added solving algorithm
Mike Becker <universe@uap-core.de>
parents: 5
diff changeset
178
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 public boolean check(Field f) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 int line[];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 for (int i = 0 ; i < 9 ; i++) {
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
182 line = f.getRow(i);
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 if (!valid(line)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 }
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
186 line = f.getColumn(i);
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 if (!valid(line)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 int square[][];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 for (int x = 0 ; x < 3 ; x++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 for (int y = 0 ; y < 3 ; y++) {
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
195 square = f.getSquare(x, y);
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 if (!valid(square)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 return true;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 private boolean valid(int[] line) {
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
206 int numbers[];
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
207 numbers = new int[9];
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 for (int i = 0 ; i < 9 ; i++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 int l = line[i]-1;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 if (l >= 0) {
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
211 if ((++numbers[l]) > 1) {
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 return true;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 private boolean valid(int[][] square) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 int[] line = new int[9];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 for (int x = 0 ; x < 3 ; x++) {
5
8ddf4af937d7 moved field methods to field class + added (parts of the) document handler
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
223 System.arraycopy(square[x], 0, line, 3*x, 3);
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 return valid(line);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 }

mercurial