ajout de plein de fonctionalité (sauver quitter, fin de partie , reprendre , reveler tout )
This commit is contained in:
+126
-35
@@ -9,6 +9,8 @@ public class FenetreJeu {
|
||||
private JPanel panneauGrille;
|
||||
private JButton[][] boutons;
|
||||
private Grille grille;
|
||||
private boolean partieTerminee = false;
|
||||
private JLabel labelStatut;
|
||||
|
||||
public FenetreJeu(Grille g) {
|
||||
this.grille = g;
|
||||
@@ -35,14 +37,59 @@ public class FenetreJeu {
|
||||
}
|
||||
}
|
||||
|
||||
fenetre.add(panneauGrille);
|
||||
fenetre.pack();
|
||||
fenetre.setLocationRelativeTo(null);
|
||||
fenetre.setVisible(true);
|
||||
fenetre.setLayout(new BorderLayout());
|
||||
fenetre.add(panneauGrille, BorderLayout.CENTER);
|
||||
|
||||
actualiserAffichage();
|
||||
// Panneau de contrôle en bas
|
||||
JPanel panneauBas = new JPanel();
|
||||
panneauBas.setLayout(new BorderLayout());
|
||||
|
||||
labelStatut = new JLabel("Partie en cours...", SwingConstants.CENTER);
|
||||
labelStatut.setFont(new Font("Arial", Font.BOLD, 14));
|
||||
|
||||
// pour aligner les boutons horizontalement
|
||||
JPanel conteneurBoutons = new JPanel();
|
||||
|
||||
JButton boutonMenu = new JButton("Menu Principal");
|
||||
boutonMenu.addActionListener(new ActionRetourMenu(fenetre));
|
||||
|
||||
JButton boutonSauver = new JButton("Sauver et Quitter");
|
||||
boutonSauver.addActionListener(new ActionSauverQuitter(grille));
|
||||
|
||||
conteneurBoutons.add(boutonMenu);
|
||||
conteneurBoutons.add(boutonSauver);
|
||||
|
||||
panneauBas.add(labelStatut, BorderLayout.NORTH);
|
||||
panneauBas.add(conteneurBoutons, BorderLayout.SOUTH);
|
||||
|
||||
fenetre.add(panneauBas, BorderLayout.SOUTH);
|
||||
|
||||
fenetre.pack();
|
||||
fenetre.setLocationRelativeTo(null);
|
||||
fenetre.setVisible(true);
|
||||
actualiserAffichage();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void verifierVictoire() {
|
||||
int casesAReveler = (grille.getLigne() * grille.getColonne());
|
||||
int nbMines = 0;
|
||||
int nbRevelees = 0;
|
||||
|
||||
for (int i = 0; i < grille.getLigne(); i++) {
|
||||
for (int j = 0; j < grille.getColonne(); j++) {
|
||||
if (grille.getCase(i, j).getEstMinee()) nbMines++;
|
||||
if (grille.getCase(i, j).getEstRevelee()) nbRevelees++;
|
||||
}
|
||||
}
|
||||
|
||||
if (nbRevelees == (casesAReveler - nbMines)) {
|
||||
partieTerminee = true;
|
||||
System.out.println("Vous avez gagné !");
|
||||
}
|
||||
}
|
||||
|
||||
public void revelerCaseProche(int l, int c){
|
||||
if (l < 0 || l >= grille.getLigne() || c < 0 || c >= grille.getColonne() )return;
|
||||
Case caseActuelle = grille.getCase(l, c);
|
||||
@@ -59,10 +106,24 @@ public class FenetreJeu {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void partiePerdu(){
|
||||
for(int i =0; i<grille.getLigne(); i++){
|
||||
for(int j=0;j < grille.getColonne();j++){
|
||||
Case currentCase = grille.getCase(i, j);
|
||||
if(currentCase.getEstMinee() || currentCase.getMarqueur() == 1) {
|
||||
currentCase.setEstRevelee(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// logique du clic droit
|
||||
public void clicDroit(int l, int c) {
|
||||
if (partieTerminee) return;
|
||||
Case caseCliquee = grille.getCase(l, c);
|
||||
caseCliquee.boucleMarqueur();
|
||||
actualiserAffichage();
|
||||
@@ -70,6 +131,7 @@ public class FenetreJeu {
|
||||
|
||||
// logique clic gauche
|
||||
public void clicGauche(int l, int c) {
|
||||
if (partieTerminee) return;
|
||||
Case caseCliquee = grille.getCase(l, c);
|
||||
|
||||
// secu on ne peut pas cliquer une une case avec un marqueur etoile ou ?
|
||||
@@ -77,8 +139,11 @@ public class FenetreJeu {
|
||||
if (caseCliquee.getMarqueur() != 0) return;
|
||||
|
||||
if (caseCliquee.getEstMinee()) {
|
||||
caseCliquee.setEstMineFatale(true);
|
||||
System.out.println("BOOM ! Vous avez touché une mine... !");
|
||||
caseCliquee.setEstRevelee(true);
|
||||
partieTerminee = true;
|
||||
partiePerdu();
|
||||
|
||||
} else {
|
||||
revelerCaseProche(l,c);
|
||||
@@ -89,36 +154,62 @@ public class FenetreJeu {
|
||||
|
||||
// mise à jour du texte et couleurs de tous les boutons
|
||||
public void actualiserAffichage() {
|
||||
int l = grille.getLigne();
|
||||
int c = grille.getColonne();
|
||||
|
||||
for (int i = 0; i < l; i++) {
|
||||
for (int j = 0; j < c; j++) {
|
||||
Case currentCase = grille.getCase(i, j);
|
||||
JButton btn = boutons[i][j];
|
||||
|
||||
if (currentCase.getEstRevelee()) {
|
||||
btn.setEnabled(false); // pour rendre le bouton incliquable
|
||||
if (currentCase.getEstMinee()) {
|
||||
btn.setText("¤");
|
||||
btn.setBackground(Color.RED);
|
||||
} else {
|
||||
int nb = currentCase.getNbMinesautour();
|
||||
if (nb > 0) {
|
||||
btn.setText(String.valueOf(nb));
|
||||
} else {
|
||||
btn.setText(""); // case viiiide
|
||||
}
|
||||
}
|
||||
} else {
|
||||
btn.setEnabled(true);
|
||||
int mq = currentCase.getMarqueur();
|
||||
if (mq == 1) btn.setText("*");
|
||||
else if (mq == 2) btn.setText("?");
|
||||
else btn.setText("");
|
||||
}
|
||||
}
|
||||
}
|
||||
int l = grille.getLigne();
|
||||
int c = grille.getColonne();
|
||||
|
||||
for (int i = 0; i < l; i++) {
|
||||
for (int j = 0; j < c; j++) {
|
||||
Case currentCase = grille.getCase(i, j);
|
||||
JButton btn = boutons[i][j];
|
||||
|
||||
if (currentCase.getEstRevelee()) {
|
||||
btn.setEnabled(false); // pour rendre le bouton incliquable
|
||||
|
||||
if (currentCase.getEstMinee()) {
|
||||
// C'est une vraie mine
|
||||
btn.setText("¤");
|
||||
if (currentCase.getEstMineFatale()) {
|
||||
btn.setBackground(Color.RED);
|
||||
} else {
|
||||
btn.setBackground(Color.ORANGE);
|
||||
}
|
||||
|
||||
} else {
|
||||
// Ce n'est pas une mine
|
||||
if (currentCase.getMarqueur() == 1) {
|
||||
// Erreur si le joueur a mis une fausse étoile
|
||||
btn.setText("X");
|
||||
btn.setBackground(Color.YELLOW);
|
||||
} else {
|
||||
// on affiche le chiffre ou rien
|
||||
int nb = currentCase.getNbMinesautour();
|
||||
if (nb > 0) {
|
||||
btn.setText(String.valueOf(nb));
|
||||
} else {
|
||||
btn.setText(""); // case vide
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// La case est encore cachée
|
||||
btn.setEnabled(true);
|
||||
int mq = currentCase.getMarqueur();
|
||||
if (mq == 1) btn.setText("★");
|
||||
else if (mq == 2) btn.setText("?");
|
||||
else btn.setText("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user