Enjoy an ad free experience by logging in. Not a member yet? Register.


Results 1 to 3 of 3
Thread: Java Sudoku

05102008, 08:37 PM #1
 Join Date
 Mar 2007
 Location
 Northern Ireland
 Posts
 40
 Thanks
 0
 Thanked 0 Times in 0 Posts
Java Sudoku
I am working on a java Sudoku game but have hit a hurdle that I just can't get over. Just to start with the game has 3 classes; Sudoku (extends JFrame), BoxPanel (extends JPanel) and NumberBox (extends JTextField).
Sudoku.class loops 9 times to create the 9 BoxPanels. Each BoxPanel also loops 9 times to generate the 9 NumberBox objects.
I'm trying to make it randomly generate the numbers each time the program starts rather than reading from file.
To do this I obviously need to ensure that the correct rules are followed;
IE only 1 of each number in 19 allowed in any outer box, row or column. I have written 3 methods as follows to check each of these (generateNumber does the work of trying to place the number):
Code:private void generateNumber() { int bCol = getCol(); int bRow = getRow(); int number = 0; boolean placed = false; do { number = (generator.nextInt(Sudoku.SIZE)+1); if(<< check for valid number here(number)>>) { this.setText(""+number); Sudoku.numberGrid[row1][col1] = number; boxNumberGrid[bRow][bCol] = number; placed = true; }// if }// do while(!placed); }// generateNumber // check to see if the number already exists in the row private boolean inRow(int pNumber) { // loop through row and check values for(int i = 0; i < Sudoku.numberGrid[row1].length; i++) { if(pNumber == Sudoku.numberGrid[row1][i]) { return true; } }// for return false; }// inRow // check to see if the number already exists in the column private boolean inCol(int pNumber) { // loop through columns and check values for(int i = 0; i < Sudoku.numberGrid.length; i++) { if(pNumber == Sudoku.numberGrid[i][col1]) { return true; }// if }// for return false; }// in Col // check to see if the number already exists box private boolean inBox(int pNumber) { // row = i; col = j for(int i = 0; i < boxNumberGrid.length; i++) { for(int j = 0 ; j < boxNumberGrid[i].length; j++) { if(pNumber == boxNumberGrid[i][j]) { return true; } }// for }// for return false; }// inBox
Anyone got any ideas or can reference me to any resource that might help me to get round this?Last edited by jmitch18; 05102008 at 08:41 PM. Reason: making code more readable
05112008, 12:59 PM
#2
the likely problem is that the psuedorandom number generator get stuck in a loop and does not give you all the possible values between 09 so, what you can do is store the values of the numbers that are generated each time and change the seed of the generator if it generates the same number again.
another solution is to create an array fill it with numbers 09 then scramble the array passing a random function to the sorting function and pop off numbers from the top. this way you will not have any collisions from the psuedorandom generator.
You never have to change anything you got up in the middle of the night to write.  Saul Bellow
05112008, 06:31 PM
#3
 Join Date
 Mar 2007
 Location
 Northern Ireland
 Posts
 40
 Thanks
 0
 Thanked 0 Times in 0 Posts
Thanks. I have been considering rewriting the generation method because of the length of time it took to pick a correct number when I applied one of the rules (took something like 16 loops to turn up a 4).
Scrambling the array sounds more like an ideal solution to the problem.
Just another very strange thing that happened;
I have been printing the numbers to the console as an attempt to place them has been made and displayed the result beside it. Say the first number it drew was 4 and it came up as "Invalid: 4", about 200 loops later 4 might come up again and then it may say "Valid: 4" even though all 4's before it turned up invalid.
I will try scrambling an array of numbers and hopefully that will deal with the issue; otherwise I will have to seriously rethink the program.
Thanks for your suggestions.