minage opérationel!
This commit is contained in:
parent
8999b949e1
commit
6708bb15e4
61
Case.java
61
Case.java
@ -1,32 +1,31 @@
|
|||||||
/*public class Case {
|
import javax.swing.*;
|
||||||
// Réalisation du damier et ajout d'éléments graphiques
|
import java.awt.*;
|
||||||
for (int currentLine=0; currentLine<lignes; currentLine++){
|
|
||||||
for(int currentCase = 0; currentCase < colonnes; currentCase++){
|
|
||||||
if (currentLine%2==0){
|
public class Case extends JComponent{
|
||||||
if (currentCase%2==0){
|
private JPanel panel= new JPanel();
|
||||||
JPanel dark = new JPanel();
|
|
||||||
dark.setSize(caseSize);
|
public Case (Dimension caseSize, boolean visible, boolean minee){
|
||||||
dark.setBackground(gray2);
|
super();
|
||||||
grille.add(dark);
|
this.panel.setSize(caseSize);
|
||||||
} else {
|
if (visible==false){
|
||||||
JPanel light = new JPanel();
|
Color gray2 = new Color(70,70,70);
|
||||||
light.setSize(caseSize);
|
this.panel.setBackground(gray2);
|
||||||
light.setBackground(gray1);
|
} else if (visible==true){
|
||||||
grille.add(light);
|
Color gray1 = new Color(80,80,80);
|
||||||
}
|
this.panel.setBackground(gray1);
|
||||||
} else {
|
if (minee==true){
|
||||||
if (currentCase%2==0){
|
Color rose = new Color(236,0,140);
|
||||||
JPanel light = new JPanel();
|
this.panel.setBackground(rose);
|
||||||
light.setSize(caseSize);
|
System.out.println("J'ai miné ! ");
|
||||||
light.setBackground(gray1);
|
|
||||||
grille.add(light);
|
|
||||||
} else {
|
|
||||||
JPanel dark = new JPanel();
|
|
||||||
dark.setSize(caseSize);
|
|
||||||
dark.setBackground(gray2);
|
|
||||||
grille.add(dark);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}*/
|
}
|
||||||
|
|
||||||
|
public JPanel getCase(){
|
||||||
|
return this.panel;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -12,8 +12,7 @@ public class FrameJeu{
|
|||||||
JFrame fenetre = new JFrame("Démineur - En Jeu");
|
JFrame fenetre = new JFrame("Démineur - En Jeu");
|
||||||
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
// Couleurs
|
// Couleur
|
||||||
//Color rose = new Color(236,0,140);
|
|
||||||
Color vert = new Color(0, 236, 96);
|
Color vert = new Color(0, 236, 96);
|
||||||
|
|
||||||
// Mise en page
|
// Mise en page
|
||||||
@ -25,7 +24,7 @@ public class FrameJeu{
|
|||||||
Dimension grilleSize= grille.getGrilleSize();
|
Dimension grilleSize= grille.getGrilleSize();
|
||||||
JPanel banniere= new JPanel();
|
JPanel banniere= new JPanel();
|
||||||
banniere.setBackground(vert);
|
banniere.setBackground(vert);
|
||||||
Dimension banniereSize=new Dimension(grilleSize.width,grilleSize.height/10);
|
Dimension banniereSize=new Dimension(grilleSize.width,grilleSize.height/8);
|
||||||
banniere.setSize(banniereSize);
|
banniere.setSize(banniereSize);
|
||||||
|
|
||||||
// Mise en place de la bannière
|
// Mise en place de la bannière
|
||||||
@ -33,7 +32,7 @@ public class FrameJeu{
|
|||||||
miseEnPage.gridx = 0;
|
miseEnPage.gridx = 0;
|
||||||
miseEnPage.gridy = 0;
|
miseEnPage.gridy = 0;
|
||||||
miseEnPage.weightx =1;
|
miseEnPage.weightx =1;
|
||||||
miseEnPage.weighty = 0.1;
|
miseEnPage.weighty = 0.15;
|
||||||
fenetre.add(banniere, miseEnPage);
|
fenetre.add(banniere, miseEnPage);
|
||||||
|
|
||||||
|
|
||||||
@ -41,13 +40,13 @@ public class FrameJeu{
|
|||||||
miseEnPage.gridx = 0;
|
miseEnPage.gridx = 0;
|
||||||
miseEnPage.gridy = 1;
|
miseEnPage.gridy = 1;
|
||||||
miseEnPage.weightx =1;
|
miseEnPage.weightx =1;
|
||||||
miseEnPage.weighty = 0.9;
|
miseEnPage.weighty = 0.85;
|
||||||
miseEnPage.fill = GridBagConstraints.BOTH;
|
miseEnPage.fill = GridBagConstraints.BOTH;
|
||||||
fenetre.add(grille.getGrille(), miseEnPage);
|
fenetre.add(grille.getGrille(), miseEnPage);
|
||||||
|
|
||||||
// Mise en place de la fenêtre et affichage
|
// Mise en place de la fenêtre et affichage
|
||||||
fenetre.pack();
|
fenetre.pack();
|
||||||
fenetre.setSize(grilleSize.width,grilleSize.height*11/10);
|
fenetre.setSize(grilleSize.width,grilleSize.height*9/8);
|
||||||
Dimension fenetreSize=fenetre.getSize();
|
Dimension fenetreSize=fenetre.getSize();
|
||||||
fenetre.setLocation(screenSize.width/2-(fenetreSize.width/2), screenSize.height/2-(fenetreSize.height/2));
|
fenetre.setLocation(screenSize.width/2-(fenetreSize.width/2), screenSize.height/2-(fenetreSize.height/2));
|
||||||
//fenetre.setResizable(false);
|
//fenetre.setResizable(false);
|
||||||
|
74
Grille.java
74
Grille.java
@ -1,5 +1,6 @@
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class Grille extends JComponent {
|
public class Grille extends JComponent {
|
||||||
private Dimension grilleSize=new Dimension(0,0);
|
private Dimension grilleSize=new Dimension(0,0);
|
||||||
@ -10,7 +11,7 @@ public class Grille extends JComponent {
|
|||||||
|
|
||||||
// Découpage de l'espace en fonction des paramètres pour la mise en place de la grille
|
// Découpage de l'espace en fonction des paramètres pour la mise en place de la grille
|
||||||
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
this.grilleSize = new Dimension(((screenSize.height*2/3)/lignes)*colonnes, screenSize.height*2/3 );
|
this.grilleSize = new Dimension(((screenSize.height*3/4)/lignes)*colonnes, screenSize.height*3/4 );
|
||||||
Dimension caseSize = new Dimension(this.grilleSize.height/lignes,this.grilleSize.height/lignes);
|
Dimension caseSize = new Dimension(this.grilleSize.height/lignes,this.grilleSize.height/lignes);
|
||||||
GridLayout damier = new GridLayout(lignes,colonnes);
|
GridLayout damier = new GridLayout(lignes,colonnes);
|
||||||
System.out.println("Taille des cases : "+caseSize);
|
System.out.println("Taille des cases : "+caseSize);
|
||||||
@ -18,45 +19,54 @@ public class Grille extends JComponent {
|
|||||||
this.grille.setLayout(damier);
|
this.grille.setLayout(damier);
|
||||||
this.grille.setSize(grilleSize);
|
this.grille.setSize(grilleSize);
|
||||||
|
|
||||||
// Création des couleurs
|
|
||||||
Color gray1 = new Color(80,80,80);
|
|
||||||
Color gray2 = new Color(70,70,70);
|
|
||||||
|
|
||||||
// Réalisation du damier et ajout d'éléments graphiques
|
// Initialisation : les cases sont cachées
|
||||||
for (int currentLine=0; currentLine<lignes; currentLine++){
|
boolean[] visible = new boolean[lignes*colonnes];
|
||||||
for(int currentCase = 0; currentCase < colonnes; currentCase++){
|
for (int i=0; i<lignes*colonnes;i++){
|
||||||
if (currentLine%2==0){
|
visible[i]=true;
|
||||||
if (currentCase%2==0){
|
}
|
||||||
JPanel dark = new JPanel();
|
|
||||||
dark.setSize(caseSize);
|
|
||||||
dark.setBackground(gray2);
|
// Instalation des mines
|
||||||
this.grille.add(dark);
|
// Création d'un tableau de nombres aléatoires pour savoir où les placer
|
||||||
} else {
|
int[] caseMine = new int[mines];
|
||||||
JPanel light = new JPanel();
|
Random mine = new Random();
|
||||||
light.setSize(caseSize);
|
for (int i=0;i<mines;i++){
|
||||||
light.setBackground(gray1);
|
caseMine[i]=mine.nextInt(lignes*colonnes);
|
||||||
this.grille.add(light);
|
// On vérifie qu'on n'a pas miné une case déjà minée
|
||||||
}
|
for (int j=0;j<mines;j++){
|
||||||
} else {
|
if (caseMine[j]==caseMine[i]){
|
||||||
if (currentCase%2==0){
|
caseMine[i]=mine.nextInt(lignes*colonnes);
|
||||||
JPanel light = new JPanel();
|
|
||||||
light.setSize(caseSize);
|
|
||||||
light.setBackground(gray1);
|
|
||||||
this.grille.add(light);
|
|
||||||
} else {
|
|
||||||
JPanel dark = new JPanel();
|
|
||||||
dark.setSize(caseSize);
|
|
||||||
dark.setBackground(gray2);
|
|
||||||
this.grille.add(dark);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
for (int i=0;i<mines;i++){
|
||||||
|
System.out.println(""+caseMine[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comme pour savoir si une case est visible, mais pour quand elle est minée
|
||||||
|
boolean[] minee = new boolean[lignes*colonnes];
|
||||||
|
for (int i=0; i<lignes*colonnes;i++){
|
||||||
|
for (int j=0;j<mines;j++){
|
||||||
|
if (caseMine[j]==i){
|
||||||
|
minee[i]=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Affichage des cases
|
||||||
|
for (int i=0;i<lignes*colonnes;i++){
|
||||||
|
Case panel = new Case(caseSize,visible[i], minee[i]);
|
||||||
|
this.grille.add(panel.getCase());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Méthode pour obtenir la taille de la grille de jeu
|
||||||
public Dimension getGrilleSize() {
|
public Dimension getGrilleSize() {
|
||||||
return grilleSize;
|
return grilleSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Methode pour obtenir le panel pour l'affichage de la grille
|
||||||
public JPanel getGrille() {
|
public JPanel getGrille() {
|
||||||
return grille;
|
return grille;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user