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

Thread: I'm creating a sudoku program that is somehow in an infinite loops.

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default I'm creating a sudoku program that is somehow in an infinite loops.

    I've been trying to figure this out all night. I started on this sudoku program and for some reason it's in an infinite loop. Basically, I'm using Math.random() to generate integers and collecting them into a two dimensional array that is 9 X 9. Then I use a method to verify that the numbers in the array are unique by row and by column (and soon to be box) if I can figure this out. It seems to work just fine when I only check either row or column at a time, but not both. I debugged and it seems to be doing what it's supposed to be doing by breaking away from the for loop when the value equals a value already in its row or column, then it generates a new random integer, and goes back.. but I can't tell why it says it changes, but gets stuck on the same value.

    public class TestSudokuLogic {
     
    	public static int counter = 0;
     
    	public static void main(String[] args) 
    	{
    		//create grid
     
    		int[][] grid = new int[9][9];
     
    		for (int row = 0; row < grid.length; row++) 
    		{
    			for (int col = 0; col < grid[row].length; col++)
    			{
    				boolean valid = false;
     
    				while (!valid) 
    				{
    					grid[row][col] = getRandomInt();
    					valid = checkRandomInt(grid, row, col);
    					counter++;
    					System.out.println(counter);
    				}
    			}	
    		}
    		printArray(grid);
    	}
     
    	public static boolean checkRandomInt(int[][] grid, int row, int col)
    	{
    		for (int i = 0; i < col; i++)
    			if (grid[row][col] == grid[row][i]) //checks each col in row (rows are unique 1-9)
    				return false;
     
    		for (int j = 0; j < row; j++)
    			if (grid[row][col] == grid[j][col]) //checks each row in col (cols are unique 1-9)
    				return false;
     
    		return true;
    	}
     
    	public static int getRandomInt()
    	{
    		int r = 0;
     
    		while(r < 1)
    			r = (int) (Math.random() * 10);
    		return r;
    	}
     
    	public static void printArray(int[][] a)
    	{
    		for (int row = 0; row < a.length; row++)
    		{
    			for (int col = 0; col < a[row].length; col++)
    				System.out.print(a[row][col] + " ");
    			System.out.println();
    		}
    	}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I'm creating a sudoku program that is somehow in an infinite loops.

    Where is it stuck in an infinite loop, main, print or check method? Add debugging statements to find out. Add more debugging statements to narrow down the problem. Keep going until you can identify what is causing the infinite loop and that should lead to a solution.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I'm creating a sudoku program that is somehow in an infinite loops.

    I figured it out. It's fail logic. For instance, while filling a 9 x 9, 2 dimensional array of integers 1 - 9 there's a high probability that this problem occurs:

    123456789
    987645321
    79856423x

    No value exists for x that fulfills the constraints of not matching any other values in the row, column, and box.

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I'm creating a sudoku program that is somehow in an infinite loops.

    Still though, it seems that if I exhaust the possibilities (throw away each grid and start a new one) eventually I'll get a grid that adheres to the guidelines and have myself a randomly generated sudoku grid. Maybe the odds are too high for such a process to occur within a few minutes?

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: I'm creating a sudoku program that is somehow in an infinite loops.

    You must think logically and must implement AI to your program. Which means, you just not have to think of the present box but the future boxes as well. Well, it all involves AI.

Similar Threads

  1. Need help coding this program with for loops
    By jwall2 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 7th, 2011, 12:53 AM
  2. Creating number Pyramids with For Loops
    By Staticity in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2011, 05:20 PM
  3. [SOLVED] Compile a set of java files for a sudoku program
    By kanishk.dudeja in forum Java Theory & Questions
    Replies: 7
    Last Post: June 16th, 2011, 09:54 AM
  4. Creating pyramids with for loops
    By jericajam in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 4th, 2011, 06:56 AM
  5. Program with for loops help
    By ixjaybeexi in forum Loops & Control Statements
    Replies: 23
    Last Post: October 8th, 2009, 10:05 AM