src/de/uapcore/sudoku/Solver.java

Sat, 26 Jan 2013 17:42:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 26 Jan 2013 17:42:07 +0100
changeset 2
5179eff8a9b6
child 3
ed931970b4ac
permissions
-rw-r--r--

check functions

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 = getRow(f, i);
            if (!valid(line)) {
                return false;
            }
            line = getColumn(f, i);
            if (!valid(line)) {
                return false;
            }
        }
        
        int square[][];
        for (int x = 0 ; x < 3 ; x++) {
            for (int y = 0 ; y < 3 ; y++) {
                square = getSquare(f, 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[] = 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++) {
            for (int y = 0 ; y < 3 ; y++) {
                line[3*x+y] = square[x][y];
            }    
        }
        return valid(line);
    }
    
    private int[][] getSquare(Field f, int x, int y) {
        if (x < 0 || x > 2 || y < 0 || y > 2) {
            throw new IllegalArgumentException("Invalid square coordinates");
        }
        int[][] square = new int[3][3];
        
        for (int u = 0 ; u < 3 ; u++) {
            for (int v = 0 ; v < 3 ; v++) {
                square[u][v] = f.getCellValue(3*x+u, 3*y+v);
            }
        }
        
        return square;
    }
    
    private int[] getRow(Field f, int y) {
        if (y < 0 || y > 8) {
            throw new IllegalArgumentException("Invalid row number");
        }
        int row[] = new int[9];
        
        for (int x = 0 ; x < 9 ; x++) {
            row[x] = f.getCellValue(x, y);
        }
        
        return row;
    }
    
    private int[] getColumn(Field f, int x) {
        if (x < 0 || x > 8) {
            throw new IllegalArgumentException("Invalid column number");
        }
        int column[] = new int[9];
        
        for (int y = 0 ; y < 9 ; y++) {
            column[y] = f.getCellValue(x, y);
        }
        
        return column;
    }
}

mercurial