mirror of
https://github.com/Cpt-Adok/SNAKE.git
synced 2026-01-25 10:34:06 +00:00
correction de problene et channel terminé
This commit is contained in:
4
Makefile
4
Makefile
@@ -1,6 +1,6 @@
|
|||||||
# Paramètres
|
# Paramètres
|
||||||
.PHONY: all clean run
|
.PHONY: all clean run
|
||||||
.SILENT: clean run
|
.SILENT: run clean
|
||||||
|
|
||||||
# variables
|
# variables
|
||||||
JAVAC = javac
|
JAVAC = javac
|
||||||
@@ -14,7 +14,7 @@ LIB_DIR = lib
|
|||||||
JAR = $(LIB_DIR)/*
|
JAR = $(LIB_DIR)/*
|
||||||
|
|
||||||
# main
|
# main
|
||||||
all: clean $(MAIN_FILE) run clean
|
all: clean $(MAIN_FILE) run
|
||||||
|
|
||||||
$(MAIN_FILE) : $(BIN_DIR)/$(MAIN_FILE).class
|
$(MAIN_FILE) : $(BIN_DIR)/$(MAIN_FILE).class
|
||||||
|
|
||||||
|
|||||||
4
run.bat
4
run.bat
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
set "error_file=error.txt"
|
set "error_file=error.txt"
|
||||||
|
|
||||||
chcp 65001
|
REM ceci est pour mettre un batch en utf-8 car sinon, nous aurons des problèmes avec les caractères.
|
||||||
|
chcp 65001
|
||||||
|
|
||||||
make 2> %error_file%
|
make 2> %error_file%
|
||||||
|
|
||||||
for %%A in ("%error_file%") do set "errror_size=%%~zA"
|
for %%A in ("%error_file%") do set "errror_size=%%~zA"
|
||||||
|
|||||||
@@ -1,155 +0,0 @@
|
|||||||
package Characters;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import Objects.Snake;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Cet classe est la classe precurseur de tout les heritage pour creer un
|
|
||||||
* personnage jouable.
|
|
||||||
*/
|
|
||||||
public class Personnage {
|
|
||||||
public static int n;
|
|
||||||
private int round;
|
|
||||||
private int size = 0;
|
|
||||||
private String name;
|
|
||||||
private String effect;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <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 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[] coordinate) {
|
|
||||||
this.coordinate = new ArrayList<int[]>();
|
|
||||||
this.coordinate.add(coordinate);
|
|
||||||
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> 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 #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).clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <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 increaseSnake(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
|
|
||||||
*/
|
|
||||||
public void moveSnake(Mouvements mouvements) {
|
|
||||||
|
|
||||||
int[] oldHeadPosition = getPrimaryCoordinate().clone();
|
|
||||||
|
|
||||||
for (int i = this.coordinate.size() - 1; i > 0; i--) {
|
|
||||||
this.coordinate.set(i, this.coordinate.get(i - 1).clone());
|
|
||||||
}
|
|
||||||
mouvements.editCoordinate(oldHeadPosition);
|
|
||||||
this.coordinate.set(0, oldHeadPosition);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSize() {
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getEffects(Object item) {
|
|
||||||
return new String();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Mouvements getMouvement(Integer keys) {
|
|
||||||
switch (keys) {
|
|
||||||
case 0x77: case 0x7A: return Mouvements.HAUT; // w ou z
|
|
||||||
case 0x73: return Mouvements.BAS; // s
|
|
||||||
case 0x61: case 0x71: return Mouvements.GAUCHE; // a
|
|
||||||
case 0x64: return Mouvements.DROITE; // d
|
|
||||||
case null: return null;
|
|
||||||
default: return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package Characters;
|
|
||||||
|
|
||||||
public class Players extends Personnage {
|
|
||||||
public Players(String name, int[] coordinate) {
|
|
||||||
super(name, 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,136 +0,0 @@
|
|||||||
package Characters;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import javax.swing.JOptionPane;
|
|
||||||
|
|
||||||
import Environnement.*;
|
|
||||||
import Objects.Effects;
|
|
||||||
|
|
||||||
public class Robot extends Personnage {
|
|
||||||
|
|
||||||
Map m;
|
|
||||||
Mouvements move;
|
|
||||||
|
|
||||||
public Robot(String name, int[] coordinate,Map m) {
|
|
||||||
super(name, coordinate);
|
|
||||||
this.m=m;
|
|
||||||
move=this.compare(this.getCoordinate().get(0), this.choix().get(0));
|
|
||||||
jouer(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String jouer(Map m){
|
|
||||||
if (this.move==Mouvements.HAUT){
|
|
||||||
return "U";
|
|
||||||
}else if(this.move==Mouvements.BAS){
|
|
||||||
return "D";
|
|
||||||
}else if(this.move==Mouvements.GAUCHE){
|
|
||||||
return "L";
|
|
||||||
}else if(this.move==Mouvements.DROITE){
|
|
||||||
return "R";
|
|
||||||
}
|
|
||||||
return "Problème";
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean estPossible(int x,int y){
|
|
||||||
JOptionPane.showInputDialog((this.m.getGrid().length+" "+ this.m.getGrid()[0].length).toString());
|
|
||||||
Object [][] grille=new Object[][] {this.m.getGrid()};
|
|
||||||
if (grille[x][y]==Effects.IMPASSABLE){
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int [] creerTab(int x,int y){
|
|
||||||
int [] t=new int [] {x,y};
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<int []> coupsPossibles(int [] co) {
|
|
||||||
ArrayList<int []> coupsValables=new ArrayList<int []> ();
|
|
||||||
if (this.estPossible(co[0]+1,co[1])){
|
|
||||||
coupsValables.add(creerTab(co[0]+1, co[1]));
|
|
||||||
}else if (this.estPossible(co[0],co[1]+1)){
|
|
||||||
coupsValables.add(creerTab(co[0], co[1]+1));
|
|
||||||
}else if (this.estPossible(co[0]-1,co[1])){
|
|
||||||
coupsValables.add(creerTab(co[0]-1, co[1]));
|
|
||||||
}else if (this.estPossible(co[0],co[1]-1)){
|
|
||||||
coupsValables.add(creerTab(co[0], co[1]-1));
|
|
||||||
}
|
|
||||||
return coupsValables;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList <int []> casesAutour(){
|
|
||||||
ArrayList <int []> t =this.coupsPossibles(this.getCoordinate().get(0));
|
|
||||||
ArrayList <int []> t2 = new ArrayList<> ();
|
|
||||||
for (int i=0;i<t.size();i++){
|
|
||||||
t.get(i)[0]+=1;
|
|
||||||
this.fusion(t2,this.coupsPossibles(t.get(i)));
|
|
||||||
t.get(i)[0]-=2;
|
|
||||||
this.fusion(t2,this.coupsPossibles(t.get(i)));
|
|
||||||
t.get(i)[0]+=1;
|
|
||||||
t.get(i)[1]+=1;
|
|
||||||
this.fusion(t2,this.coupsPossibles(t.get(i)));
|
|
||||||
t.get(i)[1]-=2;
|
|
||||||
this.fusion(t2,this.coupsPossibles(t.get(i)));
|
|
||||||
}
|
|
||||||
this.killDouble(t2);
|
|
||||||
return t2;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList<int[]> fusion(ArrayList<int[]> t, ArrayList<int[]> t2){
|
|
||||||
for (int [] e :t2){
|
|
||||||
t.add(e);
|
|
||||||
}
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList <int []> choix(){
|
|
||||||
ArrayList <int[]> cases=casesAutour();
|
|
||||||
ArrayList <ArrayList <int []>> w=new ArrayList<>();
|
|
||||||
for (int i=0;i<cases.size();i++){
|
|
||||||
w.add(this.coupsPossibles(casesAutour().get(i)));
|
|
||||||
}
|
|
||||||
ArrayList<int []> max=w.get(0);
|
|
||||||
for (ArrayList <int []> e :w){
|
|
||||||
if (e.size()>max.size()){
|
|
||||||
max=e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return max;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Mouvements compare(int[] t,int[] t2){
|
|
||||||
if (t[0]>t2[0]){
|
|
||||||
System.out.println("s");
|
|
||||||
return Mouvements.BAS;
|
|
||||||
}else if (t[0]<t2[0]){
|
|
||||||
System.out.println("w");
|
|
||||||
return Mouvements.HAUT;
|
|
||||||
}else if (t[1]<t2[1]){
|
|
||||||
System.out.println("a");
|
|
||||||
return Mouvements.GAUCHE;
|
|
||||||
}else if (t[1]>t2[1]){
|
|
||||||
System.out.println("d");
|
|
||||||
return Mouvements.DROITE;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList <int []> killDouble(ArrayList <int []> t){
|
|
||||||
for (int i=0;i<t.size();i++){
|
|
||||||
for (int j=i;j<t.size();j++){
|
|
||||||
if (t.get(i)==t.get(j)){
|
|
||||||
t.remove(j);
|
|
||||||
}else if(t.get(i)==this.getCoordinate().get(0)){
|
|
||||||
t.remove(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
// public int [] focusTete(){
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,63 +1,47 @@
|
|||||||
package Connexion;
|
package Connexion;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import environnements.*;
|
||||||
|
import object.Mouvements;
|
||||||
|
import personnages.Personnage;
|
||||||
|
|
||||||
import javax.swing.JOptionPane;
|
public class Channel extends Personnage{
|
||||||
|
private Reseau reseau;
|
||||||
|
|
||||||
import Environnement.*;
|
public Channel(Object[][] map, String channel) {
|
||||||
import Characters.*;
|
super(new int [] {19,19});
|
||||||
import display.TerminalChannel;
|
reseau=new Reseau(channel);
|
||||||
|
|
||||||
public class Channel{
|
|
||||||
|
|
||||||
Reseau reseau;
|
|
||||||
ArrayList<Personnage> personnages;
|
|
||||||
int numJoueur;
|
|
||||||
TerminalChannel term;
|
|
||||||
Map m;
|
|
||||||
|
|
||||||
public Channel(Reseau r, ArrayList<Personnage> p, int j,TerminalChannel term,Map m){
|
|
||||||
this.reseau=r;
|
|
||||||
this.personnages=p;
|
|
||||||
this.numJoueur=j;
|
|
||||||
this.term=term;
|
|
||||||
this.m=m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String typePersonnage(int i){
|
public void envoyerMessage(String s) {
|
||||||
if (i>=0 && i<personnages.size()){
|
reseau.sendContent(s);
|
||||||
Personnage p=personnages.get(i);
|
}
|
||||||
if (p instanceof Characters.Players){
|
|
||||||
return "J";
|
public String recupererMessage() {
|
||||||
}
|
return reseau.getLastedContent();
|
||||||
return "R";
|
}
|
||||||
|
|
||||||
|
public Mouvements conversion(String s){
|
||||||
|
if (s.equals("U") || s.equals("u")){
|
||||||
|
return Mouvements.HAUT;
|
||||||
|
}else if (s.equals("D") || s.equals("d")){
|
||||||
|
return Mouvements.BAS;
|
||||||
|
}else if (s.equals("L") || s.equals("l")){
|
||||||
|
return Mouvements.GAUCHE;
|
||||||
|
}else if (s.equals("R") || s.equals("r")){
|
||||||
|
return Mouvements.DROITE;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void jeu(){
|
// @Override
|
||||||
String j1=this.typePersonnage(0);
|
public boolean round(Map map){
|
||||||
Players player=(Players) this.personnages.get(0);
|
int [] coordinate=this.getHeadCoordinate();
|
||||||
while (term.run()){
|
this.moveSnake(conversion(recupererMessage()));
|
||||||
if (term.round%2!=0){
|
if (map.isGameOver(coordinate) || this.applyEffects(map.getEffect(coordinate))){
|
||||||
term.playerRound(player);
|
return true;
|
||||||
}else{
|
|
||||||
reseau.sendContent(this.jouer(j1));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
map.deleteItems(coordinate);
|
||||||
|
this.increaseRound();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public String jouer(String j){
|
|
||||||
if (j=="J"){
|
|
||||||
Players player=(Players) this.personnages.get(0);
|
|
||||||
return this.getInput(reseau);
|
|
||||||
}
|
|
||||||
Robot bot=(Robot) this.personnages.get(0);
|
|
||||||
return bot.jouer(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getInput(Reseau r){
|
|
||||||
String c=" ";
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,187 +0,0 @@
|
|||||||
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.ObjectItems = new ArrayList<>();
|
|
||||||
this.coordinateItems = new ArrayList<>();
|
|
||||||
|
|
||||||
this.fillGrid();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> cette fonction clear toute la grille qui
|
|
||||||
* contient le jeu.
|
|
||||||
*/
|
|
||||||
public void clearMap(boolean edges) {
|
|
||||||
this.fillGrid();
|
|
||||||
if(edges) this.addEdges();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <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);
|
|
||||||
placeObjects();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <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 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(Personnage personnage) {
|
|
||||||
int[] playerCoordinate = personnage.getPrimaryCoordinate();
|
|
||||||
|
|
||||||
int y = playerCoordinate[1];
|
|
||||||
int x = playerCoordinate[0];
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
System.out.println(isGamefinishedImpassable(personnage));
|
|
||||||
return isGamefinishedImpassable(personnage);
|
|
||||||
}
|
|
||||||
|
|
||||||
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 object, int number) {
|
|
||||||
Random random = new Random();
|
|
||||||
|
|
||||||
for (int i = 0; i<number; i++) {
|
|
||||||
int x = random.nextInt(this.grid[0].length);
|
|
||||||
int y = random.nextInt(this.grid.length);
|
|
||||||
|
|
||||||
if(!((getGrid()[y][x] instanceof Snake) || (getGrid()[y][x] instanceof Fruits))) {
|
|
||||||
if ((Items)getGrid()[y][x] == Items.VOID) {
|
|
||||||
this.coordinateItems.add(new int[] {x, y});
|
|
||||||
this.ObjectItems.add(object);
|
|
||||||
} else {
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +1,22 @@
|
|||||||
import Characters.Personnage;
|
import environnements.Map;
|
||||||
import Characters.Players;
|
import game.Terminal;
|
||||||
import display.Terminal;
|
import object.Items;
|
||||||
import Environnement.Map;
|
import personnages.Personnage;
|
||||||
|
import personnages.Player;
|
||||||
|
import tests.*;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Personnage.n = 4;
|
Personnage.n = 2;
|
||||||
|
|
||||||
Map map = new Map(30, 30);
|
|
||||||
|
|
||||||
Personnage[] personnages = new Personnage[] {
|
Personnage[] personnages = new Personnage[] {
|
||||||
new Players("Phillipe", new int[] {1, 1}),
|
new Player(new int[] {0, 0}, "Philippe Etchebest"),
|
||||||
new Players("Edouard", new int[] {28, 28})
|
new Player(new int[] {19, 19}, "Luke Skywalker")
|
||||||
};
|
};
|
||||||
|
|
||||||
Terminal.edges = true;
|
Map map = new Map(20, 20);
|
||||||
|
map.addObjectsRandomize(new Object[] {Items.FRAISE}, 2);
|
||||||
|
|
||||||
new Terminal(map, personnages);
|
new Terminal(map, personnages);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
package Objects;
|
|
||||||
|
|
||||||
public enum Effects {
|
|
||||||
VOID,
|
|
||||||
IMPASSABLE;
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
150
src/display/Display.java
Normal file
150
src/display/Display.java
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
package display;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import object.*;
|
||||||
|
import personnages.*;
|
||||||
|
|
||||||
|
public class Display {
|
||||||
|
/**
|
||||||
|
* cette fonction clear le terminal et le remet en
|
||||||
|
* haut a gauche de la ligne.
|
||||||
|
*/
|
||||||
|
public static void clearTerminal() {
|
||||||
|
System.out.println("\u001b[2J \u001b[H");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction dessine la grille avec des noms
|
||||||
|
* pour chaque items.
|
||||||
|
*/
|
||||||
|
public static void printMap(Object[][] map) {
|
||||||
|
for (Object[] object : map) {
|
||||||
|
for (Object value : object) {
|
||||||
|
System.out.print(value.toString() + " ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction print le snake dans le terminal avec des
|
||||||
|
* caracteres speciaux, (du utf-8).
|
||||||
|
* @param map
|
||||||
|
* @param personnages
|
||||||
|
*/
|
||||||
|
public static void printMap(Object[][] map, Personnage[] personnages) {
|
||||||
|
for (int i = 0; i<map.length; i++) {
|
||||||
|
for(int k = 0; k<map[0].length; k++) {
|
||||||
|
Object object = map[i][k];
|
||||||
|
|
||||||
|
if(object instanceof Items) printItems((Items)object, map, k, i);
|
||||||
|
if(object instanceof Snake) printSnake((Snake)object, personnages, k, i);
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printInformation(int round, int playerIndex, Personnage personnage) {
|
||||||
|
int[] coordinate = personnage.getHeadCoordinate();
|
||||||
|
|
||||||
|
System.out.println(
|
||||||
|
"Round : " + round + " | N : " + Personnage.n +
|
||||||
|
"\n Joueur " + (playerIndex+1) + " : " + personnage.getName() + " (" +
|
||||||
|
coordinate[0]+", "+ coordinate[1] +") | size : " + personnage.getSize()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printSnake(Snake item, Personnage[] personnages, int x, int y) {
|
||||||
|
switch (item) {
|
||||||
|
case BODY: System.out.print(" \u25A1 "); break;
|
||||||
|
case HEAD: printHead(personnages, x, y); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printItems(Items item, Object[][] map, int x, int y) {
|
||||||
|
switch (item) {
|
||||||
|
case FRAISE: System.out.print(" \uF353 "); break;
|
||||||
|
|
||||||
|
case WALL: printWall(map, x, y); break;
|
||||||
|
case VOID: System.out.print(" "); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void printWall(Object[][] map, int x, int y) {
|
||||||
|
// TOP BOT LEFT RIGHT
|
||||||
|
boolean[] position = new boolean[] {
|
||||||
|
(y > 0) ? map[y - 1][x] == Items.WALL : false,
|
||||||
|
(y < map.length - 1) ? map[y+1][x] == Items.WALL : false,
|
||||||
|
(x > 0) ? map[y][x - 1] == Items.WALL : false,
|
||||||
|
(x < map[y].length - 1) ? map[y][x + 1] == Items.WALL : false,
|
||||||
|
};
|
||||||
|
|
||||||
|
System.out.print(whichWall(position));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String whichWall(boolean[] isWall) {
|
||||||
|
String positionWall = new String();
|
||||||
|
|
||||||
|
if (isWall[0] && isWall[1] && isWall[2] && isWall[3]) positionWall = "\u2550\u256C\u2550";
|
||||||
|
|
||||||
|
else if (isWall[0] && isWall[1] && isWall[3]) positionWall = "\u2560";
|
||||||
|
else if (isWall[0] && isWall[1] && isWall[2]) positionWall = "\u2563";
|
||||||
|
else if (isWall[0] && isWall[2] && isWall[3]) positionWall = "\u2550\u2569\u2550";
|
||||||
|
else if (isWall[1] && isWall[2] && isWall[3]) positionWall = "\u2550\u2566\u2550";
|
||||||
|
|
||||||
|
else if (isWall[0] && isWall[1]) positionWall = "\u2551";
|
||||||
|
else if (isWall[2] && isWall[3]) positionWall = "\u2550\u2550\u2550";
|
||||||
|
else if (isWall[0] && isWall[2]) positionWall = "\u255D ";
|
||||||
|
else if (isWall[0] && isWall[3]) positionWall = "\u255A";
|
||||||
|
else if (isWall[1] && isWall[2]) positionWall = "\u2557 ";
|
||||||
|
else if (isWall[1] && isWall[3]) positionWall = "\u2554";
|
||||||
|
|
||||||
|
else positionWall = "\u2550\u256C\u2550";
|
||||||
|
|
||||||
|
return positionWall;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void printHead(Personnage[] personnages, int x, int y) {
|
||||||
|
Personnage personnage = searchSnake(personnages, x, y);
|
||||||
|
|
||||||
|
if (personnage != null) {
|
||||||
|
ArrayList<int[]> personnageCoordinate = personnage.getCoordinate();
|
||||||
|
|
||||||
|
int[] primaryCoordinate = personnage.getHeadCoordinate();
|
||||||
|
|
||||||
|
if (personnageCoordinate.size() > 1) {
|
||||||
|
int[] secondCoordinate = personnageCoordinate.get(1);
|
||||||
|
|
||||||
|
// UP DOWN LEFT RIGHT
|
||||||
|
boolean[] isHead = new boolean[] {
|
||||||
|
primaryCoordinate[0] == secondCoordinate[0] && primaryCoordinate[1] > secondCoordinate[1], // UP
|
||||||
|
primaryCoordinate[0] == secondCoordinate[0] && primaryCoordinate[1] < secondCoordinate[1], // DOWN
|
||||||
|
primaryCoordinate[1] == secondCoordinate[1] && primaryCoordinate[0] > secondCoordinate[0], // LEFT
|
||||||
|
primaryCoordinate[1] == secondCoordinate[1] && primaryCoordinate[0] < secondCoordinate[0] // RIGHT
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isHead[0]) {System.out.print(" \u21E9 ");return;}
|
||||||
|
else if (isHead[1]) {System.out.print(" \u21E7 ");return;}
|
||||||
|
else if (isHead[2]) {System.out.print(" \u21E8");return;}
|
||||||
|
else if (isHead[3]) {System.out.print("\u21E6 ");return;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.print(" \u25CF ");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Personnage searchSnake(Personnage[] personnages, int x, int y) {
|
||||||
|
for (Personnage personnage : personnages) {
|
||||||
|
int[] primaryCoordinate = personnage.getHeadCoordinate();
|
||||||
|
|
||||||
|
int primaryY = primaryCoordinate[1] + 1;
|
||||||
|
int primaryX = primaryCoordinate[0] + 1;
|
||||||
|
|
||||||
|
if (primaryX == x && primaryY == y) {
|
||||||
|
return personnage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
package display;
|
|
||||||
|
|
||||||
import java.io.IOError;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
import Characters.*;
|
|
||||||
import Environnement.*;
|
|
||||||
import Objects.*;
|
|
||||||
|
|
||||||
public class Terminal {
|
|
||||||
private static Scanner scanner;
|
|
||||||
private static Map map;
|
|
||||||
private static Personnage[] personnages;
|
|
||||||
public int round = 0;
|
|
||||||
|
|
||||||
public static boolean edges = false;
|
|
||||||
|
|
||||||
public Terminal(Map m, Personnage[] personnage) {
|
|
||||||
scanner = new Scanner(System.in);
|
|
||||||
personnages = personnage;
|
|
||||||
map = m;
|
|
||||||
|
|
||||||
run();
|
|
||||||
if (edges) map.addEdges();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void placePersonnages(Personnage[] personnages) {
|
|
||||||
for(Personnage personnage : personnages) {
|
|
||||||
map.placePersonnages(personnage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean playerRound(Players player) {
|
|
||||||
TerminalDisplay.printMap(map, personnages);
|
|
||||||
// TerminalDisplay.printMapType(map);
|
|
||||||
|
|
||||||
int[] latestCoordinate = player.keepLatestCoordinate();
|
|
||||||
int input = getInput(scanner, player);
|
|
||||||
player.moveCoordinate(input);
|
|
||||||
|
|
||||||
if(map.isGameOver(input, player)) {TerminalDisplay.clearTerminal(); System.out.println("GameOver"); return false;}
|
|
||||||
if(player.isIncreaseSize()) player.increaseSnake(latestCoordinate);
|
|
||||||
|
|
||||||
TerminalDisplay.clearTerminal();
|
|
||||||
map.clearMap(edges);
|
|
||||||
player.incrementRound();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean robotRound(Robot robot) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean instancePersonnage(Personnage personnage) {
|
|
||||||
if (personnage instanceof Players) {
|
|
||||||
// tour du Player
|
|
||||||
return playerRound((Players)personnage);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (personnage instanceof Robot) {
|
|
||||||
// tour du robot
|
|
||||||
return robotRound((Robot)personnage);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run() {
|
|
||||||
TerminalDisplay.clearTerminal();
|
|
||||||
if (edges) map.addEdges();
|
|
||||||
boolean isNotGameOver = true;
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
// place les personnages dans la grille.
|
|
||||||
placePersonnages(personnages);
|
|
||||||
|
|
||||||
while(isNotGameOver) {
|
|
||||||
for (i = 0; i<personnages.length; i++) {
|
|
||||||
Personnage personnage = personnages[i];
|
|
||||||
|
|
||||||
int[] coordinate = personnage.getPrimaryCoordinate();
|
|
||||||
|
|
||||||
System.out.println("Round : " + this.round + " | N : " + Personnage.n);
|
|
||||||
System.out.println(" Joueur " + (i+1) + " : " + personnage.getName() +
|
|
||||||
" (" + coordinate[0]+", "+ coordinate[1] +") | size : " + personnage.getSize());
|
|
||||||
|
|
||||||
isNotGameOver = instancePersonnage(personnage);
|
|
||||||
if(isNotGameOver) placePersonnages(personnages);
|
|
||||||
else break;
|
|
||||||
}
|
|
||||||
this.round++;
|
|
||||||
}
|
|
||||||
System.out.println("Le joueur " + (i+1) + " à perdu !");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
package display;
|
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
import Characters.*;
|
|
||||||
import Connexion.Reseau;
|
|
||||||
import Environnement.*;
|
|
||||||
|
|
||||||
public class TerminalChannel {
|
|
||||||
private static Map map;
|
|
||||||
private static Personnage[] personnages;
|
|
||||||
public int round = 0;
|
|
||||||
public Reseau reseau;
|
|
||||||
|
|
||||||
public static boolean edges = false;
|
|
||||||
|
|
||||||
public TerminalChannel(Map m, Personnage[] personnage, Reseau r) {
|
|
||||||
personnages = personnage;
|
|
||||||
map = m;
|
|
||||||
this.reseau=r;
|
|
||||||
|
|
||||||
run();
|
|
||||||
if (edges) map.addEdges();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void placePersonnages(Personnage[] personnages) {
|
|
||||||
for(Personnage personnage : personnages) {
|
|
||||||
map.placePersonnages(personnage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getInput(Reseau r){
|
|
||||||
char c=r.getLastedContent().charAt(0);
|
|
||||||
int i=c;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p> 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
|
|
||||||
*/
|
|
||||||
|
|
||||||
public boolean playerRound(Players player) {
|
|
||||||
TerminalDisplay.printMap(map, personnages);
|
|
||||||
// TerminalDisplay.printMapType(map);
|
|
||||||
|
|
||||||
int[] latestCoordinate = player.keepLatestCoordinate();
|
|
||||||
int input=getInput(null);
|
|
||||||
player.moveCoordinate(input);
|
|
||||||
|
|
||||||
if(map.isGameOver(input, player)) {TerminalDisplay.clearTerminal(); System.out.println("GameOver"); return false;}
|
|
||||||
if(player.isIncreaseSize()) player.increaseSnake(latestCoordinate);
|
|
||||||
|
|
||||||
TerminalDisplay.clearTerminal();
|
|
||||||
map.clearMap(edges);
|
|
||||||
player.incrementRound();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean robotRound(Robot robot) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean instancePersonnage(Personnage personnage) {
|
|
||||||
if (personnage instanceof Players) {
|
|
||||||
// tour du Player
|
|
||||||
return playerRound((Players)personnage);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (personnage instanceof Robot) {
|
|
||||||
// tour du robot
|
|
||||||
return robotRound((Robot)personnage);
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean run() {
|
|
||||||
TerminalDisplay.clearTerminal();
|
|
||||||
if (edges) map.addEdges();
|
|
||||||
boolean isNotGameOver = true;
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
// place les personnages dans la grille.
|
|
||||||
placePersonnages(personnages);
|
|
||||||
|
|
||||||
while(isNotGameOver) {
|
|
||||||
for (i = 0; i<personnages.length; i++) {
|
|
||||||
Personnage personnage = personnages[i];
|
|
||||||
|
|
||||||
int[] coordinate = personnage.getPrimaryCoordinate();
|
|
||||||
|
|
||||||
System.out.println("Round : " + this.round + " | N : " + Personnage.n);
|
|
||||||
System.out.println(" Joueur " + (i+1) + " : " + personnage.getName() +
|
|
||||||
" (" + coordinate[0]+", "+ coordinate[1] +") | size : " + personnage.getSize());
|
|
||||||
|
|
||||||
isNotGameOver = instancePersonnage(personnage);
|
|
||||||
if(isNotGameOver) placePersonnages(personnages);
|
|
||||||
else break;
|
|
||||||
}
|
|
||||||
this.round++;
|
|
||||||
}
|
|
||||||
System.out.println("Le joueur " + (i+1) + " à perdu !");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,143 +0,0 @@
|
|||||||
package display;
|
|
||||||
|
|
||||||
import Objects.Fruits;
|
|
||||||
import Objects.Items;
|
|
||||||
import Objects.Snake;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
import Characters.Personnage;
|
|
||||||
import Environnement.Map;
|
|
||||||
|
|
||||||
public class TerminalDisplay {
|
|
||||||
public static void printMap(Map map, Personnage[] personnages) {
|
|
||||||
Object[][] mapObjects = map.getGrid();
|
|
||||||
|
|
||||||
for (int i = 0; i<mapObjects.length; i++) {
|
|
||||||
for(int k = 0; k<mapObjects[0].length; k++) {
|
|
||||||
Object object = mapObjects[i][k];
|
|
||||||
|
|
||||||
if(object instanceof Items) printItems((Items)object, mapObjects, k, i);
|
|
||||||
if(object instanceof Fruits) printFruits((Fruits)object, i, k);
|
|
||||||
if(object instanceof Snake) printSnake((Snake)object, personnages, i, k);
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* cette fonction clear le terminal et le remet en
|
|
||||||
* haut a gauche de la ligne.
|
|
||||||
*/
|
|
||||||
public static void clearTerminal() {
|
|
||||||
System.out.println("\u001b[2J \u001b[H");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void printMapType(Map map) {
|
|
||||||
for (Object[] object : map.getGrid()) {
|
|
||||||
for (Object value : object) {
|
|
||||||
System.out.print(value.toString() + " ");
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printItems(Items item, Object[][] map, int x, int y) {
|
|
||||||
switch (item) {
|
|
||||||
case WALL: printWall(map, x, y); break;
|
|
||||||
case VOID: System.out.print(" "); break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printSnake(Snake item, Personnage[] personnages, int x, int y) {
|
|
||||||
switch (item) {
|
|
||||||
case BODY: System.out.print(" = "); break;
|
|
||||||
case HEAD: printHead(personnages, x, y); break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printFruits(Fruits item, int x, int y) {
|
|
||||||
switch (item) {
|
|
||||||
case FRAISE: System.out.print(" \uF353 ");break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printHead(Personnage[] personnages, int x, int y) {
|
|
||||||
Personnage personnage = searchSnake(personnages, x, y);
|
|
||||||
|
|
||||||
if (personnage != null) {
|
|
||||||
ArrayList<int[]> personnageCoordinate = personnage.getCoordinate();
|
|
||||||
|
|
||||||
int[] primaryCoordinate = personnage.getPrimaryCoordinate();
|
|
||||||
|
|
||||||
if (personnageCoordinate.size() > 1) {
|
|
||||||
int[] secondCoordinate = personnageCoordinate.get(1);
|
|
||||||
|
|
||||||
// UP DOWN LEFT RIGHT
|
|
||||||
boolean[] isHead = new boolean[] {
|
|
||||||
primaryCoordinate[0] == secondCoordinate[0] && primaryCoordinate[1] > secondCoordinate[1], // UP
|
|
||||||
primaryCoordinate[0] == secondCoordinate[0] && primaryCoordinate[1] < secondCoordinate[1], // DOWN
|
|
||||||
primaryCoordinate[1] == secondCoordinate[1] && primaryCoordinate[0] > secondCoordinate[0], // LEFT
|
|
||||||
primaryCoordinate[1] == secondCoordinate[1] && primaryCoordinate[0] < secondCoordinate[0] // RIGHT
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isHead[0]) {System.out.print(" \u21E9 ");return;}
|
|
||||||
else if (isHead[1]) {System.out.print(" \u21E7 ");return;}
|
|
||||||
else if (isHead[2]) {System.out.print(" \u21E8 ");return;}
|
|
||||||
else if (isHead[3]) {System.out.print(" \u21E6 ");return;}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.out.print(" \u21E8 ");return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static Personnage searchSnake(Personnage[] personnages, int x, int y) {
|
|
||||||
for (Personnage personnage : personnages) {
|
|
||||||
int[] primaryCoordinate = personnage.getPrimaryCoordinate();
|
|
||||||
|
|
||||||
int primaryY = primaryCoordinate[0];
|
|
||||||
int primaryX = primaryCoordinate[1];
|
|
||||||
|
|
||||||
if (primaryX == x && primaryY == y) {
|
|
||||||
return personnage;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void printWall(Object[][] map, int x, int y) {
|
|
||||||
// UP DOWN LEFT RIGHT
|
|
||||||
boolean[] isWall = new boolean[4];
|
|
||||||
|
|
||||||
if (y > 0) isWall[0] = map[y - 1][x] == Items.WALL; else isWall[0] = false;
|
|
||||||
if (y < map.length - 1) isWall[1] = map[y + 1][x] == Items.WALL; else isWall[1] = false;
|
|
||||||
if (x > 0) isWall[2] = map[y][x - 1] == Items.WALL; else isWall[2] = false;
|
|
||||||
if (x < map[0].length - 1) isWall[3] = map[y][x + 1] == Items.WALL; else isWall[3] = false;
|
|
||||||
|
|
||||||
System.out.print(whichWall(isWall));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String whichWall(boolean[] isWall) {
|
|
||||||
String positionWall = new String();
|
|
||||||
|
|
||||||
if (isWall[0] && isWall[1] && isWall[2] && isWall[3]) positionWall = "\u2550\u256C\u2550";
|
|
||||||
|
|
||||||
else if (isWall[0] && isWall[1] && isWall[3]) positionWall = "\u2560";
|
|
||||||
else if (isWall[0] && isWall[1] && isWall[2]) positionWall = "\u2563";
|
|
||||||
else if (isWall[0] && isWall[2] && isWall[3]) positionWall = "\u2550\u2569\u2550";
|
|
||||||
else if (isWall[1] && isWall[2] && isWall[3]) positionWall = "\u2550\u2566\u2550";
|
|
||||||
|
|
||||||
else if (isWall[0] && isWall[1]) positionWall = "\u2551";
|
|
||||||
else if (isWall[2] && isWall[3]) positionWall = "\u2550\u2550\u2550";
|
|
||||||
else if (isWall[0] && isWall[2]) positionWall = "\u255D ";
|
|
||||||
else if (isWall[0] && isWall[3]) positionWall = "\u255A";
|
|
||||||
else if (isWall[1] && isWall[2]) positionWall = "\u2557 ";
|
|
||||||
else if (isWall[1] && isWall[3]) positionWall = "\u2554";
|
|
||||||
|
|
||||||
else positionWall = "\u2550\u256C\u2550";
|
|
||||||
|
|
||||||
return positionWall;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
244
src/environnements/Map.java
Normal file
244
src/environnements/Map.java
Normal file
@@ -0,0 +1,244 @@
|
|||||||
|
package environnements;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import object.*;
|
||||||
|
import personnages.*;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette classe est la classe qui cree et genere
|
||||||
|
* tout ce qui est important pour la Map du jeu.
|
||||||
|
*/
|
||||||
|
public class Map {
|
||||||
|
/**
|
||||||
|
* cette variable est toute la grille où se
|
||||||
|
* passe tout le jeu.
|
||||||
|
*/
|
||||||
|
private Object grid[][];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette variable recupere tout les objects stockés,
|
||||||
|
* elle complete {@link #coordinateItems}.
|
||||||
|
*/
|
||||||
|
private ArrayList<Object> ObjectItems;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette variable recupere tout les coordonnées des
|
||||||
|
* objects stockés, elle complete {@link #ObjectItems}.
|
||||||
|
*/
|
||||||
|
private ArrayList<int[]> coordinateItems;
|
||||||
|
|
||||||
|
private int longueur;
|
||||||
|
private int largeur;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.largeur][this.longueur];
|
||||||
|
|
||||||
|
this.ObjectItems = new ArrayList<>();
|
||||||
|
this.coordinateItems = new ArrayList<>();
|
||||||
|
|
||||||
|
this.fillGrid();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* rempli toute la grille de Items.VOID pour eviter
|
||||||
|
* des problemes de null ou des problemes de
|
||||||
|
* collisions.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* renvoie la grille du jeu.
|
||||||
|
*/
|
||||||
|
public Object[][] getGrid() {
|
||||||
|
return grid.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isGameOver(int[] coordinate) {
|
||||||
|
return coordinate[0] < 0 || coordinate[0] >= this.grid[0].length ||
|
||||||
|
coordinate[1] < 0 || coordinate[1] >= this.grid.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* renvoie l'effet
|
||||||
|
* @param coordinate
|
||||||
|
* @return un {@link Effects}
|
||||||
|
*/
|
||||||
|
public Effects getEffect(int[] coordinate) {
|
||||||
|
Object object = this.grid[coordinate[1]][coordinate[0]];
|
||||||
|
return (object instanceof Items) ? ((Items)object).getEffects() : ((Snake)object).getEffects();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* clear la map.
|
||||||
|
*/
|
||||||
|
public void clearMap() {
|
||||||
|
this.fillGrid();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* inverse toute la grille pour soit mettre 0, 0 en bas
|
||||||
|
* ou en haut.
|
||||||
|
*/
|
||||||
|
public Object[][] getInverseGrid() {
|
||||||
|
Object[][] grid = getGrid();
|
||||||
|
Object[][] inverseGrid = new Object[this.largeur][this.longueur];
|
||||||
|
int k = 0;
|
||||||
|
|
||||||
|
for (int i = grid.length; i> 0; --i) {
|
||||||
|
inverseGrid[k++] = inverseGrid[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return inverseGrid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ajoute les coordonnées et les objets de façon non aléatoire
|
||||||
|
* @param object
|
||||||
|
* @param x
|
||||||
|
* @param y
|
||||||
|
*/
|
||||||
|
public void addObjects(Object object, int x, int y) {
|
||||||
|
this.coordinateItems.add(new int[]{x, y});
|
||||||
|
this.ObjectItems.add(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 addObjectsRandomize(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);
|
||||||
|
placeObjects();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction prend toutes les coordonnées du personnage
|
||||||
|
* et la place dans la grille
|
||||||
|
*/
|
||||||
|
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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction prend la grille et renvoie la meme grille
|
||||||
|
* mais avec de vrais bordures avec {@link #WALL}, cette fonction
|
||||||
|
* cree une nouvelle liste et ajoute l'ancienne avec toute les
|
||||||
|
* coordonnées dedans, les coordonnées mis dans grid seront ajouté
|
||||||
|
* de 1 dedans donc (1, 1) dans grid = (2, 2) dans edgesGrid
|
||||||
|
* @return la liste avec les murs.
|
||||||
|
*/
|
||||||
|
public Object[][] addEdges() {
|
||||||
|
Object[][] grid = this.getGrid();
|
||||||
|
Object[][] edgesGrid = new Object[this.largeur+2][this.longueur+2];
|
||||||
|
|
||||||
|
for(int i = 0; i < edgesGrid.length; i++) {
|
||||||
|
for(int k = 0; k < edgesGrid[0].length; k++) {
|
||||||
|
if (i == 0 || i == edgesGrid.length - 1 || k == 0 || k == edgesGrid[0].length - 1) {
|
||||||
|
edgesGrid[i][k] = Items.WALL;
|
||||||
|
} else {
|
||||||
|
edgesGrid[i][k] = grid[i-1][k-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return edgesGrid;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction regarde la coordonnée de la tete du
|
||||||
|
* snake et si il est egal au coordonnée d'un fruit,
|
||||||
|
* il le supprime pour eviter qu'il reste sur le terrain.
|
||||||
|
*
|
||||||
|
* Il faut le placer apres le gameover pour pas qu'il enleve un mur.
|
||||||
|
* @param coordinate
|
||||||
|
*/
|
||||||
|
public void deleteItems(int[] coordinate) {
|
||||||
|
for(int i = 0; i<this.coordinateItems.size(); i++) {
|
||||||
|
int[] itemCoordinate = this.coordinateItems.get(i);
|
||||||
|
if (itemCoordinate[0] == coordinate[0] && itemCoordinate[1] == coordinate[1]) {
|
||||||
|
this.coordinateItems.remove(i);
|
||||||
|
this.ObjectItems.remove(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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[1]][coordinate[0]] = ObjectItems.get(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* prend des coordonnées au hasard et regarde si c'est
|
||||||
|
* pas deja occupé par un item ou un corps sinon, il
|
||||||
|
* recommence.
|
||||||
|
* @param object
|
||||||
|
* @param number
|
||||||
|
*/
|
||||||
|
private void randomize(Object object, int number) {
|
||||||
|
Random random = new Random();
|
||||||
|
|
||||||
|
for (int i = 0; i<number; i++) {
|
||||||
|
int x = random.nextInt(this.grid[0].length);
|
||||||
|
int y = random.nextInt(this.grid.length);
|
||||||
|
|
||||||
|
if(!(this.grid[y][x] instanceof Snake) && (Items)this.grid[y][x] == Items.VOID) {
|
||||||
|
this.coordinateItems.add(new int[] {x, y});
|
||||||
|
this.ObjectItems.add(object);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
143
src/game/Terminal.java
Normal file
143
src/game/Terminal.java
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
package game;
|
||||||
|
|
||||||
|
import display.*;
|
||||||
|
import environnements.*;
|
||||||
|
import personnages.*;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Terminal {
|
||||||
|
private int round = 0;
|
||||||
|
|
||||||
|
private static Map map;
|
||||||
|
private static Personnage[] personnages;
|
||||||
|
|
||||||
|
private static Scanner scanner;
|
||||||
|
|
||||||
|
|
||||||
|
public Terminal(Map m, Personnage[] personnage) {
|
||||||
|
scanner = new Scanner(System.in);
|
||||||
|
personnages = personnage;
|
||||||
|
map = m;
|
||||||
|
|
||||||
|
run();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cette fonction est uniquement destiné pour la classe
|
||||||
|
* Players pour recuperer l'input dans le terminal.
|
||||||
|
* @param player
|
||||||
|
* @return il retourne int qui est le char en ascii
|
||||||
|
*/
|
||||||
|
private static int getInput(Player player) {
|
||||||
|
String value = new String();
|
||||||
|
Integer input = null;
|
||||||
|
|
||||||
|
do {
|
||||||
|
value = scanner.nextLine();
|
||||||
|
input = changeCoordinate(value);
|
||||||
|
}while(player.getMouvement(input) == null);
|
||||||
|
|
||||||
|
return input.intValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* transforme le String en prennant le premier char et
|
||||||
|
* le mets en ascii dans la classe Integer.
|
||||||
|
* @param input
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static Integer changeCoordinate(String input) {
|
||||||
|
if (input.length() > 0) {
|
||||||
|
return (int)input.charAt(0);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* place tout les personnages dans la fonction {@link #map.placePersonnages()}
|
||||||
|
* @param personnages
|
||||||
|
*/
|
||||||
|
private static void placePersonnages(Personnage[] personnages) {
|
||||||
|
for(Personnage personnage : personnages) {
|
||||||
|
map.placePersonnages(personnage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction est spécialement conçu pour gerer le gameplay du joueur.
|
||||||
|
* @param player
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static boolean playerRound(Player player) {
|
||||||
|
player.moveCoordinate(getInput(player));
|
||||||
|
|
||||||
|
int[] coordinate = player.getHeadCoordinate();
|
||||||
|
if(map.isGameOver(coordinate) || player.applyEffects(map.getEffect(coordinate))) return true;
|
||||||
|
map.deleteItems(coordinate);
|
||||||
|
|
||||||
|
player.increaseRound();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction est spécialement conçu pour gerer le gameplay du robot.
|
||||||
|
* @param player
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static boolean robotRound(Robot robot) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction cherche si le personnage mis en paramètre
|
||||||
|
* est un {@link Player} ou un {@link Robot}.
|
||||||
|
* @param player
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static boolean instancePersonnage(Personnage personnage) {
|
||||||
|
if (personnage instanceof Player) {
|
||||||
|
// tour du Player
|
||||||
|
return playerRound((Player)personnage);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (personnage instanceof Robot) {
|
||||||
|
// tour du robot
|
||||||
|
return robotRound((Robot)personnage);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* la fonction principal qui lance tout le jeu en terminal.
|
||||||
|
*/
|
||||||
|
private void run() {
|
||||||
|
boolean isGameOver = false;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while(!isGameOver) {
|
||||||
|
for (i = 0;i<personnages.length; i++) {
|
||||||
|
Personnage personnage = personnages[i];
|
||||||
|
Display.clearTerminal();
|
||||||
|
|
||||||
|
map.placeObjects();
|
||||||
|
placePersonnages(personnages);
|
||||||
|
|
||||||
|
Display.printInformation(this.round, i, personnage);
|
||||||
|
|
||||||
|
map.placeObjects();
|
||||||
|
Display.printMap(map.addEdges(), personnages);
|
||||||
|
|
||||||
|
isGameOver = instancePersonnage(personnage);
|
||||||
|
if(!isGameOver) placePersonnages(personnages);
|
||||||
|
else break;
|
||||||
|
|
||||||
|
System.out.println(1);
|
||||||
|
map.clearMap();
|
||||||
|
}
|
||||||
|
this.round++;
|
||||||
|
}
|
||||||
|
System.out.println("GAMEOVER \nLe joueur " + (i+1) + " à perdu !");
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src/object/Effects.java
Normal file
23
src/object/Effects.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package object;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette enumérateur {@link Effects} contient tout les effets
|
||||||
|
* necessaire pour le bon déroulement du jeu et quelque effets
|
||||||
|
* amusant qui pousse un peu plus les mecaniques du jeu.
|
||||||
|
*/
|
||||||
|
public enum Effects {
|
||||||
|
/**
|
||||||
|
* reduis le corps de 1.
|
||||||
|
*/
|
||||||
|
DECREASESIZE,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tue le joueur si contact.
|
||||||
|
*/
|
||||||
|
IMPASSABLE,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* le vide.
|
||||||
|
*/
|
||||||
|
VOID;
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package Objects;
|
package object;
|
||||||
|
|
||||||
public enum Items {
|
public enum Items {
|
||||||
WALL("WALL", Effects.IMPASSABLE),
|
WALL("WALL", Effects.IMPASSABLE),
|
||||||
VOID("VOID", Effects.VOID);
|
VOID("VOID", Effects.VOID),
|
||||||
|
|
||||||
|
FRAISE("FRAISE", Effects.DECREASESIZE);
|
||||||
|
|
||||||
private final String NAME;
|
private final String NAME;
|
||||||
private final Effects EFFECT;
|
private final Effects EFFECT;
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
package Characters;
|
package object;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cet enumerateur prend en charge tout les mouvements possible
|
* Cet enumerateur prend en charge tout les mouvements possible
|
||||||
* pour le serpent, il a uniquement la possibilité de se déplacer
|
* pour le serpent, il a uniquement la possibilité de se déplacer
|
||||||
* grâce a {@link Mouvements} pour la classe Player et Robot.
|
* grâce a {@link Mouvements} pour la classe Player et Robot.
|
||||||
*/
|
*/
|
||||||
public enum Mouvements {
|
@SuppressWarnings("unused")
|
||||||
|
public enum Mouvements implements Serializable {
|
||||||
/**
|
/**
|
||||||
* HAUT prend comme coordonnée (0, -1) pour se déplacer.
|
* HAUT prend comme coordonnée (0, -1) pour se déplacer.
|
||||||
* @param x = 0
|
* @param x = 0
|
||||||
@@ -48,7 +51,7 @@ public enum Mouvements {
|
|||||||
* @param coordinate prend principalement les coordonnées du
|
* @param coordinate prend principalement les coordonnées du
|
||||||
* personnage
|
* personnage
|
||||||
*/
|
*/
|
||||||
public void editCoordinate(int[] coordinate) {
|
public void updateCoordinate(int[] coordinate) {
|
||||||
coordinate[0] += this.deltaX;
|
coordinate[0] += this.deltaX;
|
||||||
coordinate[1] += this.deltaY;
|
coordinate[1] += this.deltaY;
|
||||||
}
|
}
|
||||||
@@ -60,4 +63,4 @@ public enum Mouvements {
|
|||||||
public int[] getCoordinate() {
|
public int[] getCoordinate() {
|
||||||
return new int[] {this.deltaX, this.deltaY};
|
return new int[] {this.deltaX, this.deltaY};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package Objects;
|
package object;
|
||||||
|
|
||||||
public enum Snake {
|
public enum Snake {
|
||||||
HEAD("TETE", Effects.IMPASSABLE),
|
HEAD("TETE", Effects.IMPASSABLE),
|
||||||
176
src/personnages/Personnage.java
Normal file
176
src/personnages/Personnage.java
Normal file
@@ -0,0 +1,176 @@
|
|||||||
|
package personnages;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import object.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cette classe est la primitive des classes
|
||||||
|
* {@link Player} et {@link Robot}. Elle contient
|
||||||
|
* tout le necessaire pour tout les personnages
|
||||||
|
* jouable du jeu.
|
||||||
|
*/
|
||||||
|
public class Personnage {
|
||||||
|
/**
|
||||||
|
* cette variable contient la valeur dans laquelle on sait
|
||||||
|
* quand le corps du snake grandi. il est le même pour tout
|
||||||
|
* les personnages du jeu et c'est pour ça que c'est un
|
||||||
|
* static. on peut le changer directement dans le main en
|
||||||
|
* faisant la commande par exemple : <pre><code>
|
||||||
|
* Personnage.n = 2</code></pre> sans appeler
|
||||||
|
* player ou robot, les deux valeurs vont "automatiquement"
|
||||||
|
* changer.
|
||||||
|
*/
|
||||||
|
public static int n;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette variable est le nombre de round fait par le snake,
|
||||||
|
* il s'incremente tout les tours quand le snake avance.
|
||||||
|
*/
|
||||||
|
private int round;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette variable est le nom du snake présent dans le tournois,
|
||||||
|
* il peut etre un mot aléatoire ou quelque chose de personnalisé
|
||||||
|
*/
|
||||||
|
protected String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette variable accumule toute les coordonnées du serpent, elle
|
||||||
|
* contient le corps et la tête et la première coordonnées est la tête.
|
||||||
|
* Le programme peut ajouter des coordonnées en fonction de round et n,
|
||||||
|
* exemple :
|
||||||
|
* <pre><code>
|
||||||
|
* si n > 0 et round > 0 ->
|
||||||
|
* retourner round%n == 0
|
||||||
|
* sinon
|
||||||
|
* retourner faux
|
||||||
|
* </code></pre>
|
||||||
|
*/
|
||||||
|
private ArrayList<int[]> coordinate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Le constructor initialise la variable {@link
|
||||||
|
* #coordinate} et prend en paramètre <strong>
|
||||||
|
* personnageCoordinate</strong> qui est la tête
|
||||||
|
* de notre snake. Il place dans la liste {@link
|
||||||
|
* #coordinate} la coordonnée. Nous pourrons
|
||||||
|
* recuperer les coordonnées de la tête en
|
||||||
|
* utilisant la fonction {@link #getHeadCoordinate()}.
|
||||||
|
*/
|
||||||
|
protected Personnage(int[] personnageCoordinate) {
|
||||||
|
coordinate = new ArrayList<>();
|
||||||
|
coordinate.add(personnageCoordinate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction applique tout les effets defini dans
|
||||||
|
* {@link Effects} et en même temps, sert de fermeture
|
||||||
|
* du programme avec le boolean si l'effet choisi est
|
||||||
|
* IMPASSABLE.
|
||||||
|
* @param effect est la variable qui contient tout les
|
||||||
|
* effets de {@link Effects}
|
||||||
|
* @return <pre><code>
|
||||||
|
* si effect == IMPASSABLE ->
|
||||||
|
* retourner true
|
||||||
|
*sinon
|
||||||
|
* retourner false
|
||||||
|
* </code></pre>
|
||||||
|
*/
|
||||||
|
public boolean applyEffects(Effects effect) {
|
||||||
|
switch (effect) {
|
||||||
|
case DECREASESIZE:
|
||||||
|
if (this.coordinate.size() > 1) {this.coordinate.removeLast();} break;
|
||||||
|
|
||||||
|
case VOID: break;
|
||||||
|
case IMPASSABLE: return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction renvoie la tête du snake mais en clone
|
||||||
|
* donc elle fait une copie de la position existante pour
|
||||||
|
* eviter de prendre l'adresse mémoire de la liste et pouvoir
|
||||||
|
* utiliser celle là comme on veut sans affecter la liste.
|
||||||
|
*/
|
||||||
|
public int[] getHeadCoordinate() {
|
||||||
|
return this.coordinate.getFirst().clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction renvoie la queue du snake mais en clone
|
||||||
|
* donc elle fait une copie de la position existante pour
|
||||||
|
* eviter de prendre l'adresse mémoire de la liste et pouvoir
|
||||||
|
* utiliser celle là comme on veut sans affecter la liste.
|
||||||
|
*/
|
||||||
|
public int[] getLatestCoordinate() {
|
||||||
|
return this.coordinate.getLast().clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction renvoie un clone de la liste de coordonnée
|
||||||
|
* donc elle fait une copie des positions existante pour
|
||||||
|
* eviter de prendre l'adresse mémoire de l'ArrayList et pouvoir
|
||||||
|
* utiliser celle là comme on veut sans affecter l'ArrayList.
|
||||||
|
*/
|
||||||
|
public ArrayList<int[]> getCoordinate() {
|
||||||
|
return new ArrayList<>(this.coordinate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction incremente la variable {@link #round}.
|
||||||
|
*/
|
||||||
|
public void increaseRound() {
|
||||||
|
++this.round;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction renvoie la variable {@link #round}.
|
||||||
|
*/
|
||||||
|
public int getRound() {
|
||||||
|
return round;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction renvoie la variable {@link #name}.
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction renvoie la longueur de {@link #getCoordinate()}.
|
||||||
|
*/
|
||||||
|
public int getSize() {
|
||||||
|
return this.getCoordinate().size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cette fonction change tout les coordonnées du serpent en mettant
|
||||||
|
* la coordonnée de celui d'avant, il prend [1, 2, 3, 4] et fais en
|
||||||
|
* premier temps [1, 1, 2, 3] puis modifie la premiere coordonnée
|
||||||
|
* avec Mouvement, si il y a -1, ça fait [0, 1, 2, 3].
|
||||||
|
* @param mouvement est le mouvement du personnage, comme HAUT, BAS,
|
||||||
|
* GAUCHE et DROITE.
|
||||||
|
*/
|
||||||
|
public void moveSnake(Mouvements mouvement) {
|
||||||
|
int[] latestCoordinate = getLatestCoordinate();
|
||||||
|
|
||||||
|
for (int i = this.coordinate.size() - 1; i > 0; i--) {
|
||||||
|
this.coordinate.set(i, this.coordinate.get(i - 1).clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
mouvement.updateCoordinate(this.coordinate.getFirst());
|
||||||
|
increaseSnake(latestCoordinate);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ajoute la coordonnée mis en paramètre dans le dernier emplacement si
|
||||||
|
* round > 0 et n > 0 et round%n = 0.
|
||||||
|
*/
|
||||||
|
private void increaseSnake(int[] coordinate) {
|
||||||
|
if(round > 0 && n > 0) if (round%n == 0) this.coordinate.add(coordinate);
|
||||||
|
}
|
||||||
|
}
|
||||||
47
src/personnages/Player.java
Normal file
47
src/personnages/Player.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package personnages;
|
||||||
|
|
||||||
|
import object.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* la classe Player a comme classe parent {@link Personnage}
|
||||||
|
* et qui contient tout les besoins primaire pour le bon
|
||||||
|
* fonctionnement de la classe Player. cette classse est très
|
||||||
|
* utile pour qu'un humain puisse jouer.
|
||||||
|
*/
|
||||||
|
public class Player extends Personnage {
|
||||||
|
/**
|
||||||
|
* la classe Player a comme classe parent {@link Personnage}
|
||||||
|
* et qui contient tout les besoins primaire pour le bon
|
||||||
|
* fonctionnement de la classe Player. Il comporte les coordonnées
|
||||||
|
* initiales pour placer correctement le personnage dans la grille
|
||||||
|
* du jeu.
|
||||||
|
* @param coordinate
|
||||||
|
* @param name
|
||||||
|
*/
|
||||||
|
public Player(int[] coordinate, String name) {
|
||||||
|
super(coordinate);
|
||||||
|
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean moveCoordinate(int keys) {
|
||||||
|
Mouvements value = getMouvement(keys);
|
||||||
|
|
||||||
|
if (value != null) {
|
||||||
|
moveSnake(value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mouvements getMouvement(Integer keys) {
|
||||||
|
switch (keys) {
|
||||||
|
case 0x77: case 0x7A: return Mouvements.HAUT; // w ou z
|
||||||
|
case 0x73: return Mouvements.BAS; // s
|
||||||
|
case 0x61: case 0x71: return Mouvements.GAUCHE; // a ou q
|
||||||
|
case 0x64: return Mouvements.DROITE; // d
|
||||||
|
case null: return null;
|
||||||
|
default: return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
src/personnages/Robot.java
Normal file
7
src/personnages/Robot.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package personnages;
|
||||||
|
|
||||||
|
public class Robot extends Personnage {
|
||||||
|
public Robot(int[] coordinate) {
|
||||||
|
super(coordinate);
|
||||||
|
}
|
||||||
|
}
|
||||||
38
src/tests/MapTest.java
Normal file
38
src/tests/MapTest.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package tests;
|
||||||
|
|
||||||
|
import display.Display;
|
||||||
|
import environnements.Map;
|
||||||
|
import object.Items;
|
||||||
|
import personnages.Personnage;
|
||||||
|
import personnages.Player;
|
||||||
|
|
||||||
|
public class MapTest {
|
||||||
|
public static void creationMap() {
|
||||||
|
Map map = new Map(30, 30);
|
||||||
|
map.addObjects(Items.FRAISE, 29, 29);
|
||||||
|
map.placeObjects();
|
||||||
|
|
||||||
|
Display.printMap(map.addEdges());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void drawMap() {
|
||||||
|
Map map = new Map(30, 30);
|
||||||
|
Display.printMap(map.addEdges(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void placePersonnageMap() {
|
||||||
|
Map map = new Map(30, 30);
|
||||||
|
map.placePersonnages(new Player(new int[]{1, 1}, "null"));
|
||||||
|
|
||||||
|
Display.printMap(map.addEdges(), new Personnage[] {new Player(new int[]{1, 1}, "null")});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void effects() {
|
||||||
|
Map map = new Map(5, 1);
|
||||||
|
Player player = new Player(new int[] {0, 0}, "null");
|
||||||
|
map.addObjects(Items.FRAISE, 0, 0);
|
||||||
|
|
||||||
|
player.applyEffects(map.getEffect(player.getHeadCoordinate()));
|
||||||
|
System.out.println(player.getSize());
|
||||||
|
}
|
||||||
|
}
|
||||||
38
src/tests/PersonnageTest.java
Normal file
38
src/tests/PersonnageTest.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package tests;
|
||||||
|
|
||||||
|
import object.Effects;
|
||||||
|
import object.Mouvements;
|
||||||
|
import personnages.Personnage;
|
||||||
|
import personnages.Player;
|
||||||
|
|
||||||
|
public class PersonnageTest {
|
||||||
|
public static void avancerPersonnage() {
|
||||||
|
Player player = new Player(new int[]{1, 1}, "test");
|
||||||
|
|
||||||
|
player.moveSnake(Mouvements.HAUT); // si la position s'est avancé, c'est normal
|
||||||
|
int[] coordinate = player.getHeadCoordinate();
|
||||||
|
|
||||||
|
System.out.println(coordinate[0] + " " + coordinate[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void taillePersonnage() {
|
||||||
|
Personnage.n = 2;
|
||||||
|
|
||||||
|
Player player = new Player(new int[]{1, 1}, "test");
|
||||||
|
player.increaseRound();
|
||||||
|
player.moveSnake(Mouvements.HAUT); // si il y a 2 valeurs, c'est normal
|
||||||
|
|
||||||
|
for (int[] coordinate : player.getCoordinate()) {
|
||||||
|
System.out.println(coordinate[0] + " " + coordinate[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void effectsPersonnage() {
|
||||||
|
Personnage.n = 2;
|
||||||
|
|
||||||
|
Player player = new Player(new int[]{1, 1}, "test");
|
||||||
|
player.applyEffects(Effects.DECREASESIZE); // si c'est vide c'est que ça marche
|
||||||
|
|
||||||
|
System.out.println(player.getCoordinate());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user