• November 1st, 2009, 11:04 PM
MysticDeath
Well I've been working on a Sudoku Solver. I ran into a problem;
This is a typical Sudoku:
http://i166.photobucket.com/albums/u...er545/gene.jpg

So here's my problem;
In my code, the way I'm doing it is I have 81 instances of a "cell" class, representing one of the possible values on the Sudoku.
However, for a given row ex:
1 2 3 4 5 6 7 8 9
I have an array of cells;
0 1 2 3 4 5 6 7 8 would be my first row
9 10 11 12 13 14 15 16 17would be my second row.

So I need a clever way to add cells to each appropriate "box"
so for box one, the numbers in the box would be 0 1 2 from the first row, 9 10 11 from the second row.
Box two would be 4 5 6 from the first row, and 12 13 14 from the second etc etc
Anyone have any ideas?
• November 2nd, 2009, 12:15 AM
helloworld922
I'd use a 9*9 2D array of integers

Code :

`int[][] sodoku = new int[9][9];`

Then you don't need to create a Cell class, or have to do any math to get the location of a Cell.
• November 2nd, 2009, 12:17 AM
MysticDeath
```public class GetDataMethods { int emptycells[]; int rows[]=new int[9]; int cols[]=new int[9]; int[][] newboard = {{0,0,0,0,6,0,0,0,9}, {0,6,0,0,0,0,3,0,1}, {2,0,0,0,0,9,8,0,0}, {1,9,6,0,0,2,0,0,8}, {0,0,0,3,0,8,0,0,0}, {8,0,0,9,0,0,1,7,5}, {0,0,1,7,0,0,0,0,4}, {6,0,7,0,0,0,0,8,0}, {4,0,0,0,9,0,0,0,0}}; // The Possible Values public int possibilityboard[][]; int value;   public int[] getnumbersinrow(int row,int[][]board){   for(int y = 0; y<=8;y++){ rows[y]=newboard[row][y]; }   return rows; } public int getCell(int i, int j) { return newboard[i][j]; }   public int[] getNeighbourhood(int i, int j) { int left = (i/3)*3; int top = (j/3)*3; int[] neighbourhood = {getCell(left, top), getCell(left, top+1), getCell(left, top+2), getCell(left+1, top), getCell(left+1, top+1), getCell(left+1, top+2), getCell(left+2, top), getCell(left+2, top+1), getCell(left+2, top+2)}; return neighbourhood; } public int[] getnumbersincol(int col, int[][]board){ for(int z = 0; z<=8; z++){ cols[z]=newboard[z][col]; } return cols; } public boolean isinrow(int number, int col){ for(int x = 0;x<=8;x++){ if(newboard[col][x]==number){ return true;   } } return false; } public int amountofcellsleft(){ int emptycount = 0; for(int x = 0; x<=8;x++){ for(int y = 0; y<=8;y++){ if(newboard[x][y]==0){ emptycount++; } } } return emptycount;   }     public boolean isincol(int number, int row){ for(int x =0;x<=8;x++){ if(newboard[row][x]==number){ return true; } } return false; } // Checks if a value is equal to zero //Takes an array and an int as arguments public boolean isarrayzero(int array[],int cellinarray){ if(array[cellinarray]==0){ return true; } else{ return false; } } }```
```public class runprogram { boolean unsolved = false; int copyofbox[]; int copyofrow[]; int copyofcol[]; int copytest[]; int emptycellcount = 0; GetDataMethods one = new GetDataMethods(); Cell two = new Cell();   public static void main(String[] args) { runprogram run = new runprogram(); run.findemptycells(); }   public void findemptycells(){ two.assignvaluesandloc(); for(int x = 0;x<=80;x++){ if(two.cellarray[x].value==0){ two.cellarray[x].isnull=true; } } assignpossiblevalues(); }   public void assignpossiblevalues(){ evaluatandremovepossibilities(); }   public void evaluatandremovepossibilities(){ while(one.amountofcellsleft()>0){ for(int cell = 0; cell <=80;cell++){   copyofrow= one.getnumbersincol(two.cellarray[cell].xloc, one.newboard); copyofcol= one.getnumbersinrow(two.cellarray[cell].yloc, one.newboard); copyofbox= one.getNeighbourhood(two.cellarray[cell].xloc, two.cellarray[cell].yloc);   for(int x =0 ; x<=8; x++){ for(int p = 0; p<=8;p++){ if(two.cellarray[cell].possiblevalues[x]==copyofbox[p]){ two.cellarray[p].value=10; } } for(int y = 0; y<=8;y++){   if(two.cellarray[cell].possiblevalues[x]==copyofrow[y]){ two.cellarray[cell].possiblevalues[x]=10; two.cellarray[cell].reallength--;   } if(two.cellarray[cell].possiblevalues[x]==copyofcol[y]){ two.cellarray[cell].possiblevalues[x]=10; two.cellarray[cell].reallength--;   } }   }   for(int p = 0; p<=80;p++){   if(two.cellarray[p].reallength==1){ one.newboard[two.cellarray[p].xloc][two.cellarray[p].yloc] = two.cellarray[p].value; } } }   System.out.println(one.amountofcellsleft());   } }```
```public class Cell {   boolean isnull=false; int neighborhood; int value = 0; int xloc = 0; int yloc = 0; int[] possiblevalues = {1,2,3,4,5,6,7,8,9}; int reallength = 9; Cell[] cellarray = new Cell[81]; int arraylistoffset;   //Assigns each cell the xloc, yloc, and the value. //Populates the cellarray public void assignvaluesandloc(){ int x = 0; GetDataMethods g = new GetDataMethods(); int currentnumber = 0; for (int row = 0; row <= 8; row++) { for(int col = 0; col <= 8; col++) { cellarray[x] = new Cell(); cellarray[x].value=g.newboard[row][col]; cellarray[x].xloc=col; cellarray[x].yloc=row; x++;   } } removepossiblenumber(); } private void removepossiblenumber() { for(int x = 0; x<=80;x++){ if(cellarray[x].value!=0) { cellarray[x].possiblevalues[value]=10; cellarray[x].reallength--; } } } // returns the Cell.value for a given cell public int getcellvalue(int cellnumber){ return cellarray[cellnumber].value; } }```