import java.util.Random;

public class GenerateGrid {
    private static final int GRID_SIZE = 9;
    private static final int EMPTY_CELL = 0;
    private static final int MINIMUM_CLUES = 25; // Modifier ce nombre en fonction du nombre de clues désiré


    public static void main(String[] args) {
        Grid grid = generatePuzzleGrid();
        printGrid(grid);
    }

    public static Grid generatePuzzleGrid() {
        return generateGrid();
    }

    public static Grid generateGrid() {
        Grid grid = new Grid();
        solveSudoku(grid);
        removeNumbers(grid);
        return grid;
    }

    private static boolean solveSudoku(Grid grid) {
        return solveSudokuHelper(grid, 0, 0);
    }

    private static boolean solveSudokuHelper(Grid grid, int row, int col) {
        if (row == GRID_SIZE) {
            row = 0;
            if (++col == GRID_SIZE) {
                return true;
            }
        }

        if (grid.getCell(row, col).getValue() != EMPTY_CELL) {
            return solveSudokuHelper(grid, row + 1, col);
        }

        Random random = new Random();
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        shuffleArray(numbers, random);

        for (int num : numbers) {
            if (isValidMove(grid, row, col, num)) {
                grid.getCell(row, col).setValue(num);
                if (solveSudokuHelper(grid, row + 1, col)) {
                    return true;
                }
                grid.getCell(row, col).setValue(EMPTY_CELL);
            }
        }

        return false;
    }

    private static boolean isValidMove(Grid grid, int row, int col, int num) {
        for (int i = 0; i < GRID_SIZE; i++) {
            if (grid.getCell(row, i).getValue() == num || grid.getCell(i, col).getValue() == num) {
                return false;
            }
        }

        int boxRow = row - row % 3;
        int boxCol = col - col % 3;

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (grid.getCell(boxRow + i, boxCol + j).getValue() == num) {
                    return false;
                }
            }
        }

        return true;
    }

    private static void shuffleArray(int[] array, Random random) {
        for (int i = array.length - 1; i > 0; i--) {
            int index = random.nextInt(i + 1);
            int temp = array[index];
            array[index] = array[i];
            array[i] = temp;
        }
    }

    public static void removeNumbers(Grid grid) {
        Random random = new Random();

        while (countClues(grid) > MINIMUM_CLUES) {
            int row = random.nextInt(GRID_SIZE);
            int col = random.nextInt(GRID_SIZE);
            int value = grid.getCell(row, col).getValue();
            grid.getCell(row, col).setValue(EMPTY_CELL);

            Grid tempGrid = new Grid();
            if (!solveSudoku(tempGrid)) {
                grid.getCell(row, col).setValue(value);
            }
        }
    }

    private static int countClues(Grid grid) {
        int count = 0;
        for (int row = 0; row < GRID_SIZE; row++) {
            for (int col = 0; col < GRID_SIZE; col++) {
                if (grid.getCell(row, col).getValue() != EMPTY_CELL) {
                    count++;
                }
            }
        }
        return count;
    }

    public static void printGrid(Grid grid) {
        for (int row = 0; row < GRID_SIZE; row++) {
            for (int col = 0; col < GRID_SIZE; col++) {
                System.out.print(grid.getCell(row, col).getValue() + " ");
            }
            System.out.println();
        }
    }
}