Ask Sudoku

Ask Sudoku

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;

import java.util.LinkedList;
import java.util.List;

public class GridUtils {

  public static boolean oneChoice(boolean[] choices){
    boolean result = false;
    for (int i = 1; i <= 9; i++) {
      if (choices[i]){
        if (result) return false;
        else result = true;
      }
    }
    return result;
  }
  
  public static boolean isValid(boolean[] choices){
    for (int i = 1; i <= 9; i++) {
      if (choices[i]) return true;
    }
    return false;
  } 
  
  public static int getValue(boolean[] choices){
    if (!oneChoice(choices)) throw new IllegalArgumentException("Should only contain one choice");
    for (int i = 1; i <= 9; i++) {
      if (choices[i]) return i;
    }
    return 0;
  }
  
  public static List choiceToList(boolean[] choices){
    List result = new LinkedList();
    for (int i = 1; i <= 9; i++) {
      if (choices[i]){
        result.add(i);
      }
    }
    return result;
  }
  
}