Methodes en trop supprimée + nettoyage git
This commit is contained in:
parent
8adb8b4547
commit
12ae377ec0
24
Grid.java
24
Grid.java
@ -3,22 +3,22 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Grid {
|
||||
private Cell[][] cells;
|
||||
private Cell[][] cell;
|
||||
|
||||
public Grid() {
|
||||
cells = new Cell[9][9];
|
||||
cell = new Cell[9][9];
|
||||
for (int i = 0; i < 9; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
cells[i][j] = new Cell();
|
||||
cell[i][j] = new Cell();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Grid(Grid grid) {
|
||||
cells = new Cell[9][9];
|
||||
cell = new Cell[9][9];
|
||||
for (int i = 0; i < 9; i++) {
|
||||
for (int j = 0; j < 9; j++) {
|
||||
cells[i][j] = new Cell(grid.cells[i][j].getValue());
|
||||
cell[i][j] = new Cell(grid.cell[i][j].getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -32,9 +32,9 @@ public class Grid {
|
||||
line = "000000000".substring(length) + line; // Ajoute les zéros au début si nécessaire
|
||||
}
|
||||
for (int column = 0; column < 9; column++) {
|
||||
char digit = line.charAt(column);
|
||||
int value = Character.getNumericValue(digit);
|
||||
cells[ligne][column].setValue(value);
|
||||
char number = line.charAt(column);
|
||||
int value = Character.getNumericValue(number);
|
||||
cell[ligne][column].setValue(value);
|
||||
}
|
||||
}
|
||||
System.out.println("Success");
|
||||
@ -45,13 +45,13 @@ public class Grid {
|
||||
}
|
||||
|
||||
public Cell getCell(int ligne, int col) {
|
||||
return cells[ligne][col];
|
||||
return cell[ligne][col];
|
||||
}
|
||||
|
||||
public void copyFrom(Grid other) {
|
||||
public void copyFrom(Grid second_grid) {
|
||||
for (int row = 0; row < 9; row++) {
|
||||
for (int col = 0; col < 9; col++) {
|
||||
this.cells[row][col].setValue(other.cells[row][col].getValue());
|
||||
this.cell[row][col].setValue(second_grid.cell[row][col].getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -59,7 +59,7 @@ public class Grid {
|
||||
public void printGrid() {
|
||||
for (int row = 0; row < 9; row++) {
|
||||
for (int col = 0; col < 9; col++) {
|
||||
System.out.print(cells[row][col].getValue() + " ");
|
||||
System.out.print(cell[row][col].getValue() + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,16 +4,16 @@ import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class SudokuButtonListener implements ActionListener {
|
||||
private int ligne;
|
||||
private int row;
|
||||
private int col;
|
||||
private Sudoku sudoku;
|
||||
private JButton[][] boutons;
|
||||
private JButton[][] buttons;
|
||||
|
||||
public SudokuButtonListener(int ligne, int col, Sudoku sudoku, JButton[][] boutons) {
|
||||
this.ligne = ligne;
|
||||
public SudokuButtonListener(int row, int col, Sudoku sudoku, JButton[][] buttons) {
|
||||
this.row = row;
|
||||
this.col = col;
|
||||
this.sudoku = sudoku;
|
||||
this.boutons = boutons;
|
||||
this.buttons = buttons;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -22,16 +22,16 @@ public class SudokuButtonListener implements ActionListener {
|
||||
if (input != null && input.length() > 0) {
|
||||
try {
|
||||
int num = Integer.parseInt(input);
|
||||
sudoku.getGrid().getCell(ligne, col).setValue(num);
|
||||
sudoku.getGrid().getCell(row, col).setValue(num);
|
||||
if (num == 0) {
|
||||
boutons[ligne][col].setText(""); // Case vide si le nombre est 0
|
||||
buttons[row][col].setText(""); // Case vide si le nombre est 0
|
||||
} else {
|
||||
boutons[ligne][col].setText(String.valueOf(num));
|
||||
buttons[row][col].setText(String.valueOf(num));
|
||||
}
|
||||
if (!isValidMove(num, ligne, col)) {
|
||||
boutons[ligne][col].setForeground(Color.RED); // Met le texte en rouge en cas de mouvement invalide
|
||||
if (!isValid(num, row, col)) {
|
||||
buttons[row][col].setForeground(Color.RED); // Met le texte en rouge en cas de mouvement invalide
|
||||
} else {
|
||||
boutons[ligne][col].setForeground(Color.BLACK); // Réinitialise la couleur du texte
|
||||
buttons[row][col].setForeground(Color.BLACK); // Réinitialise la couleur du texte
|
||||
}
|
||||
} catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(null, "Veuillez entrer un nombre valide.");
|
||||
@ -39,13 +39,13 @@ public class SudokuButtonListener implements ActionListener {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidMove(int num, int ligne, int col) {
|
||||
return isValidRow(num, ligne) && isValidCol(num, col) && isValidBox(num, ligne - ligne % 3, col - col % 3);
|
||||
private boolean isValid(int num, int row, int col) {
|
||||
return isValidRow(num, row) && isValidCol(num, col) && isValidBox(num, row - row % 3, col - col % 3);
|
||||
}
|
||||
|
||||
private boolean isValidRow(int num, int ligne) {
|
||||
private boolean isValidRow(int num, int row) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (sudoku.getGrid().getCell(ligne, i).getValue() == num && i != col) {
|
||||
if (sudoku.getGrid().getCell(row, i).getValue() == num && i != col) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -54,19 +54,19 @@ public class SudokuButtonListener implements ActionListener {
|
||||
|
||||
private boolean isValidCol(int num, int col) {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
if (sudoku.getGrid().getCell(i, col).getValue() == num && i != ligne) {
|
||||
if (sudoku.getGrid().getCell(i, col).getValue() == num && i != row) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isValidBox(int num, int Row, int Col) {
|
||||
private boolean isValidBox(int num, int boxStartRow, int boxStartCol) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
int ligne = i + Row;
|
||||
int col = j + Col;
|
||||
if (sudoku.getGrid().getCell(ligne, col).getValue() == num && (ligne != this.ligne || col != this.col)) {
|
||||
int row = i + boxStartRow;
|
||||
int col = j + boxStartCol;
|
||||
if (sudoku.getGrid().getCell(row, col).getValue() == num && (row != this.row || col != this.col)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,19 @@
|
||||
public class SudokuSolver {
|
||||
public boolean solve(Grid grid) {
|
||||
return solveRecursive(grid);
|
||||
long startTime = System.currentTimeMillis(); // Temps de début de la résolution
|
||||
|
||||
boolean isSolved = solveRecursive(grid);
|
||||
|
||||
long endTime = System.currentTimeMillis(); // Temps de fin de la résolution
|
||||
long duration = endTime - startTime; // Calcul de la durée de résolution
|
||||
|
||||
if (isSolved) {
|
||||
System.out.println("La grille a été résolue en " + duration + " millisecondes.");
|
||||
} else {
|
||||
System.out.println("La grille est insoluble.");
|
||||
}
|
||||
|
||||
return isSolved;
|
||||
}
|
||||
|
||||
private boolean solveRecursive(Grid grid) {
|
||||
|
@ -2,6 +2,8 @@ import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||
|
||||
public class SudokuUI extends JFrame {
|
||||
private Sudoku sudoku;
|
||||
@ -23,6 +25,8 @@ public class SudokuUI extends JFrame {
|
||||
// Création du bouton "Résoudre"
|
||||
createSolveButton();
|
||||
|
||||
// Création des boutons "Charger" et "Générer"
|
||||
createLoadButton();
|
||||
createGenerateButton();
|
||||
|
||||
// Calcul de la taille préférée de la fenêtre en fonction de la taille de la grille
|
||||
@ -80,6 +84,28 @@ public class SudokuUI extends JFrame {
|
||||
add(buttonPanel, BorderLayout.SOUTH);
|
||||
}
|
||||
|
||||
private void createLoadButton() {
|
||||
JButton loadButton = new JButton("Charger");
|
||||
loadButton.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
FileNameExtensionFilter filter = new FileNameExtensionFilter("Fichiers de grille Sudoku (*.gri)", "gri");
|
||||
fileChooser.setFileFilter(filter);
|
||||
int returnValue = fileChooser.showOpenDialog(null);
|
||||
if (returnValue == JFileChooser.APPROVE_OPTION) {
|
||||
File selectedFile = fileChooser.getSelectedFile();
|
||||
sudoku.loadGridFromFile(selectedFile.getAbsolutePath());
|
||||
updateGrid();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
JPanel buttonPanel = new JPanel();
|
||||
buttonPanel.add(loadButton);
|
||||
add(buttonPanel, BorderLayout.NORTH);
|
||||
}
|
||||
|
||||
private void createGenerateButton() {
|
||||
JButton generateButton = new JButton("Générer");
|
||||
generateButton.addActionListener(new ActionListener() {
|
||||
@ -101,7 +127,6 @@ public class SudokuUI extends JFrame {
|
||||
add(buttonPanel, BorderLayout.EAST);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void updateGrid() {
|
||||
Grid grid = sudoku.getGrid();
|
||||
|
Loading…
x
Reference in New Issue
Block a user