385 lines
10 KiB
Java
385 lines
10 KiB
Java
package org.Models;
|
|
|
|
/**
|
|
* Uniquement afin de se servir d'une paire de coordonées.
|
|
*/
|
|
import java.awt.Point;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Observable;
|
|
|
|
@SuppressWarnings("deprecation")
|
|
public class Grille extends Observable {
|
|
private boolean[][] grille;
|
|
public int nbLignes;
|
|
public int nbColonnes;
|
|
|
|
private PieceCourante pieceCourante;
|
|
private int pieceCouranteX;
|
|
private int pieceCouranteY;
|
|
private boolean enPause = false;
|
|
|
|
private int score = 0;
|
|
private int nbLignesSupprimees = 0;
|
|
|
|
public Grille(int nbLignes, int nbColonnes) {
|
|
this.nbLignes = nbLignes;
|
|
this.nbColonnes = nbColonnes;
|
|
this.grille = new boolean[nbLignes][nbColonnes];
|
|
initGrille();
|
|
}
|
|
|
|
public void initGrille() {
|
|
for (int i = 0; i < nbLignes; i++) {
|
|
for (int j = 0; j < nbColonnes; j++) {
|
|
this.grille[i][j] = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setCase(int i, int j, boolean value) {
|
|
if (i >= 0 && i < nbLignes && j >= 0 && j < nbColonnes) {
|
|
this.grille[i][j] = value;
|
|
setChanged();
|
|
notifyObservers();
|
|
}
|
|
}
|
|
|
|
public boolean getCase(int i, int j) {
|
|
if (i >= 0 && i < nbLignes && j >= 0 && j < nbColonnes) {
|
|
return this.grille[i][j];
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public PieceCourante getPieceCourante() {
|
|
return pieceCourante;
|
|
}
|
|
|
|
public void setPieceCourante(PieceCourante pieceCourante) {
|
|
this.pieceCourante = pieceCourante;
|
|
this.pieceCouranteX = 3;
|
|
this.pieceCouranteY = 0;
|
|
setChanged();
|
|
notifyObservers();
|
|
}
|
|
|
|
public int getPieceCouranteX() {
|
|
return pieceCouranteX;
|
|
}
|
|
|
|
public int getPieceCouranteY() {
|
|
return pieceCouranteY;
|
|
}
|
|
|
|
public void deplacerPiece(Direction direction) {
|
|
|
|
switch (direction) {
|
|
case BAS:
|
|
if (peuxBouger(direction, this.motifPieceCouranteColoriee())) {
|
|
deplacerPieceBas();
|
|
}
|
|
break;
|
|
case GAUCHE:
|
|
if (peuxBouger(direction, this.motifPieceCouranteColoriee())) {
|
|
deplacerPieceGauche();
|
|
}
|
|
break;
|
|
case DROITE:
|
|
if (peuxBouger(direction, this.motifPieceCouranteColoriee())) {
|
|
deplacerPieceDroite();
|
|
}
|
|
break;
|
|
case TOUTENBAS:
|
|
if (peuxBouger(direction, this.motifPieceCouranteColoriee())) {
|
|
deplacerPieceToutEnBas();
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void deplacerPieceBas() {
|
|
pieceCouranteY++;
|
|
|
|
setChanged();
|
|
notifyObservers();
|
|
}
|
|
|
|
private void deplacerPieceGauche() {
|
|
pieceCouranteX--;
|
|
|
|
setChanged();
|
|
notifyObservers();
|
|
}
|
|
|
|
private void deplacerPieceDroite() {
|
|
pieceCouranteX++;
|
|
|
|
setChanged();
|
|
notifyObservers();
|
|
}
|
|
|
|
private void deplacerPieceToutEnBas() {
|
|
while (peuxBouger(Direction.BAS, this.motifPieceCouranteColoriee())) {
|
|
deplacerPieceBas();
|
|
}
|
|
}
|
|
|
|
public int getNbLignes() {
|
|
return nbLignes;
|
|
}
|
|
|
|
public int getNbColonnes() {
|
|
return nbColonnes;
|
|
}
|
|
|
|
public List<Point> motifPieceCouranteColoriee() {
|
|
List<Point> casesColores = new ArrayList<>();
|
|
|
|
boolean[][] motif = pieceCourante.getMotif();
|
|
for (int i = 0; i < motif.length; i++) {
|
|
for (int j = 0; j < motif[i].length; j++) {
|
|
if (motif[i][j]) {
|
|
casesColores.add(new Point(pieceCouranteX + j, pieceCouranteY + i));
|
|
}
|
|
}
|
|
}
|
|
|
|
return casesColores;
|
|
}
|
|
|
|
public boolean peuxBouger(Direction direction, List<Point> motif) {
|
|
int deltaX = 0;
|
|
int deltaY = 0;
|
|
|
|
switch (direction) {
|
|
case GAUCHE:
|
|
deltaX = -1;
|
|
break;
|
|
case DROITE:
|
|
deltaX = 1;
|
|
break;
|
|
case BAS:
|
|
deltaY = 1;
|
|
break;
|
|
case TOUTENBAS:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
List<Point> nouvellePosition = calculerNouvellePosition(motif, deltaX, deltaY);
|
|
return positionValide(nouvellePosition);
|
|
}
|
|
|
|
public boolean[][] getGrille() {
|
|
return grille;
|
|
}
|
|
|
|
public void fixerPiece() {
|
|
for (Point caseColoree : motifPieceCouranteColoriee()) {
|
|
setCase(caseColoree.y, caseColoree.x, true);
|
|
}
|
|
|
|
|
|
verifierEtSupprimerLignesSiBesoin();
|
|
}
|
|
|
|
public boolean doitFixerPiece() {
|
|
if (!peuxBouger(Direction.BAS, this.motifPieceCouranteColoriee())) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void setEnPause(boolean enPause) {
|
|
this.enPause = enPause;
|
|
}
|
|
|
|
public boolean estEnPause() {
|
|
return enPause;
|
|
}
|
|
|
|
public void verifierEtSupprimerLignesSiBesoin() {
|
|
int tmpNbLignesSupprimees = 0;
|
|
System.out.println("Debut uppression d'une ligne......");
|
|
for (int i = nbLignes - 1; i >= 0; i--) {
|
|
boolean ligneSupprimable = true;
|
|
|
|
for (int j = 0; j < nbColonnes; j++) {
|
|
if (!this.grille[i][j]) {
|
|
ligneSupprimable = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (ligneSupprimable) {
|
|
supprimerLigne(i);
|
|
nbLignesSupprimees += 1;
|
|
i++;
|
|
tmpNbLignesSupprimees++;
|
|
}
|
|
}
|
|
System.out.println(tmpNbLignesSupprimees + " Lignes supprimées");
|
|
switch (tmpNbLignesSupprimees) {
|
|
case 1:
|
|
score += 100;
|
|
break;
|
|
case 2:
|
|
score += 300;
|
|
break;
|
|
case 3:
|
|
score += 500;
|
|
break;
|
|
case 4:
|
|
score += 800;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
public int getScore() {
|
|
return score;
|
|
}
|
|
|
|
public void setScore(int score) {
|
|
this.score = score;
|
|
}
|
|
|
|
public int getNbLignesSupprimees() {
|
|
return nbLignesSupprimees;
|
|
}
|
|
|
|
public void setNbLignesSupprimees(int nbLignesSupprimees) {
|
|
this.nbLignesSupprimees = nbLignesSupprimees;
|
|
}
|
|
|
|
public void supprimerLigne(int i) {
|
|
for (int ligne = i; ligne > 0; ligne--) {
|
|
for (int j = 0; j < nbColonnes; j++) {
|
|
this.setCase(ligne, j, this.getCase(ligne - 1, j));
|
|
}
|
|
}
|
|
|
|
for (int j = 0; j < nbColonnes; j++) {
|
|
this.setCase(0, j, false);
|
|
}
|
|
|
|
setChanged();
|
|
notifyObservers();
|
|
}
|
|
|
|
public List<Point> getLignePoints(int i) {
|
|
List<Point> coordonnesCase = new ArrayList<>();
|
|
|
|
for (int j = 0; j < nbColonnes; j++) {
|
|
coordonnesCase.add(new Point(j, i));
|
|
}
|
|
|
|
return coordonnesCase;
|
|
}
|
|
|
|
public void descendreLigne(int i) {
|
|
for (int j = 0; j < nbColonnes; j++) {
|
|
List<Point> coordonnesCase = new ArrayList<>();
|
|
coordonnesCase.add(new Point(j, i));
|
|
|
|
if (this.grille[i][j]) {
|
|
this.setCase(i + 1, j, true);
|
|
}
|
|
|
|
this.setCase(i, j, false);
|
|
}
|
|
}
|
|
|
|
public void tournerPiece(Orientation orientation) {
|
|
if (peuxTournerPiece(orientation)) {
|
|
boolean[][] motif = pieceCourante.getMotif();
|
|
boolean[][] nouveauMotif;
|
|
|
|
if (orientation == Orientation.SENSHORAIRE) {
|
|
nouveauMotif = PieceCourante.rotationHoraire(motif);
|
|
} else {
|
|
nouveauMotif = PieceCourante.rotationAntiHoraire(motif);
|
|
}
|
|
|
|
pieceCourante.setMotif(nouveauMotif);
|
|
|
|
setChanged();
|
|
notifyObservers();
|
|
}
|
|
}
|
|
|
|
public boolean peuxTournerPiece(Orientation orientation) {
|
|
List<Point> positionApresRotation = calculerPositionApresRotation(orientation);
|
|
return positionValide(positionApresRotation);
|
|
}
|
|
|
|
private List<Point> calculerPositionApresRotation(Orientation orientation) {
|
|
List<Point> nouvellePosition = new ArrayList<>();
|
|
boolean[][] motifActuel = pieceCourante.getMotif();
|
|
boolean[][] motifTourne;
|
|
|
|
boolean[][] motifCopie = new boolean[motifActuel.length][motifActuel[0].length];
|
|
for (int i = 0; i < motifActuel.length; i++) {
|
|
for (int j = 0; j < motifActuel[i].length; j++) {
|
|
motifCopie[i][j] = motifActuel[i][j];
|
|
}
|
|
}
|
|
|
|
if (orientation == Orientation.SENSHORAIRE) {
|
|
motifTourne = PieceCourante.rotationHoraire(motifCopie);
|
|
} else {
|
|
motifTourne = PieceCourante.rotationAntiHoraire(motifCopie);
|
|
}
|
|
|
|
for (int i = 0; i < motifTourne.length; i++) {
|
|
for (int j = 0; j < motifTourne[i].length; j++) {
|
|
if (motifTourne[i][j]) {
|
|
nouvellePosition.add(new Point(pieceCouranteX + j, pieceCouranteY + i));
|
|
}
|
|
}
|
|
}
|
|
|
|
return nouvellePosition;
|
|
}
|
|
|
|
public boolean positionValide(List<Point> points) {
|
|
for (Point point : points) {
|
|
if (point.x < 0 || point.x >= nbColonnes || point.y < 0 || point.y >= nbLignes) {
|
|
return false;
|
|
}
|
|
|
|
if (grille[point.y][point.x]) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private List<Point> calculerNouvellePosition(List<Point> points, int deltaX, int deltaY) {
|
|
List<Point> nouvellePosPoints = new ArrayList<>();
|
|
|
|
for (Point point : points) {
|
|
nouvellePosPoints.add(new Point(point.x + deltaX, point.y + deltaY));
|
|
}
|
|
|
|
return nouvellePosPoints;
|
|
}
|
|
|
|
public void reinitialiserGrille() {
|
|
initGrille();
|
|
score = 0;
|
|
nbLignesSupprimees = 0;
|
|
pieceCourante = null;
|
|
pieceCouranteX = 3;
|
|
pieceCouranteY = 0;
|
|
setChanged();
|
|
notifyObservers();
|
|
}
|
|
|
|
} |