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 17 of 17

Thread: In a bit of a pickle, unsure of what I should do

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

    Default In a bit of a pickle, unsure of what I should do

    Well I'm writing this for fun, no it's not homework.
    So anyway, it's basically a Sudoku Solver.
    I intend to have it solve a Sudoku by finding obvious pairs;
    As in naked pairs : Solving Sudoku
    That's a naked pair; A cell with only 1 possible number.
    So far I have a Cell class, and withing that class I have a method that creates 81 instances of itself:

    And then it occured to me;
    How on Earth would I assign it values;
    I was brainstorming, and thought that perhaps there could be a method that scans a row, returns an array of the nonzero numbers in the row, same for the column, and a 3*3 region, or box.


    I'm not sure how I would code that, seeing as I am a O.K. programmer, I could use a little help, and if someone could just help me out by giving me some idea on how to do this, code etc, that would be great!
    This is not homework, I code in my free time. Help would be very appreciated


    import java.util.ArrayList;
     
     
    public class Cell {
    boolean isnull=false;
    int value;
    int xloc;
    int yloc;
    int possiblevalues[] = new int[] {1,2,3,4,5,6,7,8,9};
    Cell[] cellarray = new Cell[81];
    ArrayList signs = new ArrayList(40);
     
     
    public void assignvaluesandloc(){
    	int x = 0;
    	Cell[] cellarray = new Cell[81];
    	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=row;
    		        cellarray[x].yloc=col;
    		        x++;
     
    		     	}
    		     }
    	 	}
    			public void getcellvalue(){
     
    			}
    	    }


  2. #2
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: In a bit of a pickle, unsure of what I should do

    It might be easier to use a two dimensional array here, as it more accurately represents a Sudoku square.

    public class Sudoku {
     
        private Cell[][] square = new Cell[9][9];
     
        public void initialize() {
            // set the initial values of some of the Cells
        }
     
        public void solve() {
            // write your solving algorithm here
        }
     
        public void print() {
            // print your square
        }
     
        public static void main(String[] args) {
            Sudoku puzzle = new Sudoku();
            puzzle.initalize();
            puzzle.print();
            puzzle.solve();
            puzzle.print();
        }
     
    }
     
    public class Cell {
     
        public int value;
     
        public List<Integer> possibleValues = new ArrayList<Integer>();
     
    }

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

    Default Re: In a bit of a pickle, unsure of what I should do

    Oh I don't know if you know, but I have another file representing the whole board.
    Why the two dimensional array?
    EDIT:
    Oh wait you are referring to the 3*3 portion of a board?
    I was hoping to somehow eliminate the possible values by scanning rows/columns.
    I'm not sure where this is going

    Heres another file
    public class GetDataMethods {
    	int emptycells[];
    	//The Sudoku
        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){
    int x = 0;
    for(int y = 0; y<=8;y++){
    if(board[row][y]!=0){
     
    }
    }
    	return x;
    }
    public boolean isinrow(int number, int col){
    	for(int x = 0;x<=8;x++){
    		if(newboard[col][x]==number){
    		return true;
     
    		}
    	}
    	return false;
    	}
     
    public boolean isincol(int number, int row){
    for(int x =0;x<=8;x++){
    if(newboard[row][x]==number){
    return true;
    }
    	}
    		return false;
    	}
    }

  4. #4
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: In a bit of a pickle, unsure of what I should do

    From the looks of it, you already are using a two dimensional array...

    Since that's the case, why are you creating an array of Cells of size 81?

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

    Default Re: In a bit of a pickle, unsure of what I should do

    Oh good point.
    Well this project is supposed to be completely different in the end, It will load the sudoku from a file, and I was thinking perhaps first it should be in an array, then the array should be copied with it's values to the appropriate objects.
    What do you suggest I should do?

  6. #6
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: In a bit of a pickle, unsure of what I should do

    I would still do it the way I started doing it above.

    I would make the file look like this:

    1,2:4
    4,9:8
    x,y:z

    where 1 is the horizontal position (the x value, or the first value in the array call) and 2 is the vertical position (the y value, or the second value in the array call) and the number the semi-colon (in this case, 4) is the value of the cell.

    So,

    public void initialize(File file) {
        BufferedReader in = new BufferedReader(new FileReader(file));
        for (each line of the file) {
            split it so x is the first, y is the second, and z is the third;
            square[x][y] = new Cell(z);
        }
    }

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

    Default Re: In a bit of a pickle, unsure of what I should do

    Alright, well so far that sounds good.
    I'll modify my program and adjust it to fit what you said.
    However, how would you approach removing and adding possible values.

  8. #8
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: In a bit of a pickle, unsure of what I should do

    Do you mean adding possible values to solve the puzzle? Well, I am not an expert sudoku puzzle solver, so I am not exactly sure how to proceed at the moment. How would you solve the puzzle in your head?

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

    Default Re: In a bit of a pickle, unsure of what I should do

    Ok well here's what I mean:
    Every cell in the sudoku puzzle has possible values;
    As in
    Say we have part of a puzzle

    1 2 3 _ _ _
    4 5 6 1 _ _
    7 8 9 _ _ x

    the x can't have 7 8 9 as possiblities, because they are in the same row.
    So basically, it's like saying the following numbers can go inside the little cell, or x, and work fine.
    Most of them have 1 possible value, so they will then be assigned a value, then a chain reaction will make other cells 1 possible value, eventually solving the puzzle.

  10. #10
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: In a bit of a pickle, unsure of what I should do

    Do you have any ideas?

    Half of the fun of coding is the thought that goes into it ;-)

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

    Default Re: In a bit of a pickle, unsure of what I should do

    Yea, But there's something wrong:
    I keep getting a Null Pointer Exception;
    When I have the following files:

    import java.util.ArrayList;
     
     
    public class runprogram {
    	ArrayList arrayList = new ArrayList();
    	ArrayList<Cell> a = new ArrayList<Cell>();
    	int[]emptyvalues;
    	int[]givens = new int[81];
    	int emptycellcount = 0;
    	GetDataMethods one = new GetDataMethods();
    	Cell two = new Cell();
     
    public static void main(String[] args) {
     
    runprogram run = new runprogram();
    run.runprogram();
    }
    public void runprogram(){
    findemptycells();
    }
    public void findemptycells(){
    	int emptycellcount = 0;
    	int givenscount=0;
    	int emptyx;
    	int emptyy;
    	int emptycells[] = new int[81];
    	// call the assignvalue method
    	two.assignvaluesandloc();
    	// Loops and finds any values set to 0
    	//If the value is 0, boolean isnull from the cell class is triggered.
    	for(int x = 0;x<=80;x++){
    		if(two.cellarray[x].value==0){
    		two.cellarray[x].isnull=true;
     
    		}
    else if(two.cellarray[x].value!=0){
    	givens[givenscount]=two.cellarray[x].value;
    	System.out.println(givens[givenscount]);
    	givenscount++;
     
    }
    	}
    }
     
    public void assignpossiblevalues(){
    int count = 0;
    	for(int x = 0;x<=80;x++){
    emptyvalues[count]=x;
    }
    }
    public void fillinnakedpairs(){
    // This will determine naked pairs;
    //It will grab a cell
     
    }
    }

    import java.util.ArrayList;
     
     
    public class Cell {
    boolean isnull=false;
    int value;
    int xloc;
    int yloc;
    int possiblevalues[] = new int[] {1,2,3,4,5,6,7,8,9};
    Cell[] cellarray = new Cell[81];
    ArrayList signs = new ArrayList(40);
     
     
    public void assignvaluesandloc(){
    	int x = 0;
    	Cell[] cellarray = new Cell[81];
    	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=row;
    		        cellarray[x].yloc=col;
    		        x++;
     
    		     	}
    		     }
    	 	}
     
    			public void getcellvalue(){
     
    			}
    	    }

    public class GetDataMethods {
    	int emptycells[];
    	//The Sudoku
        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){
    int x = 0;
    for(int y = 0; y<=8;y++){
    if(board[row][y]!=0){
     
    }
    }
    	return x;
    }
    public boolean isinrow(int number, int col){
    	for(int x = 0;x<=8;x++){
    		if(newboard[col][x]==number){
    		return true;
     
    		}
    	}
    	return false;
    	}
     
    public boolean isincol(int number, int row){
    for(int x =0;x<=8;x++){
    if(newboard[row][x]==number){
    return true;
    }
    	}
    		return false;
    	}
    }

    When I use the program, I can only access the array of cells inside the Cell class itself.
    Stupid, and annoying, and it's driving me nuts.
    Anyone got any suggestions?
    edit: I actually get a null pointer
    Last edited by MysticDeath; October 26th, 2009 at 08:11 PM.

  12. #12
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: In a bit of a pickle, unsure of what I should do

    Where do you get this null pointer?

    I have started writing a Sudoku solver myself. So far I have written all the setup code. Now we are both stuck at the same point: how to go about solving the puzzle. I will think of some ideas and report back tomorrow.

    [FONT="Courier New"][jeremy@twelve java]$ java Sudoku
    -------------------------
    |   7 2 | 3 8 5 | 4     |
    |   3 9 |   1 6 |       |
    | 1     | 2 7   | 3   6 |
    -------------------------
    | 7 8   |       | 6 4   |
    | 5     |       |     7 |
    |   9 4 |       |   3 1 |
    -------------------------
    | 4   1 |   6 3 |     8 |
    |       | 9 2   | 1 6   |
    |     8 | 5 4 1 | 2 7   |
    -------------------------
    [/FONT]

    Here's my input:

    1,2:7
    1,3:2
    1,4:3
    1,5:8
    1,6:5
    1,7:4
    2,2:3
    2,3:9
    2,5:1
    2,6:6
    3,1:1
    3,4:2
    3,5:7
    3,7:3
    3,9:6
    4,1:7
    4,2:8
    4,7:6
    4,8:4
    5,1:5
    5,9:7
    6,2:9
    6,3:4
    6,8:3
    6,9:1
    7,1:4
    7,3:1
    7,5:6
    7,6:3
    7,9:8
    8,4:9
    8,5:2
    8,7:1
    8,8:6
    9,3:8
    9,4:5
    9,5:4
    9,6:1
    9,7:2
    9,8:7
    Last edited by literallyjer; October 26th, 2009 at 09:15 PM.

  13. #13
    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: In a bit of a pickle, unsure of what I should do

    The simplest solution? Brute force Soduko's are simple enough they can be solved in a fraction of a second by computers via brute force.

  14. #14
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: In a bit of a pickle, unsure of what I should do

    Ooooh, but I wanted to be clever! *pouts*

    Haha, it is still a fun little exercise.

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

    Default Re: In a bit of a pickle, unsure of what I should do

    Quote Originally Posted by helloworld922 View Post
    The simplest solution? Brute force Soduko's are simple enough they can be solved in a fraction of a second by computers via brute force.
    No, not really.
    The harder ones take more time
    I'd rather use the simple logic, although idk.
    Do you know what is causing the null pointer exception though?

    Oh an my null pointer error:
    Exception in thread "main" java.lang.NullPointerException
    at runprogram.findemptycells(runprogram.java:28)
    at runprogram.runprogram(runprogram.java:20)
    at runprogram.main(runprogram.java:16)

  16. #16
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: In a bit of a pickle, unsure of what I should do

    What is line 28?

  17. #17
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: In a bit of a pickle, unsure of what I should do

    AMIS Technology blog » Blog Archive » Oracle RDBMS 11gR2 - Solving a Sudoku using Recursive Subquery Factoring

Similar Threads

  1. :!! Unsure how to set this up/ Still learning java loops!!!!!
    By raidcomputer in forum Loops & Control Statements
    Replies: 4
    Last Post: September 29th, 2009, 10:28 AM