public class Sudoku { private Grid grid; private boolean solved; public Sudoku() { this.grid = new Grid(); // Initialiser avec une grille vide this.solved = false; } public Grid getGrid() { return grid; } public void setGrid(Grid newGrid) { this.grid = newGrid; } public boolean isSolved() { return solved; } public static void main(String[] args) { Sudoku sudoku = new Sudoku(); sudoku.printGrid(); // Afficher la grille non résolue dans la console new SudokuUI(sudoku); } public void printGrid() { for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { int value = grid.getCell(row, col).getValue(); System.out.print(value + " "); } System.out.println(); // Passer à la ligne suivante après chaque ligne de la grille } } public boolean generateSudoku() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (!solveSudoku(i, j)) { return false; } } } return true; } private boolean solveSudoku(int row, int col) { if (row == 9 - 1 && col == 9) { return true; } if (col == 9) { row++; col = 0; } if (grid.getCell(row, col).getValue() != 0) { return solveSudoku(row, col + 1); } for (int num = 1; num <= 9; num++) { if (isSafe(row, col, num)) { grid.getCell(row, col).setValue(num); if (solveSudoku(row, col + 1)) { return true; } } grid.getCell(row, col).setValue(0); } return false; } private boolean isSafe(int row, int col, int num) { // Vérifiez si nous trouvons le même numéro dans la même ligne, // la même colonne ou la même sous-grille return !findInRow(row, num) && !findInCol(col, num) && !findInBox(row - row % 3, col - col % 3, num); } // Méthodes findInRow, findInCol et findInBox ici private boolean findInRow(int row, int num) { for (int col = 0; col < 9; col++) { if (grid.getCell(row, col).getValue() == num) { return true; } } return false; } private boolean findInCol(int col, int num) { for (int row = 0; row < 9; row++) { if (grid.getCell(row, col).getValue() == num) { return true; } } return false; } private boolean findInBox(int boxStartRow, int boxStartCol, int num) { for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { if (grid.getCell(row + boxStartRow, col + boxStartCol).getValue() == num) { return true; } } } return false; } public void loadGridFromFile(String fileName) { this.grid.loadGridFromFile(fileName); } }