Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you can use a 1D array, in much the same way a 1D array can model a binary tree. For example, to look at the value below a number, add 9 to the index.</p> <p>I just made this up, but could something like this work? </p> <pre><code>private boolean makePuzzle(int [] puzzle, int i) { for (int x = 0; x&lt; 10 ; x++) { if (//x satisfies all three conditions for the current square i) { puzzle[i]=x; if (i==80) return true //terminal condition, x fits in the last square else if makePuzzle(puzzle, i++);//find the next x return true; }// even though x fit in this square, an x couldn't be // found for some future square, try again with a new x } return false; //no value for x fit in the current square } public static void main(String[] args ) { int[] puzzle = new int[80]; makePuzzle(puzzle,0); // print out puzzle here } </code></pre> <p>Edit: its been a while since I've used arrays in Java, sorry if I screwed up any syntax. Please consider it pseudo code :)</p> <p>Here is the code as described below in my comment.</p> <pre><code>public class Sudoku { public int[] puzzle = new int[81]; private void makePuzzle(int[] puzzle, int i) { for (int x = 1; x&lt; 10 ; x++) { puzzle[i]=x; if(checkConstraints(puzzle)) { if (i==80)//terminal condition { System.out.println(this);//print out the completed puzzle puzzle[i]=0; return; } else makePuzzle(puzzle,i+1);//find a number for the next square } puzzle[i]=0;//this try didn't work, delete the evidence } } private boolean checkConstraints(int[] puzzle) { int test; //test that rows have unique values for (int column=0; column&lt;9; column++) { for (int row=0; row&lt;9; row++) { test=puzzle[row+column*9]; for (int j=0;j&lt;9;j++) { if(test!=0&amp;&amp; row!=j&amp;&amp;test==puzzle[j+column*9]) return false; } } } //test that columns have unique values for (int column=0; column&lt;9; column++) { for(int row=0; row&lt;9; row++) { test=puzzle[column+row*9]; for (int j=0;j&lt;9;j++) { if(test!=0&amp;&amp;row!=j&amp;&amp;test==puzzle[column+j*9]) return false; } } } //implement region test here int[][] regions = new int[9][9]; int[] regionIndex ={0,3,6,27,30,33,54,57,60}; for (int region=0; region&lt;9;region++) //for each region { int j =0; for (int k=regionIndex[region];k&lt;regionIndex[region]+27; k=(k%3==2?k+7:k+1)) { regions[region][j]=puzzle[k]; j++; } } for (int i=0;i&lt;9;i++)//region counter { for (int j=0;j&lt;9;j++) { for (int k=0;k&lt;9;k++) { if (regions[i][j]!=0&amp;&amp;j!=k&amp;&amp;regions[i][j]==regions[i][k]) return false; } } } return true; } public String toString() { String string= ""; for (int i=0; i &lt;9;i++) { for (int j = 0; j&lt;9;j++) { string = string+puzzle[i*9+j]; } string =string +"\n"; } return string; } public static void main(String[] args) { Sudoku sudoku=new Sudoku(); sudoku.makePuzzle(sudoku.puzzle, 0); } } </code></pre>
    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. This table or related slice is empty.
    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