jmitch18

05-10-2008, 08:37 PM

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):

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?

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):

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?