5 Replies - 792 Views - Last Post: 06 December 2011 - 07:49 PM Rate Topic: -----

#1 InsaneWolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-December 11

Run thru 2D array

Posted 06 December 2011 - 09:45 AM

Hello, I am creating sudoku in java and I have one problem regarding the check if the sudoku is filled in correctly or not.

I have on 2D array from 9x9 as the field, but now I need to check the smaller fields of the sudoku,
(like the 3x3 fields within the thick black lines in this picture)

The problem is running thru a small box is simple like this:
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
array[i][j];
}
}


But how can I make it that after doing 1 box of 3x3 it jumps to the next box (positions 03 --> 05, 13 --> 15, 23 --> 25) and then the next box. And then the next line of 3 boxes from 3x3 (the middle in the picture from a little earlier). I hope you get what I am trying to say.

Many kudo's to the person that can help me creating this loop.


With kind regards
InsaneWolf

Is This A Good Question/Topic? 0
  • +

Replies To: Run thru 2D array

#2 Alexanderj  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 07-July 11

Re: Run thru 2D array

Posted 06 December 2011 - 12:08 PM

i was thinking something like this:

the idea is that you start at coordinate: 0,0 in the sudoku block.
then you make steps of 3 to get to the next block.. when you reach the end of one row you go
3 down so you get in the next thick row etc.

for(int height=0; height <9; height+=3){
   
  for(int width=0; width<9; width+=3){
       
       //processing the small blocks here
       for(int i=height; i<height+3; i++){
           
           for(int j=width; j<width+3; j++){
               /*
                process one number in the small block here 
                for the first number of the seccond small block
                the sudoku the coordinates should be:
                height=3 , width=3, i=3, j=3 */
           }
       }
   }
}



i tried it and something along this way should fix your problem.

hope this helps ;)

gl ;)

This post has been edited by Alexanderj: 06 December 2011 - 12:11 PM

Was This Post Helpful? 1
  • +
  • -

#3 InsaneWolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-December 11

Re: Run thru 2D array

Posted 06 December 2011 - 02:32 PM

View PostAlexanderj, on 06 December 2011 - 12:08 PM, said:

The above text ^ :P


Hi Alexanderj thank you so much for that code! I got it working like this:
        int counter = 0;
        for (int height = 0; height < 9; height += 3) {
            for (int width = 0; width < 9; width += 3) {

               ////processing the small blocks here

                for (int i = height; i < height + 3; i++) {
                    for (int j = width; j < width + 3; j++) {
                        userInput[counter] = field[i][j]; //takes a 3x3 block and puts every int at position x in userInput
                        counter++; //obvious
                    }
                }
                Arrays.sort(userInput); //sorts the array from small to big.
                for (int i = 0; i < 9; i++) {
                    if (userInput[i] != solution[i]) { //solution contains (1 2 3 4 5 6 7 8 9)
                        isFaulty = true;
                    }
                }
                counter = 0;
            }
        }



That works perfectly, but i'm still not sure about what the part where //processing the small blocks here stands does. Anyways I will figure it out if i'm a bit more "awake" and thank you very much again, you saved me ;)
Was This Post Helpful? 0
  • +
  • -

#4 Alexanderj  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 07-July 11

Re: Run thru 2D array

Posted 06 December 2011 - 02:46 PM

View PostInsaneWolf, on 06 December 2011 - 02:32 PM, said:

View PostAlexanderj, on 06 December 2011 - 12:08 PM, said:

The above text ^ :P


Hi Alexanderj thank you so much for that code! I got it working like this:
        int counter = 0;
        for (int height = 0; height < 9; height += 3) {
            for (int width = 0; width < 9; width += 3) {

               ////processing the small blocks here

                for (int i = height; i < height + 3; i++) {
                    for (int j = width; j < width + 3; j++) {
                        userInput[counter] = field[i][j]; //takes a 3x3 block and puts every int at position x in userInput
                        counter++; //obvious
                    }
                }
                Arrays.sort(userInput); //sorts the array from small to big.
                for (int i = 0; i < 9; i++) {
                    if (userInput[i] != solution[i]) { //solution contains (1 2 3 4 5 6 7 8 9)
                        isFaulty = true;
                    }
                }
                counter = 0;
            }
        }



That works perfectly, but i'm still not sure about what the part where //processing the small blocks here stands does. Anyways I will figure it out if i'm a bit more "awake" and thank you very much again, you saved me ;)


good job !!!

just ignore that //processing the small blocks here
i meant with it that the last two for's with int i and int j where the ones that should process the small blocks inside the sudoku ( the 3x3 part)

glad my code helped you and about my sig.. changed the size maybe its a bit to big now :P :P
Was This Post Helpful? 0
  • +
  • -

#5 InsaneWolf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-December 11

Re: Run thru 2D array

Posted 06 December 2011 - 03:05 PM

View PostAlexanderj, on 06 December 2011 - 12:08 PM, said:

... ^


Ah I see what you meanth with "processing the small blocks here" now :P but again thank you very much! (Bedankt noorderbuur ;) )
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Run thru 2D array

Posted 06 December 2011 - 07:49 PM

More complicated than that, your are better to have a Cell class to handle a Cell
A Region class that holds 3X3 Cells
A Sudioku class that holds a 3X3 Region

http://www.dreaminco...-i-basic-tools/

and I have 6 other tutorials if you are interested to

Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1