samedi 31 mars 2018

Java 2D array doesn't modify when it should

I'm trying to remove values in a 2D array to create a sudoku grid that people can solve but when I call the method in my other class, it doesn't do anything to the grid. There is another method that checks if the grid is perfect, turns the generated 1D grid into a 2D grid and then the holes should be added. Here is the method and how I call it:

public int[][] generateHoles(int[][] fullGrid, int difficulty){
    int[][] holes = new int[9][9];
    for(int i = 0; i < 81; i++) {
        holes[i/9][i%9] = fullGrid[i/9][i%9];
    }
    int nbrCellulesAEnlever;

    switch(difficulty){
    case 1: //Grille façile
        rand = new Random();
        nbrCellulesAEnlever = (int) 0.57*(fullGrid.length*fullGrid[0].length); //Enlève 57% des nombres
        while(nbrCellulesAEnlever != 0) {
            int i = rand.nextInt(81);
            System.out.println(i);
            holes[i/9][i%9] = 0;
            nbrCellulesAEnlever--;
        }
        break;
    case 2: // Grille facile-moyenne
        rand = new Random();
        nbrCellulesAEnlever = (int) 0.61*(fullGrid.length*fullGrid[0].length); //Enlève 61% des nombres
        while(nbrCellulesAEnlever != 0) {
            int i = rand.nextInt(81);
            holes[i/9][i%9] = 0;
            nbrCellulesAEnlever--;
        }
        break;
    case 3: // Grille moyenne
        rand = new Random();
        nbrCellulesAEnlever = (int) 0.64*(fullGrid.length*fullGrid[0].length); //Enlève 64% des nombres
        while(nbrCellulesAEnlever != 0) {
            int i = rand.nextInt(81);
            holes[i/9][i%9] = 0;
            nbrCellulesAEnlever--;
        }
        break;
    case 4: // Grille moyenne-difficile
        rand = new Random();
        nbrCellulesAEnlever = (int) 0.68*(fullGrid.length*fullGrid[0].length); //Enlève 68% des nombres
        while(nbrCellulesAEnlever != 0) {
            int i = rand.nextInt(81);
            holes[i/9][i%9] = 0;
            nbrCellulesAEnlever--;
        }
        break;
    case 5: // Grille difficile
        rand = new Random();
        nbrCellulesAEnlever = (int) 0.72*(fullGrid.length*fullGrid[0].length); //Enlève 72% des nombres
        while(nbrCellulesAEnlever != 0) {
            int i = rand.nextInt(81);
            holes[i/9][i%9] = 0;
            nbrCellulesAEnlever--;
        }
        break;
    default: // Grille de difficulté random
        double random = ThreadLocalRandom.current().nextDouble(0.5, 0.79); //Crée un % entre 50% inclus et 79% exclus
        nbrCellulesAEnlever = (int) random*(fullGrid.length*fullGrid[0].length);
        while(nbrCellulesAEnlever != 0) {
            int i = rand.nextInt(81);
            holes[i/9][i%9] = 0;
            nbrCellulesAEnlever--;
        }
        break;
    }
    return holes;
}

And I call it in another class like this:

SudokuGenerator sg = new SudokuGenerator();
if(sg.isPerfect(sg.generateGrid())) {
    affichage(sg.generateHoles(sg.transform(sg.generateGrid()), 1));
}




Aucun commentaire:

Enregistrer un commentaire