Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help please..

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Help please..

    Well I've been working on a Sudoku Solver. I ran into a problem;
    This is a typical Sudoku:


    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?

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Help please..

    I'd use a 9*9 2D array of integers

    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.

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help please..

    =S, well the way I programmed it... gah.
    Here are my files. I already have a newboard or sudoku board two dimensional array.

    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;
    	}
    }

Tags for this Thread