Methodes en trop supprimée + nettoyage git
This commit is contained in:
96
Sudoku.java
96
Sudoku.java
@@ -6,9 +6,13 @@ public class Sudoku {
|
||||
this.grid = new Grid(); // Initialiser avec une grille vide
|
||||
this.solved = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 Grid getGrid() {
|
||||
return grid;
|
||||
}
|
||||
@@ -20,14 +24,10 @@ public class Sudoku {
|
||||
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 loadGridFromFile(String fileName) {
|
||||
this.grid.loadGridFromFile(fileName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void printGrid() {
|
||||
for (int row = 0; row < 9; row++) {
|
||||
@@ -39,82 +39,4 @@ public class Sudoku {
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user