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