Ajout de la résolution automatique pour la partie joueur

This commit is contained in:
Yann KERAUDREN 2024-05-05 04:53:24 +02:00
parent d4df7ad880
commit c9fd0bfeb6
2 changed files with 117 additions and 4 deletions

108
Resolver.java Normal file

@ -0,0 +1,108 @@
import java.util.ArrayList;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Math.*;
import java.text.DecimalFormat;
public class Resolver implements ActionListener {
private JTextField[][] grid;
private int GRID_SIZE;
public Resolver(JTextField[][] grid, int GRID_SIZE) {
this.grid = grid;
this.GRID_SIZE = GRID_SIZE;
}
@Override
public void actionPerformed(ActionEvent e) {
resolution();
}
private void resolution() {
long depart = System.nanoTime();
ArrayList<JTextField> case_vide = new ArrayList<JTextField>();
do {
for (int row = 0; row < this.GRID_SIZE; row++) {
for (int col = 0; col < this.GRID_SIZE; col++) {
if (grid[row][col].getText().isEmpty()) {
if (!case_vide.contains(grid[row][col])) {
case_vide.add(grid[row][col]);
}
//Création de la liste de chiffre acceptable
ArrayList<String> chiffre_possible = new ArrayList<String>();
for (int i = 1; i <= GRID_SIZE; i++) {
chiffre_possible.add(""+i);
}
//recherche des chiffres present sur la ligne
for (int col2 = 0; col2 < this.GRID_SIZE; col2++) {
if (!grid[row][col2].getText().isEmpty()) {
chiffre_possible.remove(""+grid[row][col2].getText());
}
}
//recherche des chiffres sur la colonne
for (int row2 = 0; row2 < this.GRID_SIZE; row2++) {
if (!grid[row2][col].getText().isEmpty()) {
chiffre_possible.remove(""+grid[row2][col].getText());
}
}
//recherche des chiffres dans la région
int rowregion = row/3*3;
int colregion = col/3*3;
for (int row2 = rowregion; row2 < rowregion + 3; row2++) {
for (int col2 = colregion; col2 < colregion + 3; col2++) {
if (!grid[row2][col2].getText().isEmpty()) {
chiffre_possible.remove(""+grid[row2][col2].getText());
}
}
}
if (chiffre_possible.size() == 1) {
grid[row][col].setText(chiffre_possible.getFirst());
case_vide.remove(grid[row][col]);
}
}
}
}
}while (!case_vide.isEmpty());
double durée = (((System.nanoTime()) - depart )* Math.pow(10,-9));
DecimalFormat df = new DecimalFormat("0.00");
JOptionPane.showMessageDialog(null, "Grille terminé en " + df.format(durée) + " secondes", "Résolution automatique", JOptionPane.INFORMATION_MESSAGE);
}
}

@ -53,24 +53,29 @@ public class SudokuGridJoueur extends JFrame {
// Bouton pour sauvegarder la grille
JButton save = new JButton("Sauvegarder");
SaveButton saver = new SaveButton(GRID_SIZE,grid);
SaveButton saver = new SaveButton(GRID_SIZE,grid);
save.addActionListener(saver);
// Bouton pour chargé la grille
JButton load = new JButton("Charger");
LoadButton loader = new LoadButton(GRID_SIZE, grid, status);
LoadButton loader = new LoadButton(GRID_SIZE, grid, status);
load.addActionListener(loader);
//Bouton pour résoudre la grille
JButton resolve = new JButton("Résoudre");
Resolver resolver = new Resolver(grid, GRID_SIZE);
resolve.addActionListener(resolver);
bouton.add(load);
bouton.add(save);
bouton.add(resolve);
// Ajout des panneaux à la fenetre