2026-03-30 22:33:20 +02:00
|
|
|
import java.io.Serializable;
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
|
|
public class Grille implements Serializable {
|
|
|
|
|
private Case[][] plateau;
|
|
|
|
|
private int lignes;
|
|
|
|
|
private int colonnes;
|
|
|
|
|
private int mines;
|
2026-04-07 16:21:33 +02:00
|
|
|
private boolean partieTerminee;
|
2026-03-30 22:33:20 +02:00
|
|
|
|
|
|
|
|
public Grille(int l, int c, int m) {
|
|
|
|
|
this.lignes = l;
|
|
|
|
|
this.colonnes = c;
|
|
|
|
|
this.mines = m;
|
2026-04-07 13:54:01 +02:00
|
|
|
this.partieTerminee = false;
|
2026-03-30 22:33:20 +02:00
|
|
|
|
|
|
|
|
this.plateau = new Case[this.lignes][this.colonnes];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < this.lignes; i++) {
|
|
|
|
|
for (int j = 0; j < this.colonnes; j++) {
|
|
|
|
|
this.plateau[i][j] = new Case();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-03 18:41:45 +02:00
|
|
|
|
|
|
|
|
placerMines();
|
|
|
|
|
calculerVoisins();
|
2026-03-30 22:33:20 +02:00
|
|
|
}
|
2026-04-03 18:41:45 +02:00
|
|
|
|
|
|
|
|
public int getLignes() { return this.lignes; }
|
|
|
|
|
public int getColonnes() { return this.colonnes; }
|
2026-04-07 13:54:01 +02:00
|
|
|
public Case getCase(int l, int c) { return this.plateau[l][c]; }
|
|
|
|
|
public boolean isPartieTerminee() { return this.partieTerminee; }
|
|
|
|
|
public void setPartieTerminee(boolean terminee) { this.partieTerminee = terminee; }
|
2026-04-03 18:41:45 +02:00
|
|
|
|
2026-04-07 16:21:33 +02:00
|
|
|
// --- NOUVELLE MÉTHODE : Calcul des mines restantes ---
|
|
|
|
|
public int getMinesRestantes() {
|
|
|
|
|
int drapeaux = 0;
|
|
|
|
|
for (int i = 0; i < this.lignes; i++) {
|
|
|
|
|
for (int j = 0; j < this.colonnes; j++) {
|
|
|
|
|
if (this.plateau[i][j].getMarqueur() == 1) { // 1 = étoile
|
|
|
|
|
drapeaux++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return this.mines - drapeaux;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 18:41:45 +02:00
|
|
|
private void placerMines() {
|
|
|
|
|
Random rand = new Random();
|
|
|
|
|
int minesPlacees = 0;
|
|
|
|
|
while (minesPlacees < this.mines) {
|
|
|
|
|
int l = rand.nextInt(this.lignes);
|
|
|
|
|
int c = rand.nextInt(this.colonnes);
|
|
|
|
|
if (!this.plateau[l][c].isMinee()) {
|
|
|
|
|
this.plateau[l][c].setMinee(true);
|
|
|
|
|
minesPlacees++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void calculerVoisins() {
|
|
|
|
|
for (int i = 0; i < this.lignes; i++) {
|
|
|
|
|
for (int j = 0; j < this.colonnes; j++) {
|
|
|
|
|
if (!this.plateau[i][j].isMinee()) {
|
|
|
|
|
int compteMines = 0;
|
|
|
|
|
for (int di = -1; di <= 1; di++) {
|
|
|
|
|
for (int dj = -1; dj <= 1; dj++) {
|
|
|
|
|
int voisinI = i + di;
|
|
|
|
|
int voisinJ = j + dj;
|
|
|
|
|
if (voisinI >= 0 && voisinI < this.lignes && voisinJ >= 0 && voisinJ < this.colonnes) {
|
|
|
|
|
if (this.plateau[voisinI][voisinJ].isMinee()) {
|
|
|
|
|
compteMines++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.plateau[i][j].setNbMinesVoisines(compteMines);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean revelerCase(int l, int c) {
|
|
|
|
|
Case caseCliquee = this.plateau[l][c];
|
2026-04-07 16:21:33 +02:00
|
|
|
if (caseCliquee.isRevelee() || caseCliquee.getMarqueur() == 1) return false;
|
2026-04-03 18:41:45 +02:00
|
|
|
caseCliquee.setRevelee(true);
|
2026-04-07 16:21:33 +02:00
|
|
|
if (caseCliquee.isMinee()) return true;
|
2026-04-03 18:41:45 +02:00
|
|
|
|
|
|
|
|
if (caseCliquee.getNbMinesVoisines() == 0) {
|
|
|
|
|
for (int di = -1; di <= 1; di++) {
|
|
|
|
|
for (int dj = -1; dj <= 1; dj++) {
|
|
|
|
|
int voisinL = l + di;
|
|
|
|
|
int voisinC = c + dj;
|
|
|
|
|
if (voisinL >= 0 && voisinL < this.lignes && voisinC >= 0 && voisinC < this.colonnes) {
|
|
|
|
|
revelerCase(voisinL, voisinC);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-07 13:54:01 +02:00
|
|
|
return false;
|
2026-04-03 18:41:45 +02:00
|
|
|
}
|
2026-04-07 13:54:01 +02:00
|
|
|
|
2026-04-03 18:41:45 +02:00
|
|
|
public boolean estGagnee() {
|
|
|
|
|
int casesRevelees = 0;
|
|
|
|
|
for (int i = 0; i < this.lignes; i++) {
|
|
|
|
|
for (int j = 0; j < this.colonnes; j++) {
|
2026-04-07 16:21:33 +02:00
|
|
|
if (this.plateau[i][j].isRevelee()) casesRevelees++;
|
2026-04-03 18:41:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-07 16:21:33 +02:00
|
|
|
return casesRevelees == ((this.lignes * this.colonnes) - this.mines);
|
2026-04-03 18:41:45 +02:00
|
|
|
}
|
2026-04-07 13:54:01 +02:00
|
|
|
}
|