-5

I'm new to programming and am trying to teach myself.

I found this Sudoku solving algorithm online but I can't seem to run it - I think I have to create a main method but I don't know what parameters to put inside it and how to call other methods.

Can someone show me an example of what I can do? Thanks! :)

public class Sudoku {
  private static int mBoard[][];
  private static int mBoardSize = 9;
  private static int mBoxSize = 3;
  private boolean mRowSubset[][];
  private boolean mColSubset[][];
  private boolean mBoxSubset[][];

  public Sudoku(int board[][]) {
    mBoard = board;
    mBoardSize = mBoard.length;
    mBoxSize = (int) Math.sqrt(mBoardSize);
  }

  public void initSubsets() {
    mRowSubset = new boolean[mBoardSize][mBoardSize];
    mColSubset = new boolean[mBoardSize][mBoardSize];
    mBoxSubset = new boolean[mBoardSize][mBoardSize];
    for (int i = 0; i < mBoard.length; i++) {
      for (int j = 0; j < mBoard.length; j++) {
        int value = mBoard[i][j];
        if (value != 0) {
          setSubsetValue(i, j, value, true);
        }
      }
    }
  }

  private void setSubsetValue(int i, int j, int value, boolean present) {
    mRowSubset[i][value - 1] = present;
    mColSubset[j][value - 1] = present;
    mBoxSubset[computeBoxNo(i, j)][value - 1] = present;
  }

  public boolean solve() {
    return solve(0, 0);
  }

  public boolean solve(int i, int j) {
    if (i == mBoardSize) {
      i = 0;
      if (++j == mBoardSize) {
        return true;
      }
    }
    if (mBoard[i][j] != 0) {
      return solve(i + 1, j);
    }
    for (int value = 1; value <= mBoardSize; value++) {
      if (isValid(i, j, value)) {
        mBoard[i][j] = value;
        setSubsetValue(i, j, value, true);
        if (solve(i + 1, j)) {
          return true;
        }
        setSubsetValue(i, j, value, false);
      }
    }

    mBoard[i][j] = 0;
    return false;
  }

  private boolean isValid(int i, int j, int val) {
    val--;
    boolean isPresent = mRowSubset[i][val] || mColSubset[j][val] || mBoxSubset[computeBoxNo(i, j)][val];
    return !isPresent;
  }

  private int computeBoxNo(int i, int j) {
    int boxRow = i / mBoxSize;
    int boxCol = j / mBoxSize;
    return boxRow * mBoxSize + boxCol;
  }

  public void print() {
    for (int i = 0; i < mBoardSize; i++) {
      if (i % mBoxSize == 0) {
        System.out.println(" -----------------------");
      }
      for (int j = 0; j < mBoardSize; j++) {
        if (j % mBoxSize == 0) {
          System.out.print("| ");
        }
        System.out.print(mBoard[i][j] != 0 ? ((Object) (Integer.valueOf(mBoard[i][j]))) : " ");
        System.out.print(' ');
      }

      System.out.println("|");
    }

    System.out.println(" -----------------------");
  }

  public static void main(String[] args) {
    Sudoku sudoku = new Sudoku(mBoard);

  }
}
Mimi
  • 21
  • 3
  • I haven't yet - should I not initialise it in the main method? – Mimi Oct 07 '16 at 08:19
  • 4
    you are new to programming and started to learn about AI algorithm that solves sudoku ? – Mohsen_Fatemi Oct 07 '16 at 08:20
  • 2
    When you are so beginner, why are you taking complex program like Sudoku instead of sort of hello world type of program? – Pradeep Simha Oct 07 '16 at 08:20
  • In your main, for now, mBoard is meaningless, try to assign the right value for it (e.g: 9x9 sudoku board) – Erik Oct 07 '16 at 08:21
  • i see many things in this code that you must learn , learn about `Math` class , about writing `methods` , working with `arrays` , the meaning of `2DArrays` and ... – Mohsen_Fatemi Oct 07 '16 at 08:22
  • I've initialised mBoardSize to 9 and mBoxSize to 3 – Mimi Oct 07 '16 at 08:23
  • Make a 2d array that represents a *valid* sudoku puzzle. Pass it into it and go from there. – ChiefTwoPencils Oct 07 '16 at 08:33
  • 2
    Yea, you need to be realistic about the steps you take in learning to program. Start with a basic "Hello World" tutorial. It's like you've chosen to compete in a rally race before you've ever even sat in a car. Skipping ahead won't help you in the long run. – solidcell Oct 07 '16 at 08:38

1 Answers1

0

You can try something like that (you should change values, size?):

public static void main(String[] args)
{
    //list of rows, where each row has 4 values
    int[][] board = new int[][] { {2,3,1,1}, {1,2,1,1},{2,3,3,3}, {4,2,3,3}};
    Sudoku sudoku = new Sudoku(board);
    sudoku.solve();
    sudoku.print();
}
olsli
  • 831
  • 6
  • 8