Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Java Sudoku

  1. #1
    New Coder
    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 1-9 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[row-1][col-1] = 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[row-1].length; i++)
    		{
    			if(pNumber == Sudoku.numberGrid[row-1][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][col-1])
    			{
    				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
    Each method itself works correctly. So for example if I change if(<< check for valid number here(number)>>) in the code posted to if(!inBox(number)) then each box will follow the right rules. Same if I change it to !inRow and !inCol. However if I try if(!inBox(number) && !inRow(number) && !inCol(number)) the program seems to go into an infinite loop and grinds to a halt.

    Anyone got any ideas or can reference me to any resource that might help me to get round this?
    Last edited by jmitch18; 05-10-2008 at 09:41 PM. Reason: making code more readable

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    the likely problem is that the psuedo-random number generator get stuck in a loop and does not give you all the possible values between 0-9 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 0-9 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 psuedo-random generator.
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New Coder
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •