From 65b6b114789ae3fffb4aaa4f7a084a87845503ed Mon Sep 17 00:00:00 2001 From: guezoloic Date: Sun, 12 May 2024 18:56:44 +0200 Subject: [PATCH] changement de plein de trucs (manque juste a regler un probleme) --- src/Characters/Mouvements.java | 16 ++-- src/Characters/Personnage.java | 41 ++++----- src/Environnement/Map.java | 35 +++++++- src/Main.java | 42 ++++----- src/display/Terminal.java | 131 ++++++++++++++++++++++++---- src/display/Test.java | 151 +++++++++++++++++++++++++++++++++ 6 files changed, 348 insertions(+), 68 deletions(-) create mode 100644 src/display/Test.java diff --git a/src/Characters/Mouvements.java b/src/Characters/Mouvements.java index 56b8748..a771c4f 100644 --- a/src/Characters/Mouvements.java +++ b/src/Characters/Mouvements.java @@ -22,17 +22,17 @@ public enum Mouvements { /** * GAUCHE prend comme coordonnée (1, 0) pour se déplacer. - * @param x = 1 - * @param y = 0 - */ - GAUCHE(1, 0), - - /** - * @DROITE prend comme coordonnée (-1, 0) pour se déplacer. * @param x = -1 * @param y = 0 */ - DROITE(-1, 0); + GAUCHE(-1, 0), + + /** + * @DROITE prend comme coordonnée (-1, 0) pour se déplacer. + * @param x = 1 + * @param y = 0 + */ + DROITE(1, 0); private final int deltaX; private final int deltaY; diff --git a/src/Characters/Personnage.java b/src/Characters/Personnage.java index 820df2e..c02f5f0 100644 --- a/src/Characters/Personnage.java +++ b/src/Characters/Personnage.java @@ -1,6 +1,7 @@ package Characters; import java.util.ArrayList; +import java.util.Arrays; import Objects.Snake; @@ -12,7 +13,6 @@ public class Personnage { private int n; private int round; private int size = 0; - private Snake head = Snake.HEAD; /** *

la liste de toute les coordonnées en fonction de N. Si N = 2, @@ -28,11 +28,10 @@ public class Personnage { *

le constructor definie un arrayList pour {@link #coordinate} * et defini n. * - * @param name est le nom du personnage. * @param n est une variable qui contient le nombre de tour avant * l'augmentation de taille. * @param coordinate est la variable qui contient les coordonnées - * qui sont placé par la suite dans {@link #coordinate}[0] + * qui sont placé par la suite dans {@link #coordinate}[0]} */ protected Personnage(int n, int[] coordinate) { this.coordinate = new ArrayList(); @@ -42,8 +41,8 @@ public class Personnage { } /** - * cette fonction retourne la premiere coordonnée de la liste {@link - * #coordinate} qui la tête du personnage + *

cette fonction retourne la premiere coordonnée de la liste + * {@link #coordinate} qui la tête du personnage. * @return la tête du personnage. */ public int[] getPrimaryCoordinate() { @@ -51,8 +50,8 @@ public class Personnage { } /** - * cette fonction retourne toute la liste {@link #coordinate} de - * coordonnée du serpent. + *

cette fonction retourne toute la liste + * {@link #coordinate} de coordonnée du serpent. * @return toute la liste des coordonnées du serpent */ public ArrayList getCoordinate() { @@ -95,12 +94,12 @@ public class Personnage { /** *

cette fonction est très pratique aggrandir le serpent * car elle garde la derniere coordonnée et si on la fusionne - * avec {@link #moveToLatestCoordinate()}, on peut l'utiliser + * avec {@link #increaseSnake()}, on peut l'utiliser * ajouter la coordonnée pour justement l'aggrandir. * @return garde la derniere coordonnée du serpent (sa queue) */ public int[] keepLatestCoordinate() { - return this.coordinate.get(getCoordinate().size()-1); + return this.coordinate.get(getCoordinate().size()-1).clone(); } /** @@ -109,7 +108,7 @@ public class Personnage { * le serpent. * @param coordinate ajout de la nouvelle coordonnée */ - public void moveToLatestCoordinate(int[] coordinate) { + public void increaseSnake(int[] coordinate) { this.coordinate.add(coordinate); } @@ -118,19 +117,23 @@ public class Personnage { * serpent. * @param mouvements le mouvement utilisé pour deplacer le serpent */ - protected void moveSnake(Mouvements mouvements) { - for (int[] coordinate : this.coordinate) { - mouvements.editCoordinate(coordinate); + public void moveSnake(Mouvements mouvements) { + mouvements.editCoordinate(this.coordinate.get(0)); + + for(int i = this.coordinate.size() - 1; i>1; i--) { + int[] value = this.coordinate.get(i-1); + this.coordinate.set(i, value); } } - public Mouvements getMouvement(int keys) { + public Mouvements getMouvement(Integer keys) { switch (keys) { - case 0x77: return Mouvements.HAUT; // w - case 0x73: return Mouvements.BAS; // s - case 0x61: return Mouvements.GAUCHE; // a - case 0x64: return Mouvements.DROITE; // d - default: return null; + case 0x77: return Mouvements.HAUT; // w + case 0x73: return Mouvements.BAS; // s + case 0x61: return Mouvements.GAUCHE; // a + case 0x64: return Mouvements.DROITE; // d + case null: return null; + default: return null; } } } diff --git a/src/Environnement/Map.java b/src/Environnement/Map.java index 3677c7c..2b9741c 100644 --- a/src/Environnement/Map.java +++ b/src/Environnement/Map.java @@ -110,15 +110,44 @@ public class Map { } } - public boolean isGamefinished(int key, Personnage personnage) { + public void placePersonnages(Personnage personnage) { + int index = 0; + + for (int[] coordinate : personnage.getCoordinate()) { + if (index == 0) { + this.grid[coordinate[1]][coordinate[0]] = Snake.HEAD; + + } else { + this.grid[coordinate[1]][coordinate[0]] = Snake.BODY; + } + index++; + } + } + + private boolean isGamefinishedImpassable(int key, Personnage personnage) { int[] coordinate = personnage.getMouvement(key).getCoordinate(); int[] playerCoordinate = personnage.getPrimaryCoordinate(); - int y = coordinate[1] + playerCoordinate[1]; int x = coordinate[0] + playerCoordinate[0]; - return this.getGrid()[y][x] instanceof Snake; + Object grid = this.getGrid()[y][x]; + + if (grid instanceof Snake) return ((Snake)grid).getEffects() == Effects.IMPASSABLE; + else if (grid instanceof Items) return ((Items)grid).getEffects() == Effects.IMPASSABLE; + else return false; + } + + public boolean isGameOver(int key, Personnage personnage) { + int[] personnageCoordinate = personnage.getPrimaryCoordinate(); + + boolean isOutX = personnageCoordinate[0] < 0 || this.grid[0].length - 1 < personnageCoordinate[0]; + boolean isOutY = personnageCoordinate[1] < 0 || this.grid.length - 1 < personnageCoordinate[1]; + + if (isOutX || isOutY) { + return true; + } + return false; } private void fillGrid() { diff --git a/src/Main.java b/src/Main.java index 2cf0210..3f41aca 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,31 +1,31 @@ -import Characters.*; +import Characters.Personnage; +import Characters.Players; import Display.Terminal; import Environnement.Map; public class Main { - // public static void print(Players player) { - // for (int[] coordinate : player.getCoordinate()) { - // for(int value : coordinate) { - // System.out.print(value + " "); - // } - // System.out.println(); - // } - // System.out.println(); - // } - public static void main(String[] args) { - // Players player = new Players(new int[] {-1, 1}, 2); + int N = 2; - // while(true) { - // int[] value = player.keepLatestCoordinate(); + Personnage[] personnages = new Personnage[] { + new Players(new int[] {1, 1}, N), + new Players(new int[] {8, 8}, N) + }; - // player.incrementRound(); - // print(player); - // if (player.isIncreaseSize()) player.moveToLatestCoordinate(value); + Map map = new Map(10, 10); + // map.addEdges(); + + Terminal.run(personnages, map, N); + + // Players player = new Players(new int[]{1, 1}, 1); + // player.increaseSnake(player.keepLatestCoordinate()); + // player.moveCoordinate(0x77); + + // for(int[] value : player.getCoordinate()) { + // for (int valueInt : value) { + // System.out.print(valueInt + " "); + // } + // System.out.println(); // } - - Terminal.run(new Personnage[]{ - new Players(new int[] {1, 1}, 2) - }, new Map(10, 10), 2); } } diff --git a/src/display/Terminal.java b/src/display/Terminal.java index e049a02..d7052e8 100644 --- a/src/display/Terminal.java +++ b/src/display/Terminal.java @@ -2,15 +2,63 @@ package Display; import java.util.Scanner; -import Characters.Mouvements; import Characters.Personnage; import Characters.Players; +import Characters.Robot; import Environnement.Map; import Objects.Fruits; import Objects.Items; import Objects.Snake; public class Terminal { + private static Scanner scanner = new Scanner(System.in); + private static String name = new String(); + + private static Map map; + + /** + *

Cette fonction est uniquement destiné pour la classe + * Players pour recuperer l'input dans le terminal. + * @param scanner + * @param player + * @return il retourne int qui est le char en ascii + */ + private static int getInput(Scanner scanner, Players player) { + String value = new String(); + Integer input = null; + + do { + value = scanner.nextLine(); + input = player.changeCoordinate(value); + }while(player.getMouvement(input) == null); + + return input.intValue(); + } + + /** + *

print toute la map. + * @param map + */ + private static void printMap(Map map) { + Object[][] mapObjects = map.getGrid(); + + for (int i = 0; i