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