1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
package jeu;
import javax.swing.*;
import java.awt.*;
public class Labyrinthe extends JPanel{
/************************************** VARIABLES **************************************/
private int taille;
private ImageIcon caseVide = new ImageIcon(new ImageIcon(getClass().getResource("/Vide.png")).getImage().getScaledInstance(35, 35, Image.SCALE_DEFAULT));
private ImageIcon caseMur = new ImageIcon(new ImageIcon(getClass().getResource("/Mur.png")).getImage().getScaledInstance(35, 35, Image.SCALE_DEFAULT));
private Dimension dim = new Dimension(caseVide.getIconWidth(), caseVide.getIconHeight());
public int[][] grille;
public JButton[][] button;
public JPanel panel;
/************************************** CONSTRUCTEUR **********************************/
public Labyrinthe(int taille){
this.taille = taille;
creerLab();
}
/************************************** FONCTIONS *************************************/
public void creerLab() {
if (taille == 1) {
//Création du labyrinthe de taille 1, les "1" seront des murs et les "0" des espaces vides
grille = new int[][]{
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 },
{ 1, 1, 0, 1, 0, 1, 0, 1, 0, 1 },
{ 1, 0, 0, 1, 0, 1, 1, 1, 0, 1 },
{ 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 1, 0, 1 },
{ 1, 0, 1, 1, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 1, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 }
};
}
else if (taille == 2) {
//Création du labyrinthe de taille 2, les "1" seront des murs et les "0" des espaces vides
grille = new int[][]{
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0 },
{ 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0 },
{ 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0 },
{ 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0 },
{ 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 }
};
}
else if (taille == 3) {
//Création du labyrinthe de taille 3, les "1" seront des murs et les "0" des espaces vides
grille = new int[][]{
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1 },
{ 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 0, 0, 0, 0,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{ 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1 },
{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1 },
{ 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
}
//Création d'une matrice de boutons
button = new JButton[grille.length][grille[0].length];
panel = new JPanel();
panel.setLayout(new GridLayout(grille.length, grille[0].length));
this.setBorder(BorderFactory.createEmptyBorder(13, 13, 13, 13));
//Début de parcours de la matrice
for (int i = 0; i < grille.length; ++i)
for (int j = 0; j < grille[0].length; ++j) {
if (grille[i][j] == 0)
//Image de fond blanc
button[i][j] = new JButton(caseVide);
else
//Image de fond noir
button[i][j] = new JButton(caseMur);
button[i][j].setPreferredSize(dim);
button[i][j].setBorder(null);
panel.add(button[i][j]);
} //Fin de parcours de la matrice
this.add(panel);
} //Fin creerLab()
/************************************** ALGORITHME DE RESOLUTION *************************************/
public boolean resolutionLab(int pos_x, int pos_y) {
boolean done = false;
//Si la fonction estValide() renvoie true
if (estValide(pos_x, pos_y)) {
grille[pos_x][pos_y] = 3;
//Si la case passée en paramètre est la dernière case du labyrinthe
if (pos_x == grille.length-1 && pos_y == grille[0].length-1)
return true;
else {
//Appels récursifs
done = resolutionLab(pos_x + 1, pos_y);
if (!done)
done = resolutionLab(pos_x, pos_y + 1);
if (!done)
done = resolutionLab(pos_x - 1, pos_y);
if (!done)
done = resolutionLab(pos_x, pos_y - 1);
}
if (done)
grille[pos_x][pos_y] = 7;
}
return done;
} //Fin resolutionLab()
private boolean estValide(int pos_x, int pos_y) {
boolean result = false;
//Si la case passée en paramètre est dans les dimensions du labyrinthe
if (pos_x >= 0 && pos_x < grille.length && pos_y >= 0 && pos_y < grille[0].length)
//Et si la case est un espace vide
if (grille[pos_x][pos_y] == 0)
result = true;
return result;
} //Fin estValide()
} //Fin class |
Partager