Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your initializeGrid is wrong. IMHO, it should be:</p> <pre><code>for (int c = 0; c &lt; puzzle[r].length; c++) </code></pre> <p>instead of</p> <pre><code>for (int c = 0; c &lt; puzzle[0].length; c++) </code></pre> <p>EDIT: my answer below this</p> <p>It's been indented properly (lesson 1) and arranged the curly braces accordingly (lesson 2). Learn to understand what you are trying to do line by line and when you are stuck on a specific line or method call, look for help (lesson 3). The code below will compile but will not (yet) solve the puzzle. Read and replace the comments inside solvePuzzle for me (lesson 4). Do some thinking and analyzing since this is your homework ;) Good luck!</p> <pre><code>public class SudokuSolver { public static void initializeGrid(int[][] grid, int[][] puzzle) { for (int r = 0; r &lt; puzzle.length; r++) { for (int c = 0; c &lt; puzzle[0].length; c++) { grid [r][c] = puzzle [r][c]; } } } public static void displayDigits(int r, int c, int[][] grid) { if (grid[r][c] == 0) { System.out.print('0'); } else { System.out.print(grid[r][c]); } } public static void displayGrid(int[][] grid) { for (int r = 0; r &lt; grid.length; r++) { if (r % 3 == 0) { System.out.println("+---+---+---+"); } for (int c = 0; c &lt; grid[r].length; c++) { if (c % 3 == 0) { System.out.print("|"); } displayDigits(r, c, grid); } System.out.print("|"); System.out.println(); } System.out.println("+---+---+---+"); } public static int getEmptyCells(int[][] grid, int[][] emptyCells) { int i = 0; int numEmptyCells = 0; for (int r = 0; r &lt; grid.length; r++) { for (int c = 0; c &lt; grid[r].length; c++) { if (grid[r][c] == 0) { emptyCells[i][0] = r; emptyCells[i][1] = c; numEmptyCells++; i++; } } } return numEmptyCells; } private static boolean hasNoDuplicates(int[] digitsList) { for (int j = 0; j &lt; digitsList.length; j++) { for (int k = j + 1; k &lt; digitsList.length; k++) { if (digitsList[j] == digitsList[k] &amp;&amp; digitsList[j] != 0) return false; } } return true; } private static boolean checkCurrentRow(int[][] grid, int currentRow) { int[] digitsList = new int[grid.length]; for (int c = 0; c &lt; digitsList.length; c++) { digitsList[c] = grid[currentRow][c]; } if (hasNoDuplicates(digitsList)) { return true; } return false; } private static boolean checkCurrentCol(int[][] grid, int currentCol) { int[] digitsList = new int[grid.length]; for (int i = 0; i &lt; digitsList.length; i++) { digitsList[i] = grid[i][currentCol]; } if (hasNoDuplicates(digitsList)) { return true; } return false; } private static boolean checkCurrentRegion(int[][] grid, int currentRow, int currentCol) { int[] digitsList = new int[grid.length]; currentRow = (currentRow / 3) * 3; currentCol = (currentCol / 3) * 3; int i = 0; for (int r = 0; r &lt; 3; r++) { for (int c = 0; c &lt; 3; c++) { digitsList[i] = grid[currentRow + r][currentCol + c]; i++; } } if (hasNoDuplicates(digitsList)) { return true; } return false; } public static boolean isConsistent(int[][] grid, int currentRow, int currentCol) { boolean checkRow = checkCurrentRow(grid, currentRow); boolean checkCol = checkCurrentCol(grid, currentCol); boolean checkReg = checkCurrentRegion(grid, currentRow, currentCol); System.out.println("r: " + checkRow + " c: " + checkCol + " r: " + checkReg); if (checkRow &amp;&amp; checkCol &amp;&amp; checkReg) { return true; } return false; } public static boolean solvePuzzle(int[][] grid, int[][] emptyCells, int numEmptyCells) { int i = 0; int currentCellDigit = 0; while (i &lt; numEmptyCells) { if (currentCellDigit != 9) { // increment cell value currentCellDigit++; // assign to current cell the current cell value //&lt;var&gt; = currentCellDigit; System.out.println("Solving---------------------- :" + currentCellDigit + " R: " + emptyCells[i][0] + " C: " + emptyCells[i][1]); // check if value is valid if (isConsistent(grid, emptyCells[i][0], emptyCells[i][1])) { // reset after setting i++; currentCellDigit = 0; } else { // reset cell to zero instead of decrementing it since we're backtracking! //grid[emptyCells[i][0]][emptyCells[i][1]] = currentCellDigit - 1; //&lt;var&gt; = 0; } } else { // reset current cell to 0 //&lt;var&gt; = 0; // go to previous cell i--; // exit if theres no cell to backtrack to if (i &lt; 0) { return false; } // set previous' cell value as current cell value //&lt;var&gt; = grid[emptyCells[i][0]][emptyCells[i][1]]; } } return true; } public static void main(String[] args) { final int SIZE = 9; int[][] puzzle = { {0,2,9,0,0,3,0,0,5}, {5,0,7,0,0,0,0,9,0}, {6,0,0,0,0,9,4,2,0}, {3,0,2,0,0,4,0,0,0}, {0,0,5,0,3,0,7,0,0}, {0,0,0,5,0,0,6,0,2}, {0,9,8,4,0,0,0,0,3}, {0,3,0,0,0,0,1,0,6}, {2,0,0,3,0,0,9,4,0} }; int[][] grid = new int[SIZE][SIZE]; int[][] emptyCellsList = new int[SIZE*SIZE][2]; int numEmptyCells = 0; initializeGrid(grid, puzzle); numEmptyCells = getEmptyCells(grid, emptyCellsList); System.out.println("The puzzle:"); displayGrid(puzzle); if (solvePuzzle(grid, emptyCellsList, numEmptyCells)) { System.out.println("has been solved:"); displayGrid(grid); } else { System.out.println("cannot be solved!"); } } } </code></pre> <p>P.S. this will do backtracking if you've replaced the comments above with proper code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload