Code
We are happy to give you the code used for solving Sudoku. It's written is Java and it'is quiet simple. You may start by the class call Solver and follow the path.
You can reuse the code and consider it as in lgpl.
Solver.java Grid.java SolvingGrid.java Cell.java CellDecision.java GridUtils.java
package com.asksudoku.logic;
public class SolvingGrid implements Cloneable{
private boolean[][][] possibility;
private SolvingGrid(boolean[][][] possibility){
this.possibility = possibility;
}
public SolvingGrid(Grid g){
possibility = new boolean[9][9][10];
int[][] grid = g.getGrid();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (byte k = 1; k <= 9; k++) {
possibility[i][j][k] = true;
}
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (grid[i][j] != 0){
applyValue(i, j, grid[i][j]);
}
}
}
}
public Grid getGrid(){
int[][] result = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
result[i][j] = GridUtils.getValue(possibility[i][j]);
}
}
return new Grid(result);
}
public void applyValue(int x, int y, int value){
for (int i = 0; i < 9; i++) {
if (i != y) {
possibility[x][i][value] = false;
int v =0;
}
}
for (int i = 0; i < 9; i++) {
if (i != x){
possibility[i][y][value] = false;
}
}
int startx = (x/3)*3;
int starty = (y/3)*3;
for (int i = startx; i < startx+3; i++) {
for (int j = starty; j < starty+3; j++) {
if (i != x && j!=y){
possibility[i][j][value] = false;
}
}
}
for (int i = 1; i <= 9; i++) {
if (i != value){
possibility[x][y][i] = false;
}
}
}
public boolean isComplete() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (!GridUtils.oneChoice(possibility[i][j])){
return false;
}
}
}
return true;
}
public boolean isValid() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (!GridUtils.isValid(possibility[i][j])) return false;
}
}
for (int x = 0; x < 9; x++) {
boolean[] taken = new boolean[10];
for (int i = 0; i < 9; i++) {
if (GridUtils.oneChoice(possibility[x][i])){
int value = GridUtils.getValue(possibility[x][i]);
if (taken[value]) return false;
taken[value] = true;
}
}
}
for (int y = 0; y < 9; y++) {
boolean[] taken = new boolean[10];
for (int i = 0; i < 9; i++) {
if (GridUtils.oneChoice(possibility[i][y])){
int value = GridUtils.getValue(possibility[i][y]);
if (taken[value]) return false;
taken[value] = true;
}
}
}
for (int squarex = 0; squarex < 3; squarex++) {
for (int squarey = 0; squarey < 3; squarey++) {
boolean[] taken = new boolean[10];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (GridUtils.oneChoice(possibility[squarex*3+i][squarey*3+j])){
int value = GridUtils.getValue(possibility[squarex*3+i][squarey*3+j]);
if (taken[value]) return false;
taken[value] = true;
}
}
}
}
}
return true;
}
public CellDecision selectBestDecisions() {
CellDecision decision = null;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
decision = CellDecision.compare(decision, new CellDecision(i,j, possibility[i][j]));
}
}
return decision;
}
protected Object clone() {
boolean[][][] cloned = new boolean[9][9][10];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 0; k < 10; k++) {
cloned[i][j][k] = possibility[i][j][k];
}
}
}
return new SolvingGrid(cloned);
}
}



