Sat, 26 Jan 2013 18:38:12 +0100
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 | 27 | package de.uapcore.sudoku; |
28 | ||
29 | /** | |
30 | * | |
31 | * @author mike | |
32 | */ | |
33 | public final class Solver { | |
34 | ||
35 | public Solver() { | |
36 | } | |
37 | ||
38 | public boolean check(Field f) { | |
39 | int line[]; | |
40 | for (int i = 0 ; i < 9 ; i++) { | |
41 | line = getRow(f, i); | |
42 | if (!valid(line)) { | |
43 | return false; | |
44 | } | |
45 | line = getColumn(f, i); | |
46 | if (!valid(line)) { | |
47 | return false; | |
48 | } | |
49 | } | |
50 | ||
51 | int square[][]; | |
52 | for (int x = 0 ; x < 3 ; x++) { | |
53 | for (int y = 0 ; y < 3 ; y++) { | |
54 | square = getSquare(f, x, y); | |
55 | if (!valid(square)) { | |
56 | return false; | |
57 | } | |
58 | } | |
59 | } | |
60 | ||
61 | return true; | |
62 | } | |
63 | ||
64 | private boolean complete(int[][] square) { | |
65 | for (int x = 0 ; x < 3 ; x++) { | |
66 | for (int y = 0 ; y < 3 ; y++) { | |
67 | if (square[x][y] == 0) { | |
68 | return false; | |
69 | } | |
70 | } | |
71 | } | |
72 | return true; | |
73 | } | |
74 | ||
75 | private boolean complete(int[] line) { | |
76 | for (int i = 0 ; i < 9 ; i++) { | |
77 | if (line[i] == 0) { | |
78 | return false; | |
79 | } | |
80 | } | |
81 | return true; | |
82 | } | |
83 | ||
84 | private boolean valid(int[] line) { | |
85 | int numbers[] = new int[9]; | |
86 | for (int i = 0 ; i < 9 ; i++) { | |
87 | int l = line[i]-1; | |
88 | if (l >= 0) { | |
89 | if (++numbers[l] > 1) { | |
90 | return false; | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | return true; | |
96 | } | |
97 | ||
98 | private boolean valid(int[][] square) { | |
99 | int[] line = new int[9]; | |
100 | for (int x = 0 ; x < 3 ; x++) { | |
101 | for (int y = 0 ; y < 3 ; y++) { | |
102 | line[3*x+y] = square[x][y]; | |
103 | } | |
104 | } | |
105 | return valid(line); | |
106 | } | |
107 | ||
108 | private int[][] getSquare(Field f, int x, int y) { | |
109 | if (x < 0 || x > 2 || y < 0 || y > 2) { | |
110 | throw new IllegalArgumentException("Invalid square coordinates"); | |
111 | } | |
112 | int[][] square = new int[3][3]; | |
113 | ||
114 | for (int u = 0 ; u < 3 ; u++) { | |
115 | for (int v = 0 ; v < 3 ; v++) { | |
116 | square[u][v] = f.getCellValue(3*x+u, 3*y+v); | |
117 | } | |
118 | } | |
119 | ||
120 | return square; | |
121 | } | |
122 | ||
123 | private int[] getRow(Field f, int y) { | |
124 | if (y < 0 || y > 8) { | |
125 | throw new IllegalArgumentException("Invalid row number"); | |
126 | } | |
127 | int row[] = new int[9]; | |
128 | ||
129 | for (int x = 0 ; x < 9 ; x++) { | |
130 | row[x] = f.getCellValue(x, y); | |
131 | } | |
132 | ||
133 | return row; | |
134 | } | |
135 | ||
136 | private int[] getColumn(Field f, int x) { | |
137 | if (x < 0 || x > 8) { | |
138 | throw new IllegalArgumentException("Invalid column number"); | |
139 | } | |
140 | int column[] = new int[9]; | |
141 | ||
142 | for (int y = 0 ; y < 9 ; y++) { | |
143 | column[y] = f.getCellValue(x, y); | |
144 | } | |
145 | ||
146 | return column; | |
147 | } | |
148 | } |