Sun, 27 Jan 2013 15:03:57 +0100
file IO
/* * Copyright 2013 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ package de.uapcore.sudoku; /** * * @author mike */ public final class Solver { public Solver() { } public boolean check(Field f) { int line[]; for (int i = 0 ; i < 9 ; i++) { line = f.getRow(i); if (!valid(line)) { return false; } line = f.getColumn(i); if (!valid(line)) { return false; } } int square[][]; for (int x = 0 ; x < 3 ; x++) { for (int y = 0 ; y < 3 ; y++) { square = f.getSquare(x, y); if (!valid(square)) { return false; } } } return true; } private boolean complete(int[][] square) { for (int x = 0 ; x < 3 ; x++) { for (int y = 0 ; y < 3 ; y++) { if (square[x][y] == 0) { return false; } } } return true; } private boolean complete(int[] line) { for (int i = 0 ; i < 9 ; i++) { if (line[i] == 0) { return false; } } return true; } private boolean valid(int[] line) { int numbers[]; numbers = new int[9]; for (int i = 0 ; i < 9 ; i++) { int l = line[i]-1; if (l >= 0) { if ((++numbers[l]) > 1) { return false; } } } return true; } private boolean valid(int[][] square) { int[] line = new int[9]; for (int x = 0 ; x < 3 ; x++) { System.arraycopy(square[x], 0, line, 3*x, 3); } return valid(line); } }