test
This commit is contained in:
commit
907925c4d3
@ -6,6 +6,10 @@ import java.io.FileWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
// CODE TEMPORAIRE
|
||||
// A AMÉLIORER FORTEMENT !!!!!!
|
||||
|
||||
|
||||
/**
|
||||
* Class to manage grid saving and loading operations.
|
||||
* @author Moncef STITI
|
||||
|
25
src/GrilleButtonClickListener.java
Normal file
25
src/GrilleButtonClickListener.java
Normal file
@ -0,0 +1,25 @@
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class GrilleButtonClickListener implements ActionListener {
|
||||
private Window window;
|
||||
|
||||
public GrilleButtonClickListener(Window window) {
|
||||
this.window = window;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
Button sourceButton = (Button) e.getSource();
|
||||
String buttonText = sourceButton.getText();
|
||||
|
||||
// Si le bouton "Retour au menu principal" est cliqué, on efface ce qui est présent et on met le menu jouer
|
||||
if (buttonText.equals("Retour au menu principal")) {
|
||||
if (window.getContentPane().getComponent(0) instanceof GrilleView) {
|
||||
GrilleView grilleView = (GrilleView) window.getContentPane().getComponent(0);
|
||||
grilleView.removeAllComponents(window);
|
||||
window.changeMenu(new PlayMenuView(this.window)); // PAS BON CAR ON VA CRÉE UN MENU JOUER À CHAQUE FOIS AU LIEU DE RÉUTILISER L'ANCIEN
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,15 +1,69 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class GrilleView extends JFrame {
|
||||
public class GrilleView extends JPanel {
|
||||
private final Font BUTTON_FONT = new Font("Copperplate", Font.BOLD, 24);
|
||||
private final String[] BUTTON_TEXTS = {"Retour au menu principal"};
|
||||
private final Dimension BUTTON_SIZE = new Dimension(450, 60);
|
||||
private Grille grille;
|
||||
private Window window;
|
||||
private JTextField[][] cases;
|
||||
|
||||
public GrilleView(Grille grille) {
|
||||
public GrilleView(Grille grille, Window window) {
|
||||
super();
|
||||
this.grille = grille;
|
||||
initUI();
|
||||
this.window = window;
|
||||
initialize();
|
||||
}
|
||||
|
||||
private void initUI() {
|
||||
// EN COURS
|
||||
private void initialize() {
|
||||
// Définir le style de fond similaire à HomeView
|
||||
this.setBackground(new Color(54, 91, 109));
|
||||
|
||||
// Définir le gestionnaire de disposition et créer le tableau de cases
|
||||
this.setLayout(new BorderLayout());
|
||||
JPanel grillePanel = new JPanel(new GridLayout(9, 9));
|
||||
grillePanel.setBackground(new Color(54, 91, 109));
|
||||
this.add(grillePanel, BorderLayout.CENTER);
|
||||
|
||||
this.cases = new JTextField[9][9];
|
||||
|
||||
// Créer les cases et les ajoutez à la grille
|
||||
for (int row = 0; row < 9; row++) {
|
||||
for (int col = 0; col < 9; col++) {
|
||||
int valeur = this.grille.getValeur(row, col);
|
||||
JTextField caseField = new JTextField(1);
|
||||
if (valeur != 0) {
|
||||
caseField.setText(Integer.toString(valeur));
|
||||
}
|
||||
caseField.setHorizontalAlignment(JTextField.CENTER);
|
||||
caseField.setEditable(false);
|
||||
this.cases[row][col] = caseField;
|
||||
grillePanel.add(caseField);
|
||||
}
|
||||
}
|
||||
|
||||
// Ajouter un bouton "Retour au menu principal" en bas
|
||||
Button retourButton = new Button(BUTTON_TEXTS[0], BUTTON_SIZE, BUTTON_FONT);
|
||||
retourButton.addActionListener(new GrilleButtonClickListener(window));
|
||||
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
||||
buttonPanel.setBackground(new Color(54, 91, 109));
|
||||
buttonPanel.add(retourButton);
|
||||
this.add(buttonPanel, BorderLayout.SOUTH);
|
||||
}
|
||||
|
||||
public void addComponentsToWindow() {
|
||||
window.getContentPane().add(this);
|
||||
window.setPageTitle("Grille");
|
||||
window.pack();
|
||||
window.setLocationRelativeTo(null);
|
||||
window.setVisible(true);
|
||||
}
|
||||
|
||||
// Method to remove all components from the window
|
||||
public void removeAllComponents(Window window) {
|
||||
window.remove(this);
|
||||
window.revalidate();
|
||||
window.repaint();
|
||||
}
|
||||
}
|
||||
|
@ -76,10 +76,4 @@ public class HomeView extends JPanel {
|
||||
window.setLocationRelativeTo(null);
|
||||
window.setVisible(true);
|
||||
}
|
||||
|
||||
public void removeAllComponents() {
|
||||
window.getContentPane().removeAll();
|
||||
window.revalidate();
|
||||
window.repaint();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
|
||||
public class PlayButtonClickListener implements ActionListener {
|
||||
private Window window;
|
||||
@ -13,15 +15,44 @@ public class PlayButtonClickListener implements ActionListener {
|
||||
Button sourceButton = (Button) e.getSource();
|
||||
String buttonText = sourceButton.getText();
|
||||
|
||||
// Handle different button actions based on their text
|
||||
if (buttonText.equals("Générer une grille")) {
|
||||
if (buttonText.equals("Jouer sans grille")) {
|
||||
Grille nouvelleGrille = new Grille();
|
||||
SudokuGenerator sudokuGenerator = new SudokuGenerator(nouvelleGrille);
|
||||
SudokuGenerator sudokuGenerator = new SudokuGenerator(nouvelleGrille,81); // 81 = générer une grille avec 81 espaces vides
|
||||
Grille grilleGeneree = sudokuGenerator.genererGrille(); // Générer la grille
|
||||
FileManager.saveGrille(grilleGeneree);
|
||||
GrilleView grilleView1 = new GrilleView(grilleGeneree, window); // Créer la vue de la grille
|
||||
window.changeMenu(grilleView1); // Modifier le menu pour afficher la grille générée
|
||||
}
|
||||
|
||||
else if (buttonText.equals("Générer une grille")) {
|
||||
// Demander à l'utilisateur combien de cases vides ils souhaite sur sa grille
|
||||
String input = JOptionPane.showInputDialog(window, "Combien de cases vides voulez-vous sur la grille ?", "Nombre de cases vides", JOptionPane.QUESTION_MESSAGE);
|
||||
|
||||
// Vérifier si l'utilisateur a annulé la saisie ou a fermé la fenêtre de dialogue
|
||||
if (input != null && !input.isEmpty()) {
|
||||
int nombreChiffres = Integer.parseInt(input);
|
||||
|
||||
// Vérifier que le nombre est compris entre 0 et 81
|
||||
if (nombreChiffres >= 0 && nombreChiffres <= 81) {
|
||||
Grille nouvelleGrille = new Grille();
|
||||
SudokuGenerator sudokuGenerator = new SudokuGenerator(nouvelleGrille, nombreChiffres);
|
||||
Grille grilleGeneree = sudokuGenerator.genererGrille(); // Générer la grille
|
||||
|
||||
// Créer la vue de la grille
|
||||
GrilleView grilleView1 = new GrilleView(grilleGeneree, window);
|
||||
|
||||
// Modifier le menu pour afficher la grille générée
|
||||
window.changeMenu(grilleView1);
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(window, "Veuillez saisir un nombre entre 0 et 81.", "Erreur", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
} else {
|
||||
// L'utilisateur a annulé la saisie ou fermé la fenêtre de dialogue, traitement en conséquence
|
||||
}
|
||||
} else if (buttonText.equals("Charger une grille")) {
|
||||
Grille grille = FileManager.chargerGrille();
|
||||
GrilleView test = new GrilleView(grille);
|
||||
GrilleView grilleView = new GrilleView(grille, window);
|
||||
window.changeMenu(grilleView);
|
||||
|
||||
} else if (buttonText.equals("Retour au menu principal")) {
|
||||
if (window.getContentPane().getComponent(0) instanceof PlayMenuView) {
|
||||
PlayMenuView playMenuView = (PlayMenuView) window.getContentPane().getComponent(0);
|
||||
|
@ -9,7 +9,7 @@ public class PlayMenuView extends JPanel {
|
||||
private final Color TITLE_TEXT_COLOR = Color.WHITE;
|
||||
private final Font TITLE_FONT = new Font("Copperplate", Font.BOLD, 40);
|
||||
private final Font BUTTON_FONT = new Font("Copperplate", Font.BOLD, 24);
|
||||
private final String[] BUTTON_TEXTS = {"Générer une grille", "Charger une grille"};
|
||||
private final String[] BUTTON_TEXTS = {"Jouer sans grille", "Générer une grille", "Charger une grille"};
|
||||
private Title titleLabel;
|
||||
private Button[] playModeButtons;
|
||||
private Button returnButton;
|
||||
@ -37,7 +37,8 @@ public class PlayMenuView extends JPanel {
|
||||
for (int i = 0; i < BUTTON_TEXTS.length; i++) {
|
||||
Button button = new Button(BUTTON_TEXTS[i], BUTTON_SIZE, BUTTON_FONT, BACKGROUND_COLOR);
|
||||
playModeButtons[i] = button;
|
||||
button.addActionListener(new PlayButtonClickListener(window));
|
||||
PlayButtonClickListener ecouteurBouton = new PlayButtonClickListener(window);
|
||||
button.addActionListener(ecouteurBouton);
|
||||
buttonPanel.add(button);
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,12 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class SudokuGenerator {
|
||||
private int nombreCasesVides = 40;
|
||||
private int nombreCasesVides;
|
||||
private Grille grille;
|
||||
|
||||
public SudokuGenerator(Grille grille) {
|
||||
public SudokuGenerator(Grille grille, int nombreCasesVides) {
|
||||
this.grille = grille;
|
||||
this.nombreCasesVides = nombreCasesVides;
|
||||
}
|
||||
|
||||
public Grille genererGrille() {
|
||||
@ -50,8 +51,6 @@ public class SudokuGenerator {
|
||||
return solver.resoudreSudoku(grilleTemp);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Méthode de remplissage de la grille et remplissage des diagonales...
|
||||
private void remplirDiagonales() {
|
||||
for (int i = 0; i < 9; i += 3) {
|
||||
|
@ -51,5 +51,4 @@ public class Window extends JFrame {
|
||||
revalidate();
|
||||
repaint();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user