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

• October 26th, 2009, 06:47 PM
MysticDeath
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 :))

Code :

```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(){   } }```
• October 26th, 2009, 06:58 PM
literallyjer
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.

Code :

```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>();   }```
• October 26th, 2009, 07:03 PM
MysticDeath
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
Code :

```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; } }```
• October 26th, 2009, 07:06 PM
literallyjer
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?
• October 26th, 2009, 07:07 PM
MysticDeath
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?
• October 26th, 2009, 07:18 PM
literallyjer
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,

Code :

```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); } }```
• October 26th, 2009, 07:22 PM
MysticDeath
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.
• October 26th, 2009, 07:51 PM
literallyjer
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?
• October 26th, 2009, 07:54 PM
MysticDeath
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.
• October 26th, 2009, 07:59 PM
literallyjer
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 ;-)
• October 26th, 2009, 08:05 PM
MysticDeath
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:

Code :

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

Code :

```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(){   } }```

Code :

```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
• October 26th, 2009, 09:06 PM
literallyjer
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.

Code :

```[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:

Code :

```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```
• October 26th, 2009, 09:19 PM
helloworld922
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.
• October 27th, 2009, 07:37 AM
literallyjer
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.
• October 27th, 2009, 03:52 PM
MysticDeath
Re: In a bit of a pickle, unsure of what I should do
Quote:

Originally Posted by helloworld922
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?:-t

Oh an my null pointer error:
at runprogram.findemptycells(runprogram.java:28)
at runprogram.runprogram(runprogram.java:20)
at runprogram.main(runprogram.java:16)
• October 28th, 2009, 08:36 AM
literallyjer
Re: In a bit of a pickle, unsure of what I should do
What is line 28?
• October 31st, 2009, 09:10 AM
literallyjer
Re: In a bit of a pickle, unsure of what I should do