# Java sudoku



## GSquadron (Aug 18, 2011)

A friend of mine gave me a java sudoku ready, i just modified the main part but looks like it 
doesn't show up anything (used the code in neatbeans and in eclipse too):

```
package javaapplication2;
import java.util.*;

public class Main {
    public static void main(String args[]){}
    Random generator = new Random();
    int[] serie = new int[9];
    int[][] sudoku = new int[9][9];
    int[][] select = new int[9][9];
    int livello;
    public Main(int livello) {
        this.livello = livello;
        for (int i=0; i<9; i++) {
            for (int j=0; j<9; j++) {
                select[i][j] = -1;
            }
        }
        for (int i=0; i<9; i++) {
            for (int j=0; j<9; j++) {
                sudoku[i][j] = -1;
            }
        }
        for (int i=0; i<9; i++) {
            serie[i] = -1;
        }
    }
    private void genArray() {
        int numCas = Math.abs(generator.nextInt(9))+1;
        for (int i=0; i<9;i++) {
            if (i==0) {
                serie[i] = numCas;
            } else {
                while (serie[0] == numCas ||
                        serie[1] == numCas ||
                        serie[2] == numCas ||
                        serie[3] == numCas ||
                        serie[3] == numCas ||
                        serie[4] == numCas ||
                        serie[5] == numCas ||
                        serie[6] == numCas ||
                        serie[7] == numCas ||
                        serie[8] == numCas) {
                    numCas = Math.abs(generator.nextInt(9))+1;
                }
                serie[i] = numCas;
            }
        }
    }
    private void  generateSudoku() {
        for (int i=0; i<9; i++) {
            if (i==0) {
                for (int k=0; k<9; k++) {
                    sudoku[i][k] = serie[k];
                }
            } else if (i==2) {
                for (int k=0; k<6; k++) {
                    sudoku[i][k+3] = serie[k];
                }
                for (int k=0; k<3; k++) {
                    sudoku[i][k] = serie[k+6];
                }
            } else if (i==1) {
                for (int k=0; k<3; k++) {
                    sudoku[i][k+6] = serie[k];
                }
                for (int k=0; k<6; k++) {
                    sudoku[i][k] = serie[k+3];
                }
            } else if (i==3) {
                for (int k=0; k<8; k++) {
                    sudoku[i][k+1] = serie[k];
                }
                for (int k=0; k<1; k++) {
                    sudoku[i][k] = serie[k+8];
                }
            } else if (i==5) {
                for (int k=0; k<5; k++) {
                    sudoku[i][k+4] = serie[k];
                }
                for (int k=0; k<4; k++) {
                    sudoku[i][k] = serie[k+5];
                }
            } else if (i==4) {
                for (int k=0; k<2; k++) {
                    sudoku[i][k+7] = serie[k];
                }
                for (int k=0; k<7; k++) {
                    sudoku[i][k] = serie[k+2];
                }
            } else if (i==6) {
                for (int k=0; k<7; k++) {
                    sudoku[i][k+2] = serie[k];
                }
                for (int k=0; k<2; k++) {
                    sudoku[i][k] = serie[k+7];
                }
            } else if (i==8) {
                for (int k=0; k<4; k++) {
                    sudoku[i][k+5] = serie[k];
                }
                for (int k=0; k<5; k++) {
                    sudoku[i][k] = serie[k+4];
                }
            } else if (i==7) {
                for (int k=0; k<1; k++) {
                    sudoku[i][k+8] = serie[k];
                }
                for (int k=0; k<8; k++) {
                    sudoku[i][k] = serie[k+1];
                }
            }
        }
        for (int i=0; i<9; i++) {
            for (int j=0; j<9; j++) {
                System.out.print(sudoku[i][j] + " ");
            }
            System.out.println("");
        }
        arrange();
        for (int i=0; i<9; i++) {
            for (int j=0; j<9; j++) {
                System.out.print(sudoku[i][j] + " ");
            }
            System.out.println("");
        }
    }
    private void arrange() {
        int cas = Math.abs(generator.nextInt(3));
        for (int i=0; i<9; i++) {
            if (cas == 0) {
                if (i == 0) {
                    swap0(i);
                } else if (i == 3) {
                    swap0(i);
                } else if (i == 6) {
                    swap0(i);
                }
            } else if (cas == 1) {
                if (i == 0) {
                    swap1(i);
                } else if (i == 3) {
                    swap1(i);
                } else if (i == 6) {
                    swap1(i);
                }
            } else if (cas == 2) {
                if (i == 0) {
                    swap2(i);
                } else if (i == 3) {
                    swap2(i);
                } else if (i == 6) {
                    swap2(i);
                }
            }
            cas = Math.abs(generator.nextInt(3));
        }
    }
    private void swap0(int pivot) {
        int aux;
        for (int j=0; j<9; j++) {
            aux = sudoku[pivot][j];
            sudoku[pivot][j] = sudoku[pivot+1][j];
            sudoku[pivot+1][j] = aux;
            aux = sudoku[pivot+1][j];
            sudoku[pivot+1][j] = sudoku[pivot+2][j];
            sudoku[pivot+2][j] = aux;
        }
        for (int j=0; j<9; j++) {
            aux = sudoku[j][pivot];
            sudoku[j][pivot] = sudoku[j][pivot+2];
            sudoku[j][pivot+2] = aux;
        }
    }
    private void swap1(int pivot) {
        int aux;
        for (int j=0; j<9; j++) {
            aux = sudoku[pivot][j];
            sudoku[pivot][j] = sudoku[pivot+2][j];
            sudoku[pivot+2][j] = aux;
            aux = sudoku[pivot+1][j];
            sudoku[pivot+1][j] = sudoku[pivot+2][j];
            sudoku[pivot+2][j] = aux;
        }
        for (int j=0; j<9; j++) {
            aux = sudoku[j][pivot];
            sudoku[j][pivot] = sudoku[j][pivot+1];
            sudoku[j][pivot+1] = aux;
            aux = sudoku[j][pivot+1];
            sudoku[j][pivot+1] = sudoku[j][pivot+2];
            sudoku[j][pivot+2] = aux;
        }
    }
    private void swap2(int pivot) {
        int aux;
        for (int j=0; j<9; j++) {
            aux = sudoku[pivot][j];
            sudoku[pivot][j] = sudoku[pivot+2][j];
            sudoku[pivot+2][j] = aux;
        }
        for (int j=0; j<9; j++) {
            aux = sudoku[j][pivot];
            sudoku[j][pivot] = sudoku[j][pivot+2];
            sudoku[j][pivot+2] = aux;
            aux = sudoku[j][pivot+1];
            sudoku[j][pivot+1] = sudoku[j][pivot+2];
            sudoku[j][pivot+2] = aux;
        }
    }
    public void select() {
        int numX;
        int numY;
        genArray();
        generateSudoku();
        if (livello == 0) {
            for (int i=0; i<30; i++) {
                numX = Math.abs(generator.nextInt(9));
                numY = Math.abs(generator.nextInt(9));
                while (select[numX][numY] != -1) {
                    numX = Math.abs(generator.nextInt(9));
                    numY = Math.abs(generator.nextInt(9));
                }
                select[numX][numY] = sudoku[numX][numY];
            }
        } else if (livello == 1) {
            for (int i=0; i<20; i++) {
                numX = Math.abs(generator.nextInt(9));
                numY = Math.abs(generator.nextInt(9));
                while (select[numX][numY] != -1) {
                    numX = Math.abs(generator.nextInt(9));
                    numY = Math.abs(generator.nextInt(9));
                }
                select[numX][numY] = sudoku[numX][numY];
            }
        } else if (livello == 2) {
            for (int i=0; i<10; i++) {
                numX = Math.abs(generator.nextInt(9));
                numY = Math.abs(generator.nextInt(9));
                while (select[numX][numY] != -1) {
                    numX = Math.abs(generator.nextInt(9));
                    numY = Math.abs(generator.nextInt(9));
                }
                select[numX][numY] = sudoku[numX][numY];
            }
        }
        try {
            notifyAll();
        } catch (Exception e) {

        }
        for (int i=0; i<9; i++) {
            for (int j=0; j<9; j++) {
                System.out.print(sudoku[i][j] + " ");
            }
            System.out.println("");
        }
    }
}
```

Where the problem is here? It compiles perfectly! Nothing shows on the screen


----------



## Kreij (Aug 18, 2011)

The Main method does not do anything except create and fill 3 arrays.


----------



## Easy Rhino (Aug 18, 2011)

without any documentation in the code it is hard to understand what exactly is going on. i see the array creation and some number population based on a whole bunch of conditions but i dont see the program ever called to run.


----------



## Kreij (Aug 18, 2011)

That's what I said in my post Rhino. lol
The Main method ...

```
public Main(int livello) {
        this.livello = livello;
        for (int i=0; i<9; i++) {
            for (int j=0; j<9; j++) {
                select[i][j] = -1;
            }
        }
        for (int i=0; i<9; i++) {
            for (int j=0; j<9; j++) {
                sudoku[i][j] = -1;
            }
        }
        for (int i=0; i<9; i++) {
            serie[i] = -1;
        }
    }
```

Never calls any of the other methods in the class.
The two methods with any screen output are GenerateSudoku and Select.


----------



## xbonez (Aug 18, 2011)

Aleksander, I have seen you post a couple of threads lately regarding doubts about programming, and its hard to ignore the fact that most of your doubts are based on some very elementary issues (this one included), despite the fact that you are attempting to tackle rather complex programs (relatively).

I really suggest you read a book on OOP (C++ is a great place to start), and teach yourself the fundamentals of object oriented programming. It's a one time process of which you will reap benefits for a long, long time. Once you have a clear understanding of OOP, programming in C/C++/Java/C#/PHP/Python etc will be just a matter of familiarizing yourself with the syntax.

Just a suggestion.


----------

