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

• November 17th, 2011, 10:44 PM
Leiferson
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.

Code :

```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(); } } }```
• November 17th, 2011, 10:52 PM
Junky
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.
• November 18th, 2011, 01:19 AM
Leiferson
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.
• November 18th, 2011, 01:51 AM
Leiferson
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?
• November 18th, 2011, 02:21 AM
Mr.777
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.