loop?

Hi,

Im new to programming and ive just set up a sudoku GUI in Java.

Basically my goal is to be able to solve a sudoku puzzle, but i like the graphics supported by java. I have a 9x9 array holding instances of JCombobox.

I have a set up JButton, that when pressed loops through the array and if any items have been selected in an instance of JCombobox it sets the Enable() methos to false.

Then at the moment my "solve" button loops through each row and column to see if any JComboBox has been deactivate, if so it gets the selected item from that instance of the combobox and removes the selected item from the ArrayList of every other JComboBox in that row/column.

i.e.

for(int i=0; i<9; i++)

for(int j=0; j<9; j++)

{

if(!cb_Array[i][j].isEnabled())

for(int k=0; k<9; k++)

cb_Array[i][k].avail_list((String)cb_Array[i][j].getSelectedItem());

}

searches through the rows and removes selected items form the available number list( (Integer)ArrayList ) of every other JComboBox istance in that particular row.

What Im having trouble with is doing something similar for the 3x3 squares.

If the [i][j] coordinates for the first (top left corner) 3x3 are as follows:

0,0 0,1 0,2

1,0 1,1 1,2

2,0 2,1 2,2

how do i loop throught it?

Im new to programming and ive just set up a sudoku GUI in Java.

Basically my goal is to be able to solve a sudoku puzzle, but i like the graphics supported by java. I have a 9x9 array holding instances of JCombobox.

I have a set up JButton, that when pressed loops through the array and if any items have been selected in an instance of JCombobox it sets the Enable() methos to false.

Then at the moment my "solve" button loops through each row and column to see if any JComboBox has been deactivate, if so it gets the selected item from that instance of the combobox and removes the selected item from the ArrayList of every other JComboBox in that row/column.

i.e.

for(int i=0; i<9; i++)

for(int j=0; j<9; j++)

{

if(!cb_Array[i][j].isEnabled())

for(int k=0; k<9; k++)

cb_Array[i][k].avail_list((String)cb_Array[i][j].getSelectedItem());

}

searches through the rows and removes selected items form the available number list( (Integer)ArrayList ) of every other JComboBox istance in that particular row.

What Im having trouble with is doing something similar for the 3x3 squares.

If the [i][j] coordinates for the first (top left corner) 3x3 are as follows:

0,0 0,1 0,2

1,0 1,1 1,2

2,0 2,1 2,2

how do i loop throught it?

You might want to check out this thread:

http://www.coderanch.com/t/35387/Programming/SoDuko-puzzle

People there have been discussing ways to write a solver.

The traditional way to traverse a 2-d array is to use nested for loops. You're already using the nested for loops to go over all of the squares (the outer i and j loops). You can just use another two loops to cover the 3x3 segment. You'll can set the initial value of the loop variable according to which 3x3 segment you're looking at (i=0, j=0 for the top left segment, i=0, j=3 for the next segment over, i=6,j=6 for the lower right segment).

Hope that helps.

_M_

http://www.coderanch.com/t/35387/Programming/SoDuko-puzzle

People there have been discussing ways to write a solver.

The traditional way to traverse a 2-d array is to use nested for loops. You're already using the nested for loops to go over all of the squares (the outer i and j loops). You can just use another two loops to cover the 3x3 segment. You'll can set the initial value of the loop variable according to which 3x3 segment you're looking at (i=0, j=0 for the top left segment, i=0, j=3 for the next segment over, i=6,j=6 for the lower right segment).

Hope that helps.

_M_

so something like

for(int i=0; i<9; i++)

for(int j=0; j<9; j++)

for(int k=0,l=0; k<3,l<3; k++,j++)

if(!cb_Array[k][l].isEnabled())

cb_Array[k][l].avail_list((String)cb_Array[i][j].getSelectedItem());

for the upper left 3x3???

for(int i=0; i<9; i++)

for(int j=0; j<9; j++)

for(int k=0,l=0; k<3,l<3; k++,j++)

if(!cb_Array[k][l].isEnabled())

cb_Array[k][l].avail_list((String)cb_Array[i][j].getSelectedItem());

for the upper left 3x3???

Well, not quite. Something more like:

_M_

_M_

Cheers, I had it figured out.....eventually.

Not bad...

Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

This thread has been viewed 684 times.

All times above are in ranch (not your local) time.

The current ranch time is

Jun 24, 2018 16:11:50.

The current ranch time is

Jun 24, 2018 16:11:50.