src/de/uapcore/sudoku/Solver.java

Sat, 26 Jan 2013 18:38:12 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 18:38:12 +0100
changeset 3
ed931970b4ac
parent 2
5179eff8a9b6
child 5
8ddf4af937d7
permissions
-rw-r--r--

added license and main menu

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
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 /**
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 *
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 * @author mike
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 public final class Solver {
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 public Solver() {
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 public boolean check(Field f) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 int line[];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 for (int i = 0 ; i < 9 ; i++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 line = getRow(f, i);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 if (!valid(line)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 return false;
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 line = getColumn(f, i);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 if (!valid(line)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 return false;
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 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 int square[][];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 for (int x = 0 ; x < 3 ; x++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 for (int y = 0 ; y < 3 ; y++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 square = getSquare(f, x, y);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 if (!valid(square)) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 return false;
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 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 return true;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 private boolean complete(int[][] square) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 for (int x = 0 ; x < 3 ; x++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 for (int y = 0 ; y < 3 ; y++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 if (square[x][y] == 0) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 }
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 return true;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 private boolean complete(int[] line) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 for (int i = 0 ; i < 9 ; i++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 if (line[i] == 0) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 return false;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 }
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 return true;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 private boolean valid(int[] line) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 int numbers[] = new int[9];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 for (int i = 0 ; i < 9 ; i++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 int l = line[i]-1;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 if (l >= 0) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 if (++numbers[l] > 1) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 return false;
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
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 return true;
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
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 private boolean valid(int[][] square) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 int[] line = new int[9];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 for (int x = 0 ; x < 3 ; x++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 for (int y = 0 ; y < 3 ; y++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 line[3*x+y] = square[x][y];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 return valid(line);
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
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 private int[][] getSquare(Field f, int x, int y) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 if (x < 0 || x > 2 || y < 0 || y > 2) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 throw new IllegalArgumentException("Invalid square coordinates");
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 int[][] square = new int[3][3];
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 for (int u = 0 ; u < 3 ; u++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 for (int v = 0 ; v < 3 ; v++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 square[u][v] = f.getCellValue(3*x+u, 3*y+v);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 }
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 square;
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
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 private int[] getRow(Field f, int y) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 if (y < 0 || y > 8) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 throw new IllegalArgumentException("Invalid row number");
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 int row[] = new int[9];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 for (int x = 0 ; x < 9 ; x++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 row[x] = f.getCellValue(x, y);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 return row;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 private int[] getColumn(Field f, int x) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 if (x < 0 || x > 8) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 throw new IllegalArgumentException("Invalid column number");
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 int column[] = new int[9];
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 for (int y = 0 ; y < 9 ; y++) {
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 column[y] = f.getCellValue(x, y);
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 return column;
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 }
5179eff8a9b6 check functions
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 }

mercurial