src/de/uapcore/sudoku/Solver.java

Sat, 26 Jan 2013 17:42:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 17:42:07 +0100
changeset 2
5179eff8a9b6
child 3
ed931970b4ac
permissions
-rw-r--r--

check functions

2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 package de.uapcore.sudoku;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 /**
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 *
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 * @author mike
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 */
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 public final class Solver {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 public Solver() {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 public boolean check(Field f) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 int line[];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 for (int i = 0 ; i < 9 ; i++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 line = getRow(f, i);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 if (!valid(line)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 line = getColumn(f, i);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 if (!valid(line)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 int square[][];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 for (int x = 0 ; x < 3 ; x++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 for (int y = 0 ; y < 3 ; y++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 square = getSquare(f, x, y);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 if (!valid(square)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 }
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
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 return true;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 }
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 private boolean complete(int[][] square) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 for (int x = 0 ; x < 3 ; x++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 for (int y = 0 ; y < 3 ; y++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 if (square[x][y] == 0) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 return true;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 private boolean complete(int[] line) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 for (int i = 0 ; i < 9 ; i++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 if (line[i] == 0) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 return true;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 private boolean valid(int[] line) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 int numbers[] = new int[9];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 for (int i = 0 ; i < 9 ; i++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 int l = line[i]-1;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 if (l >= 0) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 if (++numbers[l] > 1) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 return true;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 private boolean valid(int[][] square) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 int[] line = new int[9];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 for (int x = 0 ; x < 3 ; x++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 for (int y = 0 ; y < 3 ; y++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 line[3*x+y] = square[x][y];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 return valid(line);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 private int[][] getSquare(Field f, int x, int y) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 if (x < 0 || x > 2 || y < 0 || y > 2) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 throw new IllegalArgumentException("Invalid square coordinates");
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 int[][] square = new int[3][3];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 for (int u = 0 ; u < 3 ; u++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 for (int v = 0 ; v < 3 ; v++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 square[u][v] = f.getCellValue(3*x+u, 3*y+v);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 return square;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 private int[] getRow(Field f, int y) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 if (y < 0 || y > 8) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 throw new IllegalArgumentException("Invalid row number");
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 int row[] = new int[9];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 for (int x = 0 ; x < 9 ; x++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 row[x] = f.getCellValue(x, y);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 return row;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 private int[] getColumn(Field f, int x) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 if (x < 0 || x > 8) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 throw new IllegalArgumentException("Invalid column number");
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 int column[] = new int[9];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 for (int y = 0 ; y < 9 ; y++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 column[y] = f.getCellValue(x, y);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 return column;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 }

mercurial