3

I am doing this Sudoku solver in java, and for some reason I have an error in my code that I just cant fix. My code has a guess function where it guesses numbers from 1-9 in each box while it checks if the number is already written before.

The error is in the line:

 else if (board[r + (i % 3)][c + (i / 3)] == num)

Where i get an ArithmeticException (divide by 0) for some reason I can't see why. Hope you can help

My code:

public class SudokuSolver
{
final int size = 9;
private int box_size;

private int[][] board;

// Create an empty board
public SudokuSolver()
{
  board = new int[size][size];
  this.box_size = size / 3;

}

// initialize a given board
public SudokuSolver(int[][] board)
{
  this.board = board;
}

public void setCell(int num, int row, int col)
{
  board[row][col] = num;
}

public int getCell(int row, int col)
{
  return board[row][col];
}

private boolean check(int num, int row, int col)
{
  int r = (row / 3) * 3;
  int c = (col / 3) * 3;

  for (int i = 0; i < size; i++)
  {
     if (board[row][i] == num)
        return false;

     else if (board[i][col] == num)
        return false;

     else if (board[r + (i % box_size)][c + (i / box_size)] == num)
        return false;
  }
  return true;
  }

  public boolean guess(int row, int col)
  {
  int nextCol = (col + 1) % size;
  int nextRow = (nextCol == 0) ? row + 1 : row;

  try
  {
     if (board[row][col] != 0)
        return guess(nextRow, nextCol);
  }
  catch (ArrayIndexOutOfBoundsException e)
  {
     return true;
  }

  for (int i = 1; i <= size; i++)
  {
     if (check(i, row, col))
     {
        board[row][col] = i;
        if (guess(nextRow, nextCol))
        {
           return true;
        }
     }
  }
  board[row][col] = 0;
  return false;
  }

  public void printBoard()
  {
  for (int row = 0; row < size; row++)
  {
     for (int col = 0; col < size; col++)
     {
        System.out.print(board[row][col] + "  ");
     }
     System.out.println();
   }

  }



  public static void main(String[] args)
  {

  int[][] board = { { 0, 6, 0, 1, 0, 4, 0, 5, 0 },
        { 0, 0, 8, 3, 0, 5, 6, 0, 0 }, { 2, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 8, 0, 0, 4, 0, 7, 0, 0, 6 }, { 0, 0, 6, 0, 0, 0, 3, 0, 0 },
        { 7, 0, 0, 9, 0, 1, 0, 0, 4 }, { 5, 0, 0, 0, 0, 0, 0, 0, 2 },
        { 0, 0, 7, 2, 0, 6, 9, 0, 0 }, { 0, 4, 0, 5, 0, 8, 0, 7, 0 } };

  SudokuSolver ss = new SudokuSolver(board);
  ss.printBoard();
  System.out.println();
  System.out.println();
  if(ss.guess(0, 0))
     ss.printBoard();


 }
Ibrahim Yildirim
  • 2,731
  • 2
  • 19
  • 31

1 Answers1

3

Printing box_size before the point at which the error is encountered will reveal that it is in fact 0. You never initialize it in this constructor, so it retains its default value of 0:

public SudokuSolver(int[][] board) {
    this.board = board;
}

You probably meant to include a line like this.box_size = board.length / 3.


P.S. The real line that causes the error is

else if (board[r + (i % box_size)][c + (i / box_size)] == num)

You probably shouldn't have substituted box_size for what you expected it to be in your question.

arshajii
  • 127,459
  • 24
  • 238
  • 287