fix: piece generation

This commit is contained in:
Morph01
2025-05-19 22:02:16 +02:00
committed by Morph01
parent a8f543114e
commit 85aafa8240
14 changed files with 205 additions and 41 deletions

View File

@@ -21,6 +21,7 @@ public class Grille extends Observable { // TODO: ?? implements Runnable {
this.nbColonnes = nbColonnes;
this.grille = new boolean[nbLignes][nbColonnes];
initGrille();
this.grille[nbLignes - 1][nbColonnes - 1] = true;
}
public void initGrille() {
@@ -70,22 +71,22 @@ public class Grille extends Observable { // TODO: ?? implements Runnable {
switch (direction) {
case BAS:
if (peuxBouger(direction)) {
if (peuxBouger(direction, this.motifPieceCouranteColoriee())) {
deplacerPieceBas();
}
break;
case GAUCHE:
if (peuxBouger(direction)) {
if (peuxBouger(direction, this.motifPieceCouranteColoriee())) {
deplacerPieceGauche();
}
break;
case DROITE:
if (peuxBouger(direction)) {
if (peuxBouger(direction, this.motifPieceCouranteColoriee())) {
deplacerPieceDroite();
}
break;
case TOUTENBAS:
if (peuxBouger(direction)) {
if (peuxBouger(direction, this.motifPieceCouranteColoriee())) {
deplacerPieceToutEnBas();
}
default:
@@ -115,7 +116,7 @@ public class Grille extends Observable { // TODO: ?? implements Runnable {
}
private void deplacerPieceToutEnBas() {
while (peuxBouger(Direction.BAS)) {
while (peuxBouger(Direction.BAS, this.motifPieceCouranteColoriee())) {
deplacerPieceBas();
}
}
@@ -166,7 +167,7 @@ public class Grille extends Observable { // TODO: ?? implements Runnable {
return casesColores;
}
public boolean peuxBouger(Direction direction) {
public boolean peuxBouger(Direction direction, List<Point> motif) {
int deltaX = 0;
int deltaY = 0;
@@ -186,8 +187,7 @@ public class Grille extends Observable { // TODO: ?? implements Runnable {
break;
}
List<Point> motifPieceColoree = motifPieceCouranteColoriee();
for (Point caseColoree : motifPieceColoree) {
for (Point caseColoree : motif) {
int newX = caseColoree.x + deltaX;
int newY = caseColoree.y + deltaY;
@@ -211,10 +211,13 @@ public class Grille extends Observable { // TODO: ?? implements Runnable {
for (Point caseColoree : motifPieceCouranteColoriee()) {
setCase(caseColoree.y, caseColoree.x, true);
}
System.err.println("post fixage de piece");
verifierEtSupprimerLignesSiBesoin();
}
public boolean doitFixerPiece() {
if (!peuxBouger(Direction.BAS)) {
if (!peuxBouger(Direction.BAS, this.motifPieceCouranteColoriee())) {
return true;
}
@@ -230,4 +233,96 @@ public class Grille extends Observable { // TODO: ?? implements Runnable {
}
// public void verifierEtSupprimerLignesSiBesoin() {
// 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;
// }
// }
// if (ligneSupprimable) {
// supprimerLigne(i);
// }
// }
// }
public void verifierEtSupprimerLignesSiBesoin() {
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);
i++;
}
}
}
// // TODO : EUHHHHHHHHHH JE CROIS PAS que ça marche comme ça c'est pas en mode
// gravité récursive, c'est juste tout descend de n lignes cassées
// // dès qu'on a fixe la pièce courante on verifie le nombre de lignes a
// supprimes
// // dans la grille
// // ->
// // faire tomber JUSQU'EN bas la ligne X au dessus des n lines supprimes
// // PAR CONTRE faire tomber de n lignes toutes les lignes au dessus de X.
// while (peuxBouger(Direction.BAS, getLignePoints(i))) {
// System.err.println("testtttttttttt descnete");
// descendreLigne(i - 1);
// }
// // for (int j = i - 2; j > 0; j--) {
// // descendreLigne(j);
// // }
// }
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);
}
}
}

View File

@@ -2,8 +2,15 @@ package org.Models;
import java.awt.Point;
import java.util.Observable;
import java.util.Random;
import org.Models.Pieces.PieceI;
import org.Models.Pieces.PieceJ;
import org.Models.Pieces.PieceL;
import org.Models.Pieces.PieceO;
import org.Models.Pieces.PieceS;
import org.Models.Pieces.PieceT;
import org.Models.Pieces.PieceZ;
@SuppressWarnings("deprecation")
public class Jeu extends Observable implements Runnable {
@@ -15,16 +22,16 @@ public class Jeu extends Observable implements Runnable {
private int pieceSuivanteX;
private int pieceSuivanteY;
// TODO : remove test variable
public int test = 0;
public boolean jeuEnCours = true;
public Jeu(Grille grille, Musique musique) {
System.err.println("init jeu");
this.grille = grille;
this.musique = musique;
System.err.println("init nouvelle piece courante");
this.grille.setPieceCourante(getNouvellePiece());
System.err.println("init nouvelle piece suivante");
this.pieceSuivante = getNouvellePiece();
this.ordonnanceur = new Ordonnanceur(this, 1000);
@@ -32,10 +39,46 @@ public class Jeu extends Observable implements Runnable {
}
private PieceCourante getNouvellePiece() {
this.pieceSuivante = new PieceL();
this.pieceSuivanteX = 3;
this.pieceSuivanteY = 0;
return pieceSuivante;
Random random = new Random();
int randomiiii = random.nextInt(6);
System.err.println("randomiiiii : " + randomiiii);
PieceCourante nouvellePiece;
switch (randomiiii) {
case 0:
nouvellePiece = new PieceI();
System.err.println("piece I");
break;
case 1:
nouvellePiece = new PieceL();
System.err.println("piece L");
break;
case 2:
nouvellePiece = new PieceJ();
System.err.println("piece J");
break;
case 3:
nouvellePiece = new PieceO();
System.err.println("piece O");
break;
case 4:
nouvellePiece = new PieceS();
System.err.println("piece S");
break;
case 5:
nouvellePiece = new PieceT();
System.err.println("piece T");
break;
case 6:
nouvellePiece = new PieceZ();
System.err.println("piece Z");
break;
default:
nouvellePiece = new PieceL();
break;
}
return nouvellePiece;
}
public PieceCourante getPieceSuivante() {
@@ -91,7 +134,8 @@ public class Jeu extends Observable implements Runnable {
this.grille.deplacerPiece(Direction.BAS);
} else {
this.grille.fixerPiece();
this.grille.setPieceCourante(getNouvellePiece());
this.grille.setPieceCourante(pieceSuivante);
this.pieceSuivante = getNouvellePiece();
}
setChanged();

View File

@@ -1,6 +1,5 @@
package org.Models;
public interface PieceCourante {
boolean[][] motif = new boolean[4][4];
abstract public boolean[][] getMotif();
}

View File

@@ -1,18 +0,0 @@
package org.Models.Pieces;
import org.Models.PieceCourante;
public class PieceCarre implements PieceCourante {
public PieceCarre() {
motif[1][1] = true;
motif[1][2] = true;
motif[2][1] = true;
motif[2][2] = true;
}
@Override
public boolean[][] getMotif() {
return motif;
}
}

View File

@@ -4,6 +4,8 @@ import org.Models.PieceCourante;
public class PieceI implements PieceCourante {
private boolean[][] motif = new boolean[4][4];
public PieceI() {
motif[0][1] = true;
motif[1][1] = true;

View File

@@ -4,6 +4,8 @@ import org.Models.PieceCourante;
public class PieceJ implements PieceCourante {
private boolean[][] motif = new boolean[3][3];
public PieceJ() {
motif[0][2] = true;
motif[1][2] = true;

View File

@@ -4,12 +4,14 @@ import org.Models.PieceCourante;
public class PieceL implements PieceCourante {
private boolean[][] motif = new boolean[3][3];
public PieceL(){
motif[0][0] = true;
motif[1][0] = true;
motif[2][0] = true;
motif[3][0] = true;
motif[3][1] = true;
motif[3][2] = true;
motif[2][1] = true;
motif[2][2] = true;
}
@Override

View File

@@ -3,11 +3,13 @@ package org.Models.Pieces;
import org.Models.PieceCourante;
public class PieceO implements PieceCourante {
private boolean[][] motif = new boolean[4][4];
public PieceO() {
motif[0][1] = true;
motif[0][2] = true;
motif[1][1] = true;
motif[1][2] = true;
motif[2][1] = true;
motif[2][2] = true;
}
@Override

View File

@@ -4,6 +4,8 @@ import org.Models.PieceCourante;
public class PieceS implements PieceCourante {
private boolean[][] motif = new boolean[3][3];
public PieceS() {
motif[0][1] = true;
motif[1][1] = true;

View File

@@ -4,6 +4,8 @@ import org.Models.PieceCourante;
public class PieceT implements PieceCourante {
private boolean[][] motif = new boolean[3][3];
public PieceT() {
motif[0][1] = true;
motif[1][1] = true;

View File

@@ -4,6 +4,8 @@ import org.Models.PieceCourante;
public class PieceZ implements PieceCourante {
private boolean[][] motif = new boolean[3][3];
public PieceZ() {
motif[0][2] = true;
motif[1][2] = true;