-2

I'm trying to figure how answer to these question in my code:

create a method called getValidValues that: returns an array of 9 boolean values that corresponds to 9 digits (1-9) and, it is true if that digit can be placed in that position [row][column] without violating game rules.

This is my code:

public class SudokuClass {

private final int SIZE = 9;
boolean board = new int[SIZE][SIZE];
boolean[][] start = new boolean[SIZE][SIZE];

public SudokuClass() {
    for(int i=0; i < SIZE; i++) {
        for(int j=0; j < SIZE; j++) {
            board[i][j] = 0;
        }
    }
}

public String toString () {
      String result = "";
      for (int i = 0; i < SIZE; i++) {
        if (i % 3 == 0) {
          result = result + "+-------+-------+-------+\n";
        }
        for (int j = 0; j < SIZE; j++) {
          if (j % 3 == 0) {
            result = result + "| ";
          }
          if (scacchiera [i] [j] == 0) {
            result = result + "  ";
          } else {
            result = result + board[i][j] + " ";
          }
        }
        result = result + "|\n";
      }
      result = result + "+-------+-------+-------+\n";
      return result;
  }

public void addStartValues(int row,int col, int val) {
    board[row][col] = value;
    start[row][col] = true;
}

public void addMove(int row,int col,int val) {
    scacchiera[row][col] = val;
    inizio[row][col] = false;
}

public boolean verifyGame () {
    if (board.length != 9) {
      System.out.println("Board should have 9 rows");
      return false;
    }
    for (int i = 0; i < board.length; i++) {
      if (board[i].length != 9) {
        System.out.println("Row "+ i +" should have 9 cells.");
        return false;
      }
    }
    /* check each cell for conflicts */
    for (int i = 0; i < board.length; i++) {
      for (int j = 0; j < board.length; j++) {
        int cell = board[i][j];
        if (cell == 0) {
          continue;   /* blanks are always OK */
        }
        if ((cell < 1) || (cell > 9)) {
          System.out.println("Row "+ i +", column "+ j +" has value illegal "+ cell);
          return false;
        }
        /* does it match any other value in the same row? */
        for (int m = 0; m < board.length; m++) {
          if ((j != m) && (cell == board[i][m])) 
          {
            System.out.println("Row "+ i +" has "+ cell +" in position "+ j +" and "+ m);
            return false;
          }
        }
        /* does it match any other value it in the same column? */
        for (int k = 0; k < board.length; k++) {
          if ((i != k) && (cell == board[k][j])) {
            System.out.println("Column "+ j +" has "+ cell +" in position "+ i +" and "+ k);
            return false;
          }
        }
        /* does it match any other value in the 3x3? */
        for (int k = 0; k < 3; k++) {
          for (int m = 0; m < 3; m++) {
            int testRow = (i / 3 * 3) + k;   /* test this row */
            int testCol = (j / 3 * 3) + m;   /* test this col */
            if ((i != testRow) && (j != testCol) && (cell == board[testRow][testCol])) {
                    System.out.println("Value "+ cella +" at row "+ i +", column "+ j +" matches with value at row "+ testRow +", column "+ testColumn);
                    return false;
            }
          }
        }
      }
    }
    return true;
}

public int getValoreIn(int row, int col) {
    return scacchiera[row][col];
}

private boolean isInRow(int row, int num) {
    for (int i = 0; i < SIZE; i++)
        if (board[row][i] == num) {
            return true;
        }
    return false;
}

// we check if a possible number is already in a column
private boolean isInCol(int col, int number) {
    for (int i = 0; i < SIZE; i++)
        if (board[i][col] == number) {
            return true;
        }
    return false;
}

// we check if a possible number is in its 3x3 box
private boolean isInBox(int row, int col, int number) {
    int r = row - row % 3;
    int c = col - col % 3;

    for (int i = r; i < r + 3; i++)
        for (int j = c; j < c + 3; j++)
            if (board[i][j] == number) {
                return true;
            }
    return false;
}

public boolean[][] getValidValues(int row, int col) {
    boolean[][] validValues = new boolean[9][9];
    int[] digit = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    for(int i=0; i < digit.length; i++) {
        for(int j=0; j < digit.length; j++) {
            if(!isInRow(row,digit[i]) && !isInCol(col,digit[i]) && !isInBox(row,col,digit[i])) {
                validValues[i][j] = true;
            } else {
                validValues[i][j] = false;
            }
        }
    }
    return validValues;
}

I edited the code, adding other, private, methods called: isInRow, isInCol, isInBox. I thought to do this to get an easier way to implement the method getValidValues. What do you think about? Are there any suggestions?

1 Answers1

0

The main rule in sudoku is: all numbers in a column, a row and a 3x3 square have to be unique. Based on that you have to do three thing:

  • Iterate over all cells in the same column. If given column contains a number, set that number to invalid.
  • Same as above but for the row.
  • Find out the 3x3 square for the cell you're validating. It will start at coordinates like [floor(x/3), floor(y/3)]. Then you iterate over cells in that square and set numbers to invalid, just like above.

I hope that is enough to get you started. Don't want to post the code because that will take away the learning process.

Amongalen
  • 3,101
  • 14
  • 20
  • i'm trying like you said. I'll post the piece of code as i finish! – Andrea Manisi Mar 18 '19 at 09:39
  • 'public boolean[][] getValidValues(int row, int column) { boolean[][] validValues = new boolean[9][9]; int[] digit = {1, 2, 3, 4, 5, 6, 7, 8, 9}; for(int i=0; i < digit.length; i++) { for(int j=0; j < digit.length; j++) { if(!isInRow(row,digit[i]) && !isInCol(row,digit[i]) && !isInBox(row,column.digit[i])) { validValues[i][j] = true; } } } return valoriValidi; }' – Andrea Manisi Mar 18 '19 at 09:44
  • @AndreaManisi Don't post code in comments, it is totally unreadable. – Amongalen Mar 18 '19 at 09:50
  • @AndreaManisi you could edit your question and put your new code below. – Amongalen Mar 18 '19 at 09:54