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

universe@3 1 /*
universe@3 2 * Copyright 2013 Mike Becker. All rights reserved.
universe@3 3 *
universe@3 4 * Redistribution and use in source and binary forms, with or without
universe@3 5 * modification, are permitted provided that the following conditions are met:
universe@3 6 *
universe@3 7 * 1. Redistributions of source code must retain the above copyright
universe@3 8 * notice, this list of conditions and the following disclaimer.
universe@3 9 *
universe@3 10 * 2. Redistributions in binary form must reproduce the above copyright
universe@3 11 * notice, this list of conditions and the following disclaimer in the
universe@3 12 * documentation and/or other materials provided with the distribution.
universe@3 13 *
universe@3 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@3 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@3 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@3 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@3 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@3 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@3 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@3 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@3 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@3 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@3 24 * POSSIBILITY OF SUCH DAMAGE.
universe@3 25 */
universe@3 26
universe@2 27 package de.uapcore.sudoku;
universe@2 28
universe@2 29 /**
universe@2 30 *
universe@2 31 * @author mike
universe@2 32 */
universe@2 33 public final class Solver {
universe@2 34
universe@2 35 public Solver() {
universe@2 36 }
universe@2 37
universe@2 38 public boolean check(Field f) {
universe@2 39 int line[];
universe@2 40 for (int i = 0 ; i < 9 ; i++) {
universe@2 41 line = getRow(f, i);
universe@2 42 if (!valid(line)) {
universe@2 43 return false;
universe@2 44 }
universe@2 45 line = getColumn(f, i);
universe@2 46 if (!valid(line)) {
universe@2 47 return false;
universe@2 48 }
universe@2 49 }
universe@2 50
universe@2 51 int square[][];
universe@2 52 for (int x = 0 ; x < 3 ; x++) {
universe@2 53 for (int y = 0 ; y < 3 ; y++) {
universe@2 54 square = getSquare(f, x, y);
universe@2 55 if (!valid(square)) {
universe@2 56 return false;
universe@2 57 }
universe@2 58 }
universe@2 59 }
universe@2 60
universe@2 61 return true;
universe@2 62 }
universe@2 63
universe@2 64 private boolean complete(int[][] square) {
universe@2 65 for (int x = 0 ; x < 3 ; x++) {
universe@2 66 for (int y = 0 ; y < 3 ; y++) {
universe@2 67 if (square[x][y] == 0) {
universe@2 68 return false;
universe@2 69 }
universe@2 70 }
universe@2 71 }
universe@2 72 return true;
universe@2 73 }
universe@2 74
universe@2 75 private boolean complete(int[] line) {
universe@2 76 for (int i = 0 ; i < 9 ; i++) {
universe@2 77 if (line[i] == 0) {
universe@2 78 return false;
universe@2 79 }
universe@2 80 }
universe@2 81 return true;
universe@2 82 }
universe@2 83
universe@2 84 private boolean valid(int[] line) {
universe@2 85 int numbers[] = new int[9];
universe@2 86 for (int i = 0 ; i < 9 ; i++) {
universe@2 87 int l = line[i]-1;
universe@2 88 if (l >= 0) {
universe@2 89 if (++numbers[l] > 1) {
universe@2 90 return false;
universe@2 91 }
universe@2 92 }
universe@2 93 }
universe@2 94
universe@2 95 return true;
universe@2 96 }
universe@2 97
universe@2 98 private boolean valid(int[][] square) {
universe@2 99 int[] line = new int[9];
universe@2 100 for (int x = 0 ; x < 3 ; x++) {
universe@2 101 for (int y = 0 ; y < 3 ; y++) {
universe@2 102 line[3*x+y] = square[x][y];
universe@2 103 }
universe@2 104 }
universe@2 105 return valid(line);
universe@2 106 }
universe@2 107
universe@2 108 private int[][] getSquare(Field f, int x, int y) {
universe@2 109 if (x < 0 || x > 2 || y < 0 || y > 2) {
universe@2 110 throw new IllegalArgumentException("Invalid square coordinates");
universe@2 111 }
universe@2 112 int[][] square = new int[3][3];
universe@2 113
universe@2 114 for (int u = 0 ; u < 3 ; u++) {
universe@2 115 for (int v = 0 ; v < 3 ; v++) {
universe@2 116 square[u][v] = f.getCellValue(3*x+u, 3*y+v);
universe@2 117 }
universe@2 118 }
universe@2 119
universe@2 120 return square;
universe@2 121 }
universe@2 122
universe@2 123 private int[] getRow(Field f, int y) {
universe@2 124 if (y < 0 || y > 8) {
universe@2 125 throw new IllegalArgumentException("Invalid row number");
universe@2 126 }
universe@2 127 int row[] = new int[9];
universe@2 128
universe@2 129 for (int x = 0 ; x < 9 ; x++) {
universe@2 130 row[x] = f.getCellValue(x, y);
universe@2 131 }
universe@2 132
universe@2 133 return row;
universe@2 134 }
universe@2 135
universe@2 136 private int[] getColumn(Field f, int x) {
universe@2 137 if (x < 0 || x > 8) {
universe@2 138 throw new IllegalArgumentException("Invalid column number");
universe@2 139 }
universe@2 140 int column[] = new int[9];
universe@2 141
universe@2 142 for (int y = 0 ; y < 9 ; y++) {
universe@2 143 column[y] = f.getCellValue(x, y);
universe@2 144 }
universe@2 145
universe@2 146 return column;
universe@2 147 }
universe@2 148 }

mercurial