Skip to content

Commit

Permalink
Fix bug deplacement limite de grille et correctifs
Browse files Browse the repository at this point in the history
  • Loading branch information
Svvmy committed Jan 13, 2024
1 parent 0271dde commit eea2cf4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 85 deletions.
18 changes: 14 additions & 4 deletions src/main/java/Classes/Fantome.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ public void deplacer(char[][] terrain) {

System.out.println("Postion Fantome "+ position.getPositionX() +" "+position.getPositionY());

// Choisir une direction aléatoire (0 pour haut, 1 pour droite, 2 pour bas, 3 pour gauche)
// Choisir une direction aléatoire (0 pour haut, 1 pour droite, 2 pour bas, 3 pour gauche)
do {
direction = random.nextInt(4);
System.out.println("doWHile et direction: "+direction);
System.out.println("Direction aléatoirement choisi: "+direction);
} while (!peutSeDeplacer(terrain, direction, positionX, positionY));

// direction = random.nextInt(4);
// System.out.println("Direction aléatoirement choisi: "+direction);
// if(!peutSeDeplacer(terrain, direction, positionX, positionY)){

// if (this.game.modeSansMurIsActive()) {
// direction = (random.nextBoolean()) ? 0 : 2; // Choisir aléatoirement entre haut (0) et bas (2)
// } else {
Expand Down Expand Up @@ -117,8 +122,13 @@ private boolean peutSeDeplacer(char[][] terrain, int direction, int positionX, i
}

// Vérifier les limites de la grille
if (newX < 0 || newX >= this.game.getTerrain()[0].length-1 || newY < 0 || newY >= this.game.getTerrain().length+1) {
System.out.println("Limite de grille " + direction);
if (newX < 0 || newX >= this.game.getTerrain().length || newY < 0 || newY >= this.game.getTerrain()[0].length) {
System.out.println("Fantome a atteint la limite de grille " + direction);
System.out.println("Valeur de NewX = "+newX);
System.out.println("TabX valeur = "+this.game.getTerrain()[0].length);
System.out.println("Valeur de NewY = "+newY);
System.out.println("TabY valeur = "+this.game.getTerrain().length);

return false; // En dehors de la grille
}

Expand Down
17 changes: 14 additions & 3 deletions src/main/java/Classes/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Game {
private boolean started;
private int niveauActuel;
private boolean isWin; //True si on a gagne ou false sinon.
private String result;
List<Boule> boules = new ArrayList<>();
List<SuperBoule> superBoules = new ArrayList<>();
List<Fruit> fruits = new ArrayList<>();
Expand Down Expand Up @@ -48,6 +49,7 @@ public void updateGrid(int direction) {
afficherGrille(this.terrain.getGrille());
}else{
System.out.println("FIN DU JEU Valeur de Started"+this.started);
this.initialiserJeu();
}
}

Expand Down Expand Up @@ -80,8 +82,9 @@ public void finJeu(){
}


public void reinitialiserGrille() { // Réinitialise la map d'origine
public void reinitialiserGrille() {
this.terrain = new Terrain();
this.terrain.setInitGrille(); // Réinitialise la map d'origine
initialiserFruitEtBoule(); // TO DO reinitiliser et save les manger
reinitialiserFantomesEtPacman();
}
Expand Down Expand Up @@ -241,19 +244,27 @@ public void checkWin(){ //TO DO win par niveau ?
if (this.niveauActuel == 3 && this.toutesLesBoulesMangees()){
this.isWin = true;
this.started =false;
this.setResult("WIN");
}
}

public void gameOver(){
this.isWin = false;
this.started = false;
this.setResult("GAMEOVER");
}


public String getResult() { // Retouren si on a gagner ou perdu la partie
if (this.started && !this.isWin) { // si le jeu est lancer
return "Partie en cours";
}
return this.isWin? "WIN" : "Game Over !";
if(!this.started && this.score == 0){
return "No Start";
}
return this.result;
}

public void setResult(String res){
this.result = res;
}
}
32 changes: 4 additions & 28 deletions src/main/java/Classes/PacMan.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Classes;

import javax.swing.text.Position;
import java.util.Random;

public class PacMan extends Personnage {
Expand Down Expand Up @@ -131,6 +132,7 @@ private boolean peutSeDeplacer(char[][] terrain, int direction, int positionX, i
// Nouvelles coordonnées après le déplacement
int newX = positionX;
int newY = positionY;
System.out.println("POSITION X "+positionX+" POSITION Y "+ positionY);

switch (direction) {
case 0:
Expand All @@ -151,38 +153,12 @@ private boolean peutSeDeplacer(char[][] terrain, int direction, int positionX, i
}

// Vérifier les limites de la grille
if (newX < 0 || newX >= terrain[0].length-1 || newY < 0 || newY >= this.game.getTerrain().length+1) {
System.out.println("Pacman Limite de grille " + direction+" "+"NextX: "+newX+"NextY "+newY);
if (newX < 0 || newX >= terrain.length || newY < 0 || newY >= terrain[0].length) {
System.out.println("Pacman Limite de grille " + direction+" "+"NextX: "+newX+" NextY "+newY);
System.out.println("Taille Tab X "+terrain[0].length+" "+"Taille Tab Y "+terrain.length);
return false; // En dehors de la grille
}

// if (newX < 0) {
// System.out.println("1 newX < 0");
// System.out.println("Pacman Limite de grille " + direction+" "+"NextX: "+newX+"NextY "+newY);
// System.out.println("Taille Tab X "+terrain[0].length+" "+"Taille Tab Y "+terrain.length);
// return false; // En dehors de la grille
// }
//
// if ( newX >= terrain[0].length-1) {
// System.out.println("2 newX >= terrain[0].length-1");
// System.out.println("Pacman Limite de grille " + direction+" "+"NextX: "+newX+"NextY "+newY);
// System.out.println("Taille Tab X "+terrain[0].length+" "+"Taille Tab Y "+terrain.length);
// return false; // En dehors de la grille
// }
// if ( newY < 0 ) {
// System.out.println("3 newY < 0");
// System.out.println("Pacman Limite de grille " + direction+" "+"NextX: "+newX+"NextY "+newY);
// System.out.println("Taille Tab X "+terrain[0].length+" "+"Taille Tab Y "+terrain.length);
// return false; // En dehors de la grille
// }
// if (newY >= terrain.length-1) {
// int test = terrain.length-1;
// System.out.println("4 newY >= terrain.length-1 "+test);
// System.out.println("Pacman Limite de grille " + direction+" "+"NextX: "+newX+"NextY "+newY);
// System.out.println("Taille Tab X "+terrain[0].length+" "+"Taille Tab Y "+terrain.length);
// return false; // En dehors de la grille
// }

// Vérifier s'il y a un mur à la nouvelle position
if (terrain[newX][newY] == 'M') {
Expand Down
102 changes: 52 additions & 50 deletions src/main/java/Classes/Terrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,47 @@ public class Terrain {
public Terrain() {

grilleActuel = new char[][]{
{'P', 'o', 'o', '.'},
{'.', '.', '.', 'O'},
{'.', '.', '.', 'O'},
{'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
{'M', 'P', 'o', 'M', '.', '.', 'M', '.', 'M', '.', '.', 'M', '.', 'M', 'M', '.', '.', '.', '.', '.', '.', 'M'},
{'M', '.', '.', '.', 'M', '.', 'M', '.', 'M', '.', 'M', '.', 'F', '.', '.', '.', '.', 'M', 'M', 'M', '.', 'M'},
{'M', 'O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'M', 'M', 'M'},
};

// grilleActuel = new char[][]{
// {'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
// {'M', 'P', 'o', 'M', '.', 'o', 'M', '.', 'M', 'o', 'M', 'M', '.', 'M', 'M', '.', '.', '.', '.', '.', 'M', 'M'},
// {'M', 'M', '.', '.', 'M', '.', 'M', 'o', 'M', '.', 'M', '.', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
// {'M', 'o', '.', '.', 'o', '.', '.', '.', '.', '.', 'o', '.', '.', 'o', '.', '.', '.', '.', 'F', 'M', 'M', 'M'},
// {'M', 'M', '.', '.', 'M', 'M', 'M', 'M', 'M', 'M', '.', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
// {'M', '.', '.', 'M', '.', '.', '.', '.', 'M', 'M', '.', 'o', 'M', '.', 'M'},
// {'M', '.', '.', 'o', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'M'},
// {'M', '.', '.', '.', '.', 'M', 'M', 'M', '.', '.', 'O', 'M', 'M', 'M', 'M'},
// {'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
// };

grilleInit = new char[][] {
{'F', 'o', 'O', 'P', 'M', '.', '.', '.', '.', '.', '.', '.', '.', 'O', '.'},
{'K', 'M', '.', 'M', 'M', '.', '.', '.', 'M', 'M', '.', 'o', 'M', '.', '.'},
{'K', 'M', 'M', 'O', 'M', 'M', '.', 'M', '.', 'C', '.', '.', '.', 'K', '.'},
{'O', '.', 'M', '.', '.', 'M', '.', 'M', '.', '.', 'O', 'M', 'M', 'M', '.'},
{'.', '.', '.', 'B', '.', '.', '.', 'M', 'M', 'M', '.', '.', 'K', 'M', 'M'},
{'B', 'C', 'M', '.', '.', 'M', '.', 'O', 'M', '.', '.', 'M', '.', 'K', 'C'},
{'K', '.', '.', 'O', '.', '.', '.', 'M', '.', 'M', '.', 'M', 'M', 'M', '.'},
{'M', 'M', 'M', '.', 'M', 'B', 'M', 'K', 'M', '.', 'M', 'K', 'M', '.', 'o'},
{'.', '.', 'M', '.', 'M', '.', 'M', '.', 'M', 'o', 'M', '.', '.', 'B', '.'},
{'M', 'M', 'M', '.', '.', '.', 'M', 'M', 'M', '.', '.', 'C', 'M', 'M', '.'},
{'K', 'o', '.', '.', '.', 'B', 'C', '.', 'M', '.', 'O', '.', 'M', '.', '.'},
{'M', 'M', 'M', 'M', 'M', 'M', 'M', 'K', '.', 'C', '.', 'O', 'M', '.', '.'},
{'.', '.', '.', 'M', '.', 'C', '.', '.', '.', 'M', '.', 'M', 'M', 'O', 'C'},
{'C', 'O', '.', 'M', '.', '.', '.', 'B', '.', '.', 'B', 'M', '.', '.', '.'},
{'F', '.', '.', '.', '.', 'M', 'M', 'M', '.', '.', '.', 'o', 'K', 'M', '.'},
{'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
{'M', 'P', 'o', 'M', '.', '.', 'M', '.', 'M', '.', '.', 'M', '.', 'M', 'M', '.', '.', '.', '.', '.', '.', 'M'},
{'M', '.', '.', '.', 'M', '.', 'M', '.', 'M', '.', 'M', '.', 'F', '.', '.', '.', '.', 'M', 'M', 'M', '.', 'M'},
{'M', 'O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'M', 'M', 'M'},
};

grilleNiveau2 = new char[][] {
{'P', 'o', 'O', 'F', 'M', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', 'M', '.', 'M', 'M', '.', '.', '.', 'M', 'M', '.', 'o', 'M', '.', '.'},
{'.', 'M', 'M', 'O', 'M', 'M', '.', 'M', '.', '.', '.', '.', '.', '.', '.'},
{'O', '.', 'M', '.', '.', 'M', '.', 'M', '.', '.', 'O', 'M', 'M', 'M', '.'},
{'.', '.', '.', '.', '.', '.', '.', 'M', 'M', 'M', '.', '.', '.', 'M', 'M'},
{'.', '.', 'M', '.', '.', 'M', '.', 'O', 'M', '.', '.', 'M', '.', '.', '.'},
{'.', '.', '.', 'O', '.', '.', '.', 'M', '.', 'M', '.', 'M', 'M', 'M', '.'},
{'M', 'M', 'M', '.', 'M', '.', 'M', '.', 'M', '.', 'M', '.', 'M', '.', 'o'},
{'.', '.', 'M', '.', 'M', '.', 'M', '.', 'M', 'o', 'M', '.', '.', '.', '.'},
{'M', 'M', 'M', '.', '.', '.', 'M', 'M', 'M', '.', '.', '.', 'M', 'M', '.'},
{'.', 'o', '.', '.', '.', '.', '.', '.', 'M', '.', 'O', '.', 'M', '.', '.'},
{'M', 'M', 'M', 'M', 'M', 'M', 'M', '.', '.', '.', '.', 'O', 'M', '.', '.'},
{'.', '.', '.', 'M', '.', '.', '.', '.', '.', 'M', '.', 'M', 'M', 'O', '.'},
{'.', 'O', '.', 'M', '.', '.', '.', '.', '.', '.', '.', 'M', '.', '.', '.'},
{'F', '.', '.', '.', '.', 'M', 'M', 'M', '.', '.', '.', 'o', '.', 'M', '.'}
{'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
{'M', '.', '.', 'M', '.', '.', '.', '.', 'M', 'M', '.', '.', 'M', '.', 'M'},
{'M', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'M'},
{'M', '.', '.', '.', '.', 'M', 'M', 'M', '.', '.', '.', 'M', 'M', 'M', 'M'},
{'M', '.', '.', '.', '.', '.', '.', '.', 'M', 'M', '.', '.', 'F', 'M', 'M'},
{'M', '.', 'M', 'M', 'M', 'M', '.', '.', 'M', '.', '.', '.', '.', 'M', 'M'},
{'M', '.', 'M', '.', '.', '.', 'M', 'M', 'M', 'M', '.', 'M', 'M', 'M', 'M'},
{'M', '.', 'M', '.', 'M', '.', 'M', 'O', 'M', '.', '.', '.', 'M', '.', 'M'},
{'M', '.', '.', '.', 'M', '.', 'M', 'o', 'M', '.', 'M', '.', '.', '.', 'M'},
{'M', 'M', '.', 'M', '.', '.', 'M', 'o', 'M', '.', '.', '.', 'M', 'M', 'M'},
{'M', '.', '.', 'M', '.', '.', '.', 'O', '.', '.', '.', '.', '.', '.', 'M'},
{'M', '.', '.', 'M', 'M', 'M', '.', 'P', '.', '.', 'M', 'M', 'M', '.', 'M'},
{'M', 'M', '.', '.', '.', '.', '.', '.', '.', 'M', '.', 'M', '.', '.', 'M'},
{'M', '.', '.', 'M', '.', '.', '.', '.', '.', 'M', '.', '.', '.', 'M', 'M'},
{'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
};

// grilleNiveau2 = new char[][] {
Expand All @@ -68,30 +70,30 @@ public Terrain() {
// {'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
// };
grilleNiveau3 = new char[][] {
{'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
{'M', 'P', 'o', 'M', '.', 'K', 'M', '.', 'M', 'o', 'C', 'M', '.', 'B', 'M'},
{'M', 'M', 'F', '.', 'M', '.', 'M', 'o', 'M', '.', 'M', '.', 'M', 'M', 'M'},
{'M', 'o', '.', '.', 'o', '.', '.', '.', '.', '.', 'o', '.', '.', 'o', 'M'},
{'M', 'M', '.', '.', 'M', 'M', 'M', 'M', 'M', 'M', '.', 'M', 'M', 'M', 'M'},
{'M', 'F', 'o', '.', '.', 'M', 'B', 'O', 'M', '.', '.', '.', 'o', 'K', 'M'},
{'M', '.', 'M', 'M', 'M', '.', 'M', '.', 'M', '.', 'M', 'M', 'M', '.', 'M'},
{'M', 'o', 'M', '.', 'M', 'o', 'M', 'C', 'M', 'o', 'M', '.', 'M', 'o', 'M'},
{'M', '.', 'M', 'M', 'M', '.', 'M', '.', 'M', '.', 'M', 'M', 'M', '.', 'M'},
{'M', 'C', 'o', '.', '.', 'M', '.', 'M', 'M', '.', '.', '.', 'o', 'C', 'M'},
{'M', 'M', 'M', '.', 'M', 'M', 'M', 'M', 'M', 'M', '.', 'M', 'M', 'M', 'M'},
{'M', '.', '.', 'o', '.', 'M', '.', '.', '.', 'M', '.', 'o', '.', '.', 'M'},
{'M', 'o', 'M', 'M', '.', 'M', 'M', '.', 'M', 'M', '.', 'M', 'M', 'o', 'M'},
{'M', 'B', 'o', 'M', '.', 'o', 'M', '.', 'M', 'o', '.', 'M', 'o', 'F', 'M'},
{'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'}
{'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
{'M', '.', '.', 'M', '.', '.', '.', '.', 'M', 'M', '.', '.', 'M', '.', 'M'},
{'M', 'P', 'O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'M'},
{'M', 'O', '.', '.', '.', 'M', 'M', 'M', '.', '.', '.', 'M', 'M', 'M', 'M'},
{'M', '.', '.', '.', '.', '.', '.', '.', 'M', 'M', '.', '.', 'F', 'M', 'M'},
{'M', '.', 'M', 'M', 'M', 'M', '.', '.', 'M', '.', '.', '.', '.', 'M', 'M'},
{'M', '.', 'M', '.', '.', '.', 'M', 'M', 'M', 'M', '.', 'M', 'M', 'M', 'M'},
{'M', '.', 'M', '.', 'M', '.', 'M', '.', 'M', '.', '.', '.', 'M', '.', 'M'},
{'M', '.', '.', '.', 'M', '.', 'M', '.', 'M', '.', 'M', '.', '.', '.', 'M'},
{'M', 'M', '.', 'M', '.', '.', 'M', '.', 'M', '.', '.', '.', 'M', 'M', 'M'},
{'M', '.', '.', 'M', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'M'},
{'M', '.', '.', 'M', 'M', 'M', '.', '.', '.', '.', 'M', 'M', 'M', '.', 'M'},
{'M', 'M', '.', '.', '.', '.', '.', '.', '.', 'M', '.', 'M', '.', '.', 'M'},
{'M', '.', '.', 'M', '.', '.', '.', '.', '.', 'M', '.', '.', '.', 'M', 'M'},
{'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M', 'M'},
};
}

public char[][] getGrille() {
return this.grilleActuel;
}

public void setGrille(char[][] grille){
this.grilleActuel = grille;
public void setInitGrille(){
this.grilleActuel = this.grilleInit;
}

// Méthode pour changer la grille en fonction du niveau
Expand Down

0 comments on commit eea2cf4

Please sign in to comment.