2024-04-28 01:40:14 +02:00
|
|
|
public class Sudoku {
|
|
|
|
private Grid grid;
|
2024-05-04 00:13:44 +02:00
|
|
|
private boolean solved;
|
2024-04-28 01:40:14 +02:00
|
|
|
|
|
|
|
public Sudoku() {
|
2024-05-04 00:13:44 +02:00
|
|
|
this.grid = new Grid(); // Initialiser avec une grille vide
|
|
|
|
this.solved = false;
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
2024-05-04 00:13:44 +02:00
|
|
|
|
|
|
|
|
2024-04-28 01:40:14 +02:00
|
|
|
|
|
|
|
public Grid getGrid() {
|
|
|
|
return grid;
|
|
|
|
}
|
|
|
|
|
2024-05-04 00:13:44 +02:00
|
|
|
public void setGrid(Grid newGrid) {
|
|
|
|
this.grid = newGrid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSolved() {
|
|
|
|
return solved;
|
|
|
|
}
|
|
|
|
|
2024-04-28 01:40:14 +02:00
|
|
|
public static void main(String[] args) {
|
2024-05-04 00:13:44 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
2024-05-04 00:13:44 +02:00
|
|
|
return true;
|
2024-04-28 01:40:14 +02:00
|
|
|
}
|
2024-05-04 00:13:44 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|