Sat, 26 Jan 2013 19:34:31 +0100
moved field methods to field class + added (parts of the) document handler
1 /*
2 * Copyright 2013 Mike Becker. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
27 package de.uapcore.sudoku;
29 /**
30 *
31 * @author mike
32 */
33 public final class Solver {
35 public Solver() {
36 }
38 public boolean check(Field f) {
39 int line[];
40 for (int i = 0 ; i < 9 ; i++) {
41 line = f.getRow(i);
42 if (!valid(line)) {
43 return false;
44 }
45 line = f.getColumn(i);
46 if (!valid(line)) {
47 return false;
48 }
49 }
51 int square[][];
52 for (int x = 0 ; x < 3 ; x++) {
53 for (int y = 0 ; y < 3 ; y++) {
54 square = f.getSquare(x, y);
55 if (!valid(square)) {
56 return false;
57 }
58 }
59 }
61 return true;
62 }
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 }
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 }
84 private boolean valid(int[] line) {
85 int numbers[];
86 numbers = new int[9];
87 for (int i = 0 ; i < 9 ; i++) {
88 int l = line[i]-1;
89 if (l >= 0) {
90 if ((++numbers[l]) > 1) {
91 return false;
92 }
93 }
94 }
96 return true;
97 }
99 private boolean valid(int[][] square) {
100 int[] line = new int[9];
101 for (int x = 0 ; x < 3 ; x++) {
102 System.arraycopy(square[x], 0, line, 3*x, 3);
103 }
104 return valid(line);
105 }
106 }