mirror of
https://github.com/Cpt-Adok/SNAKE.git
synced 2026-01-25 14:34:07 +00:00
Ajout et correction
This commit is contained in:
|
Before Width: | Height: | Size: 1.5 MiB After Width: | Height: | Size: 1.5 MiB |
63
src/Characters/Mouvements.java
Normal file
63
src/Characters/Mouvements.java
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package Characters;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cet enumerateur prend en charge tout les mouvements possible
|
||||||
|
* pour le serpent, il a uniquement la possibilité de se déplacer
|
||||||
|
* grâce a {@link Mouvements} pour la classe Player et Robot.
|
||||||
|
*/
|
||||||
|
public enum Mouvements {
|
||||||
|
/**
|
||||||
|
* HAUT prend comme coordonnée (0, -1) pour se déplacer.
|
||||||
|
* @param x = 0
|
||||||
|
* @param y = -1
|
||||||
|
*/
|
||||||
|
HAUT(0, -1),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BAS prend comme coordonnée (0, 1) pour se déplacer.
|
||||||
|
* @param x = 0
|
||||||
|
* @param y = 1
|
||||||
|
*/
|
||||||
|
BAS(0, 1),
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
|
private final int deltaX;
|
||||||
|
private final int deltaY;
|
||||||
|
|
||||||
|
Mouvements(int deltaX, int deltaY) {
|
||||||
|
this.deltaX = deltaX;
|
||||||
|
this.deltaY = deltaY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cette fonction prend les coordonnées mis en paramètre et
|
||||||
|
* modifie avec les coordonnées de l'enum.
|
||||||
|
* @param coordinate prend principalement les coordonnées du
|
||||||
|
* personnage
|
||||||
|
*/
|
||||||
|
public void editCoordinate(int[] coordinate) {
|
||||||
|
coordinate[0] += this.deltaX;
|
||||||
|
coordinate[1] += this.deltaY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cette fonction retourne les coordonnées des valeurs du mouvement.
|
||||||
|
* @return la liste qui contient [0] = x et [1] = y
|
||||||
|
*/
|
||||||
|
public int[] getCoordinate() {
|
||||||
|
return new int[] {this.deltaX, this.deltaY};
|
||||||
|
}
|
||||||
|
}
|
||||||
136
src/Characters/Personnage.java
Normal file
136
src/Characters/Personnage.java
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
package Characters;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import Objects.Snake;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cet classe est la classe precurseur de tout les heritage pour creer un
|
||||||
|
* personnage jouable.
|
||||||
|
*/
|
||||||
|
public class Personnage {
|
||||||
|
private int n;
|
||||||
|
private int round;
|
||||||
|
private int size = 0;
|
||||||
|
private Snake head = Snake.HEAD;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> la liste de toute les coordonnées en fonction de N. Si N = 2,
|
||||||
|
* tout les deux tours, la taille du serpent augmente de 1. Si N = 3,
|
||||||
|
* tous les 3 tours, ça va augmenter de 1. On peut l'ecrire comme
|
||||||
|
* Round/N (les deux variables en int).
|
||||||
|
* <p> Le premier index est la coordonnée de la tête et les autres
|
||||||
|
* sont les coordonnées de son corps.
|
||||||
|
*/
|
||||||
|
private ArrayList<int[]> coordinate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> 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]
|
||||||
|
*/
|
||||||
|
protected Personnage(int n, int[] coordinate) {
|
||||||
|
this.coordinate = new ArrayList<int[]>();
|
||||||
|
this.coordinate.add(coordinate);
|
||||||
|
|
||||||
|
this.n = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
||||||
|
return coordinate.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction retourne toute la liste {@link #coordinate} de
|
||||||
|
* coordonnée du serpent.
|
||||||
|
* @return toute la liste des coordonnées du serpent
|
||||||
|
*/
|
||||||
|
public ArrayList<int[]> getCoordinate() {
|
||||||
|
return coordinate;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return augmente le tour après que le personnage a jouer
|
||||||
|
*/
|
||||||
|
public int incrementRound() {
|
||||||
|
return ++this.round;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return retourn un bool pour savoir si la taille s'est
|
||||||
|
* aggrandi
|
||||||
|
*/
|
||||||
|
public boolean isIncreaseSize() {
|
||||||
|
int size = this.round/n;
|
||||||
|
if (this.size < size) {
|
||||||
|
this.size = size;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return retourn un bool pour savoir si la taille s'est
|
||||||
|
* retreci.
|
||||||
|
*/
|
||||||
|
public boolean isDecreaseSize() {
|
||||||
|
int size = this.round/n;
|
||||||
|
if (this.size > size) {
|
||||||
|
this.size = size;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> 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
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> ajoute au dernier index, la valeur en parametre, très utile
|
||||||
|
* en combinant avec {@link #keepLatestCoordinate()} pour aggrandir
|
||||||
|
* le serpent.
|
||||||
|
* @param coordinate ajout de la nouvelle coordonnée
|
||||||
|
*/
|
||||||
|
public void moveToLatestCoordinate(int[] coordinate) {
|
||||||
|
this.coordinate.add(coordinate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> modifie toute la liste {@link #coordinate} pour deplacer tout le
|
||||||
|
* serpent.
|
||||||
|
* @param mouvements le mouvement utilisé pour deplacer le serpent
|
||||||
|
*/
|
||||||
|
protected void moveSnake(Mouvements mouvements) {
|
||||||
|
for (int[] coordinate : this.coordinate) {
|
||||||
|
mouvements.editCoordinate(coordinate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mouvements getMouvement(int 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/Characters/Players.java
Normal file
23
src/Characters/Players.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package Characters;
|
||||||
|
|
||||||
|
public class Players extends Personnage {
|
||||||
|
public Players(int[] coordinate, int n) {
|
||||||
|
super(n, coordinate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer changeCoordinate(String input) {
|
||||||
|
if (input.length() > 0) {
|
||||||
|
return (int)input.charAt(0);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean moveCoordinate(int keys) {
|
||||||
|
Mouvements value = getMouvement(keys);
|
||||||
|
if (value != null) {
|
||||||
|
moveSnake(value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/Characters/Robot.java
Normal file
9
src/Characters/Robot.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package Characters;
|
||||||
|
|
||||||
|
public class Robot extends Personnage {
|
||||||
|
|
||||||
|
protected Robot(int[] coordinate, int n) {
|
||||||
|
super(n, coordinate);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
package Connexion;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
145
src/Environnement/Map.java
Normal file
145
src/Environnement/Map.java
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
package Environnement;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import Characters.Personnage;
|
||||||
|
import Objects.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> cette classe est la classe qui cree et genere
|
||||||
|
* tout ce qui est important pour la Map du jeu
|
||||||
|
*/
|
||||||
|
public class Map {
|
||||||
|
/**
|
||||||
|
* <p> cette variable est toute la grille où se
|
||||||
|
* passe tout le jeu.
|
||||||
|
*/
|
||||||
|
private Object[][] grid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> cette variable recupere tout les objects stockés
|
||||||
|
*/
|
||||||
|
private ArrayList<Object> ObjectItems;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> cette variable recupere tout les coordonnées des
|
||||||
|
* objects stockés
|
||||||
|
*/
|
||||||
|
private ArrayList<int[]> coordinateItems;
|
||||||
|
|
||||||
|
private int longueur;
|
||||||
|
private int largeur;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> le constructeur crée une grille "vide" qui
|
||||||
|
* contient uniquement l'enumerateur Items.VOID
|
||||||
|
* avec la longueur et la largeur en paramètre.
|
||||||
|
*
|
||||||
|
* @param longueur pour la grille du jeu
|
||||||
|
* @param largeur pour la grille du jeu
|
||||||
|
*/
|
||||||
|
public Map(int longueur, int largeur) {
|
||||||
|
this.longueur = longueur;
|
||||||
|
this.largeur = largeur;
|
||||||
|
|
||||||
|
this.grid = new Object[this.longueur][this.largeur];
|
||||||
|
this.fillGrid();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> cette fonction clear toute la grille qui
|
||||||
|
* contient le jeu.
|
||||||
|
*/
|
||||||
|
public void clearMap() {
|
||||||
|
this.fillGrid();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> cette fonction renvoie toute la grille Object[][]
|
||||||
|
* qui contient tout la grille du jeu
|
||||||
|
* @return la grille du jeu
|
||||||
|
*/
|
||||||
|
public Object[][] getGrid() {
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> cette fonction ajoute les bordures sur la grille
|
||||||
|
*/
|
||||||
|
public void addEdges() {
|
||||||
|
for(int i = 0; i < this.grid.length; i++) {
|
||||||
|
for(int k = 0; k < this.grid[0].length; k++) {
|
||||||
|
if (i == 0 || i == this.grid.length - 1 || k == 0 || k == this.grid[0].length - 1) {
|
||||||
|
this.grid[i][k] = Items.WALL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> cette fonction ajoute dans {@link #grid} les
|
||||||
|
* objects contenu dans {@link #coordinateItems}
|
||||||
|
* et {@link #ObjectItems}.
|
||||||
|
* @param objects prend le type d'objets que vous voulez
|
||||||
|
* mettre dedans.
|
||||||
|
* @param number prend le nombre d'objets global que
|
||||||
|
* vous voulez mettre dedans.
|
||||||
|
*/
|
||||||
|
public void addObjects(Object[] objects, int number) {
|
||||||
|
int lengthObjects = objects.length-1;
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
for(int i = 0; i<lengthObjects; i++) {
|
||||||
|
int value = random.nextInt(number);
|
||||||
|
number -= value;
|
||||||
|
randomize(objects[i], value);
|
||||||
|
}
|
||||||
|
randomize(objects[lengthObjects], number);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> cette fonction place les objets dans la grille du
|
||||||
|
* jeu.
|
||||||
|
*/
|
||||||
|
public void placeObjects() {
|
||||||
|
for(int i = 0; i<this.coordinateItems.size(); i++) {
|
||||||
|
int[] coordinate = this.coordinateItems.get(i);
|
||||||
|
|
||||||
|
this.grid[coordinate[0]][coordinate[1]] = ObjectItems.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isGamefinished(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fillGrid() {
|
||||||
|
for(int i = 0; i < this.grid.length; i++) {
|
||||||
|
for(int k = 0; k < this.grid[0].length; k++) {
|
||||||
|
this.grid[i][k] = Items.VOID;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void randomize(Object item, int number) {
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
for(int i = 0; i<number; i++) {
|
||||||
|
int x = random.nextInt(1, this.grid[0].length);
|
||||||
|
int y = random.nextInt(1, this.grid.length);
|
||||||
|
|
||||||
|
if(!(getGrid()[y][x] instanceof Items)) {this.coordinateItems.add(new int[]{y, x}); this.ObjectItems.add(grid);}
|
||||||
|
else if(!(getGrid()[y][x] instanceof Snake)) {this.coordinateItems.add(new int[]{y, x}); this.ObjectItems.add(grid);}
|
||||||
|
else if(!(getGrid()[y][x] instanceof Fruits)) {this.coordinateItems.add(new int[]{y, x}); this.ObjectItems.add(grid);}
|
||||||
|
else i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,119 +0,0 @@
|
|||||||
package environnements;
|
|
||||||
|
|
||||||
import java.lang.Object;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import item.Effects;
|
|
||||||
import item.Items;
|
|
||||||
|
|
||||||
public class Map {
|
|
||||||
private Object[][] grid;
|
|
||||||
private Object[][] emptyGrid;
|
|
||||||
private Random random;
|
|
||||||
|
|
||||||
public int longueur;
|
|
||||||
public int largeur;
|
|
||||||
|
|
||||||
public Map(long longueur, long largeur) {
|
|
||||||
this.longueur = (int)longueur;
|
|
||||||
this.largeur = (int)largeur;
|
|
||||||
|
|
||||||
this.random = new Random();
|
|
||||||
|
|
||||||
this.grid = new Object[this.largeur][this.longueur];
|
|
||||||
this.emptyGrid = new Object[this.largeur][this.longueur];
|
|
||||||
|
|
||||||
this.fillGrid();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Items getCoordinate(int x, int y) {
|
|
||||||
if (x >= 0 && x < grid[0].length && y >= 0 && y < grid.length) {
|
|
||||||
return (Items)grid[y][x];
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Items getItems(int[] coordinate) {
|
|
||||||
return (Items)getGrid()[coordinate[1]][coordinate[0]];
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isGameOver(int[] coordinate) {
|
|
||||||
Items item = getCoordinate(coordinate[0], coordinate[1]);
|
|
||||||
|
|
||||||
if (item != null) {
|
|
||||||
Effects effect = item.getEffects();
|
|
||||||
return effect == Effects.IMPASSABLE;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getStringGrid() {
|
|
||||||
StringBuffer stringGrid = new StringBuffer();
|
|
||||||
|
|
||||||
for(Object[] listValue : this.grid) {
|
|
||||||
for(Object value : listValue) {
|
|
||||||
stringGrid.append(value);
|
|
||||||
}
|
|
||||||
stringGrid.append("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
return stringGrid.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addItems(Items[] items, int number) {
|
|
||||||
int lengthItems = (items.length-1);
|
|
||||||
for(int i = 0; i<lengthItems; i++) {
|
|
||||||
int value = this.random.nextInt(number);
|
|
||||||
number -= value;
|
|
||||||
randomize(items[i], value);
|
|
||||||
}
|
|
||||||
randomize(items[lengthItems], number);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object[][] getGrid() {
|
|
||||||
return grid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void cleanGrid() {
|
|
||||||
this.grid = this.emptyGrid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ajoutBordure() {
|
|
||||||
for(int i = 0; i < this.grid.length; i++) {
|
|
||||||
for(int k = 0; k < this.grid[0].length; k++) {
|
|
||||||
if (i == 0 || i == this.grid.length - 1 || k == 0 || k == this.grid[0].length - 1) {
|
|
||||||
this.grid[i][k] = Items.MUR;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void fillGrid() {
|
|
||||||
for(int i = 0; i < this.grid.length; i++) {
|
|
||||||
for(int k = 0; k < this.grid[0].length; k++) {
|
|
||||||
this.grid[i][k] = Items.VOID;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addCoordinate(int[] coordinate, Items items) {
|
|
||||||
for(int i = 0; i < this.grid.length; i++) {
|
|
||||||
for(int k = 0; k < this.grid[0].length; k++) {
|
|
||||||
if(i == coordinate[1] && k == coordinate[0]) {
|
|
||||||
this.grid[i][k] = items;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void randomize(Items item, int number) {
|
|
||||||
for(int i = 0; i<number; i++) {
|
|
||||||
int x = this.random.nextInt(1, this.grid[0].length);
|
|
||||||
int y = this.random.nextInt(1, this.grid.length);
|
|
||||||
|
|
||||||
if(!(getGrid()[y][x] instanceof Items)) this.grid[y][x] = item;
|
|
||||||
else i--;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package item;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Ceci est l'enumération où il y aura tout les effets disponible dans le projet.
|
|
||||||
*/
|
|
||||||
public enum Effects {
|
|
||||||
/**
|
|
||||||
* <p>pouvoir faire un dash de 4 ligne pendant le round prochain après
|
|
||||||
* la recupération.
|
|
||||||
*/
|
|
||||||
POWER,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> Modifie la valeur de N pendant 2 tours en le multipliant par 2
|
|
||||||
* <p> <strong>Exemple : </strong> si N = 2, il va être /2 donc N sera
|
|
||||||
* egal à 1 et le prochain tour quand N = 3, il sera égal à 1 (division
|
|
||||||
* d'entier).
|
|
||||||
*/
|
|
||||||
BOOST,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>pouvoir etre invincible pendant le prochain round.
|
|
||||||
*/
|
|
||||||
INVINCIBILITY,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> impossible à passer à travers.
|
|
||||||
*/
|
|
||||||
IMPASSABLE,
|
|
||||||
|
|
||||||
VOID;
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
package item;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cette enumération contient tout les items à effets disponnible dans le jeu.
|
|
||||||
*/
|
|
||||||
public enum Items {
|
|
||||||
MUR("mur", Effects.IMPASSABLE),
|
|
||||||
|
|
||||||
BODY("corps", Effects.IMPASSABLE),
|
|
||||||
HEAD("tete", Effects.IMPASSABLE),
|
|
||||||
|
|
||||||
VOID("void", Effects.VOID),
|
|
||||||
|
|
||||||
FRAISE("fraise", Effects.INVINCIBILITY),
|
|
||||||
ORANGE("orange", Effects.POWER),
|
|
||||||
BANANE("banane", Effects.BOOST);
|
|
||||||
|
|
||||||
private final String nom;
|
|
||||||
private final Effects effect;
|
|
||||||
|
|
||||||
Items(String nom, Effects effects) {
|
|
||||||
this.nom = nom;
|
|
||||||
this.effect = effects;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> type de variable pour recuperer le nom :
|
|
||||||
* <pre><code>String name = Item.FRAISE.getName()</code></pre>
|
|
||||||
* @return Avoir le nom de l'item
|
|
||||||
*/
|
|
||||||
public String getName() {
|
|
||||||
return this.nom;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> type de variable pour recuperer l'effet :
|
|
||||||
* <pre><code>Effects effect = Item.FRAISE.getEffects()</code></pre>
|
|
||||||
* @return Avoir l'effet de l'item
|
|
||||||
*/
|
|
||||||
public Effects getEffects() {
|
|
||||||
return effect;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,15 +1,31 @@
|
|||||||
import environnements.Map;
|
import Characters.*;
|
||||||
import personnages.Personnage;
|
import Display.Terminal;
|
||||||
import personnages.Player;
|
import Environnement.Map;
|
||||||
|
|
||||||
public class Main {
|
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) {
|
public static void main(String[] args) {
|
||||||
int n = 2;
|
// Players player = new Players(new int[] {-1, 1}, 2);
|
||||||
|
|
||||||
Personnage[] players = new Personnage[] {
|
// while(true) {
|
||||||
new Player("player1", n, new int[]{2, 2})
|
// int[] value = player.keepLatestCoordinate();
|
||||||
};
|
|
||||||
|
|
||||||
display.Terminal.run(players, new Map(10, 10), 2);
|
// player.incrementRound();
|
||||||
|
// print(player);
|
||||||
|
// if (player.isIncreaseSize()) player.moveToLatestCoordinate(value);
|
||||||
|
// }
|
||||||
|
|
||||||
|
Terminal.run(new Personnage[]{
|
||||||
|
new Players(new int[] {1, 1}, 2)
|
||||||
|
}, new Map(10, 10), 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
src/Objects/Effects.java
Normal file
6
src/Objects/Effects.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package Objects;
|
||||||
|
|
||||||
|
public enum Effects {
|
||||||
|
VOID,
|
||||||
|
IMPASSABLE;
|
||||||
|
}
|
||||||
31
src/Objects/Fruits.java
Normal file
31
src/Objects/Fruits.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package Objects;
|
||||||
|
|
||||||
|
public enum Fruits {
|
||||||
|
FRAISE("fraise", Effects.VOID);
|
||||||
|
|
||||||
|
private final String NAME;
|
||||||
|
private final Effects EFFECT;
|
||||||
|
|
||||||
|
Fruits(String name, Effects effect) {
|
||||||
|
this.NAME = name;
|
||||||
|
this.EFFECT = effect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> type de variable pour recuperer le nom des fruits:
|
||||||
|
* <pre><code>String name = Item.FRAISE.getName()</code></pre>
|
||||||
|
* @return Avoir le nom de l'item
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return this.NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> type de variable pour recuperer l'effet des fruits:
|
||||||
|
* <pre><code>Effects effect = Item.FRAISE.getEffects()</code></pre>
|
||||||
|
* @return Avoir l'effet de l'item
|
||||||
|
*/
|
||||||
|
public Effects getEffects() {
|
||||||
|
return this.EFFECT;
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/Objects/Items.java
Normal file
32
src/Objects/Items.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package Objects;
|
||||||
|
|
||||||
|
public enum Items {
|
||||||
|
WALL("WALL", Effects.IMPASSABLE),
|
||||||
|
VOID("VOID", Effects.VOID);
|
||||||
|
|
||||||
|
private final String NAME;
|
||||||
|
private final Effects EFFECT;
|
||||||
|
|
||||||
|
Items(String name, Effects effect) {
|
||||||
|
this.NAME = name;
|
||||||
|
this.EFFECT = effect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> type de variable pour recuperer le nom de l'item:
|
||||||
|
* <pre><code>String name = Item.WALL.getName()</code></pre>
|
||||||
|
* @return Avoir le nom de l'item
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return this.NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> type de variable pour recuperer l'effet de l'item:
|
||||||
|
* <pre><code>Effects effect = Item.WALL.getEffects()</code></pre>
|
||||||
|
* @return Avoir l'effet de l'item
|
||||||
|
*/
|
||||||
|
public Effects getEffects() {
|
||||||
|
return this.EFFECT;
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/Objects/Snake.java
Normal file
32
src/Objects/Snake.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package Objects;
|
||||||
|
|
||||||
|
public enum Snake {
|
||||||
|
HEAD("TETE", Effects.IMPASSABLE),
|
||||||
|
BODY("BODY", Effects.IMPASSABLE);
|
||||||
|
|
||||||
|
private final String NAME;
|
||||||
|
private final Effects EFFECT;
|
||||||
|
|
||||||
|
Snake(String name, Effects effect) {
|
||||||
|
this.NAME = name;
|
||||||
|
this.EFFECT = effect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> type de variable pour recuperer le nom du corps du snake:
|
||||||
|
* <pre><code>String name = Snake.HEAD.getName()</code></pre>
|
||||||
|
* @return Avoir le nom de l'item
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return this.NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> type de variable pour recuperer l'effet du corps du snake:
|
||||||
|
* <pre><code>Effects effect = Snake.HEAD.getEffects()</code></pre>
|
||||||
|
* @return Avoir l'effet de l'item
|
||||||
|
*/
|
||||||
|
public Effects getEffects() {
|
||||||
|
return this.EFFECT;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,63 +1,42 @@
|
|||||||
package display;
|
package Display;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import environnements.Map;
|
import Characters.Mouvements;
|
||||||
import item.Items;
|
import Characters.Personnage;
|
||||||
import personnages.Personnage;
|
import Characters.Players;
|
||||||
import personnages.Player;
|
import Environnement.Map;
|
||||||
|
import Objects.Fruits;
|
||||||
|
import Objects.Items;
|
||||||
|
import Objects.Snake;
|
||||||
|
|
||||||
public class Terminal {
|
public class Terminal {
|
||||||
|
/**
|
||||||
|
* cette fonction clear le terminal et le remet en
|
||||||
|
* haut a gauche de la ligne.
|
||||||
|
*/
|
||||||
private static void clearTerminal() {
|
private static void clearTerminal() {
|
||||||
System.out.println("\u001b[2J \u001b[H");
|
System.out.println("\u001b[2J \u001b[H");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void showMap(Object[][] grid) {
|
private static int getInput(Scanner scanner, Players player) {
|
||||||
for (int i = 0; i<grid.length; i++) {
|
|
||||||
for(int k = 0; k<grid[0].length; k++) {
|
|
||||||
System.out.print(((Items)grid[i][k]).getName() + " ");
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void getInput(Scanner scanner, Player player) {
|
|
||||||
String value = new String();
|
String value = new String();
|
||||||
|
int input = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
value = scanner.nextLine();
|
value = scanner.nextLine();
|
||||||
} while(!player.changeCoordinate(value));
|
} while(player.getMouvement(input = player.changeCoordinate(value)) == null);
|
||||||
|
|
||||||
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isGameOver(Personnage[] personnages, Map map) {
|
private static void printMap(Map map) {
|
||||||
boolean gameover = false;
|
Object[][] mapObjects = map.getGrid();
|
||||||
for(Personnage personnage : personnages) {
|
|
||||||
if (gameover = map.isGameOver(personnage.getCoordinate())) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return gameover;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void run(Personnage[] personnages, Map map, long n) {
|
public static void run(Personnage[] personnages, Map map, int n) {
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
printMap(map);
|
||||||
for(Personnage personnage : personnages) {
|
scanner.close();
|
||||||
map.addCoordinate(personnage.getCoordinate(), Items.HEAD);
|
|
||||||
}
|
|
||||||
|
|
||||||
showMap(map.getGrid());
|
|
||||||
|
|
||||||
System.out.println(map.isGameOver(personnages[0].getCoordinate()));
|
|
||||||
|
|
||||||
while (!isGameOver(personnages, map)) {
|
|
||||||
for(Personnage personnage : personnages) {
|
|
||||||
getInput(scanner, (Player)personnage);
|
|
||||||
map.addCoordinate(((Player)personnage).getCoordinate(), Items.HEAD);
|
|
||||||
}
|
|
||||||
|
|
||||||
map.cleanGrid();
|
|
||||||
clearTerminal();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
package personnages;
|
|
||||||
|
|
||||||
enum Mouvement {
|
|
||||||
DROITE(1, 0),
|
|
||||||
HAUT(0, -1),
|
|
||||||
BAS(0, 1),
|
|
||||||
GAUCHE(-1, 0);
|
|
||||||
|
|
||||||
private final int deltaX;
|
|
||||||
private final int deltaY;
|
|
||||||
|
|
||||||
Mouvement(int deltaX, int deltaY) {
|
|
||||||
this.deltaX = deltaX;
|
|
||||||
this.deltaY = deltaY;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void editCoordinate(int[] coordinate) {
|
|
||||||
coordinate[0] += this.deltaX;
|
|
||||||
coordinate[1] += this.deltaY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
package personnages;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import item.Effects;
|
|
||||||
import item.Items;
|
|
||||||
|
|
||||||
public class Personnage {
|
|
||||||
/**
|
|
||||||
* <p> taille du serpent.
|
|
||||||
*/
|
|
||||||
private int size;
|
|
||||||
private int n; // N
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> une variable pour changer le round pour chaque fois
|
|
||||||
* que la manche est fini seulement pour le personnage.
|
|
||||||
*/
|
|
||||||
private int round;
|
|
||||||
|
|
||||||
private String nom;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> la liste de toute les coordonnées en fonction de N. Si N = 2,
|
|
||||||
* tout les deux tours, la taille du serpent augmente de 1. Si N = 3,
|
|
||||||
* tous les 3 tours, ça va augmenter de 1. On peut l'ecrire comme Round/N
|
|
||||||
* (les deux variables en int).
|
|
||||||
* <p> Le premier index est la coordonnée de la tête et les autres
|
|
||||||
* sont les coordonnées de son corps.
|
|
||||||
*/
|
|
||||||
protected ArrayList<int[]> coordinate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> 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]
|
|
||||||
*/
|
|
||||||
protected Personnage(String name, int n, int[] coordinate) {
|
|
||||||
this.coordinate = new ArrayList<int[]>();
|
|
||||||
this.coordinate.add(coordinate);
|
|
||||||
|
|
||||||
this.n = n;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return nom;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int incrementRound() {
|
|
||||||
return ++this.round;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int[] getCoordinate() {
|
|
||||||
return coordinate.get(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package personnages;
|
|
||||||
|
|
||||||
import static java.util.Arrays.toString;
|
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>la classe Player est la classe principale pour l'utilisation de toute
|
|
||||||
* input et output que l'utilisateur peut faire.
|
|
||||||
*/
|
|
||||||
public class Player extends Personnage {
|
|
||||||
/**
|
|
||||||
* <p>le constructor definie les coordonnées de la tête et defini n.
|
|
||||||
*
|
|
||||||
* @param n est une variable qui contient le nombre detour avant
|
|
||||||
* l'augmentation de taille.
|
|
||||||
* @param coordinate est un array de <strong>2 entiers</strong> qui
|
|
||||||
* est représenté comme <strong>{x, y}</strong> et qui represente les
|
|
||||||
* coordonnées de la tête du personnage.
|
|
||||||
*/
|
|
||||||
public Player(String name, int n, int[] coordinate) {
|
|
||||||
super(name, n, coordinate);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean changeCoordinate(String input) {
|
|
||||||
if (input.length() > 0) {
|
|
||||||
return moveCoordinate(input.charAt(0));
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean moveCoordinate(int keys) {
|
|
||||||
switch (keys) {
|
|
||||||
case 0x77: Mouvement.HAUT.editCoordinate(getCoordinate()); break; // w
|
|
||||||
case 0x73: Mouvement.BAS.editCoordinate(getCoordinate()); break; // s
|
|
||||||
case 0x61: Mouvement.GAUCHE.editCoordinate(getCoordinate()); break; // a
|
|
||||||
case 0x64: Mouvement.DROITE.editCoordinate(getCoordinate()); break; // d
|
|
||||||
default: return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package personnages;
|
|
||||||
|
|
||||||
public class Robot extends Personnage {
|
|
||||||
public Robot(String name, int size, int[] coordinate) {
|
|
||||||
super(name, size, coordinate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user