mirror of
https://github.com/Cpt-Adok/SNAKE.git
synced 2026-01-25 12:34:07 +00:00
Robot fini mais problème en réseau
This commit is contained in:
@@ -19,11 +19,15 @@ public class Robot extends Personnage {
|
|||||||
this.m=m;
|
this.m=m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**Fonction commune aux sous-classes de Personnage
|
||||||
|
* permettant le renvoi d'un mouvement pour chacun.
|
||||||
|
*/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean round(Map map, String channel){
|
public boolean round(Map map, String channel){
|
||||||
System.out.println(this.choix().size()+ " move : "+this.compare(this.getHeadCoordinate(), this.choix().get(0)));
|
this.move=this.compare(this.choix(),this.getHeadCoordinate());
|
||||||
this.move=this.compare(this.getHeadCoordinate(), this.choix().get(0));
|
// System.out.println("Mouvement choisi : "+this.move);
|
||||||
|
this.moveSnake(move);
|
||||||
|
|
||||||
int[] coordinate = this.getHeadCoordinate();
|
int[] coordinate = this.getHeadCoordinate();
|
||||||
if (channel != null) Channel.envoyerMessage(this.move);
|
if (channel != null) Channel.envoyerMessage(this.move);
|
||||||
@@ -35,6 +39,10 @@ public class Robot extends Personnage {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accès à la variable move
|
||||||
|
* @return Mouvement
|
||||||
|
*/
|
||||||
public Mouvement getMove(){
|
public Mouvement getMove(){
|
||||||
if (this.move!=null){
|
if (this.move!=null){
|
||||||
return move;
|
return move;
|
||||||
@@ -42,88 +50,110 @@ public class Robot extends Personnage {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet de savoir si une case de la map est vide ou si elle est impassable
|
||||||
|
* @param x coordonnée longueur
|
||||||
|
* @param y coordonnée largeur
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
public boolean estPossible(int x,int y){
|
public boolean estPossible(int x,int y){
|
||||||
Object [][] grille=this.m.getGrid();
|
Grid [][] grille=this.m.getGrid();
|
||||||
if (x>=0 && x<grille.length && y>=0 && y<grille[0].length){
|
if (x>=0 && x<grille.length && y>=0 && y<grille[0].length){
|
||||||
if (grille[x][y]!=Effect.IMPASSABLE){
|
if (m.getEffect(this.creerTab(x, y))!=Effect.IMPASSABLE){
|
||||||
System.out.println("Ca passe");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
System.out.println("Ca passe que le premier if");
|
|
||||||
}
|
}
|
||||||
System.out.println("Ca passe rien");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fonction pour éviter les répétitions
|
||||||
|
* Permet de créer un tableau à partir de
|
||||||
|
* @param x et
|
||||||
|
* @param y
|
||||||
|
* @return int []
|
||||||
|
*/
|
||||||
public int [] creerTab(int x,int y){
|
public int [] creerTab(int x,int y){
|
||||||
int [] t=new int [] {x,y};
|
int [] t=new int [] {x,y};
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Identifie les cases présentes autour de la tête du serpent
|
||||||
|
* @param co coordonnées de la tête
|
||||||
|
* @return ArrayList<int []>, ArrayList des positions autour
|
||||||
|
*/
|
||||||
|
public ArrayList<int []> casesAutour(int [] co){
|
||||||
|
ArrayList<int []> autour=new ArrayList<>();
|
||||||
|
int x=co[0]; // x= ligne
|
||||||
|
int y=co[1]; // y= colonne
|
||||||
|
autour.add(creerTab(x+1, y));
|
||||||
|
autour.add(creerTab(x-1, y));
|
||||||
|
autour.add(creerTab(x, y+1));
|
||||||
|
autour.add(creerTab(x, y-1));
|
||||||
|
return autour;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Permet d'identifier les cases valables parmi les voisines de la tête du serpent
|
||||||
|
* @param co
|
||||||
|
* @return ArrayList<int[]> regroupant les cases voisines qui sont jouables sans mourir
|
||||||
|
*/
|
||||||
|
|
||||||
public ArrayList<int []> coupsPossibles(int [] co) {
|
public ArrayList<int []> coupsPossibles(int [] co) {
|
||||||
ArrayList<int []> coupsValables=new ArrayList<int []> ();
|
ArrayList<int []> coupsValables=new ArrayList<int []> ();
|
||||||
if (this.estPossible(co[0]+1,co[1])){
|
ArrayList<int []> autour=casesAutour(co);
|
||||||
coupsValables.add(creerTab(co[0]+1, co[1]));
|
for (int [] e:autour){
|
||||||
}else if (this.estPossible(co[0],co[1]+1)){
|
if (this.estPossible(e[0], e[1])){
|
||||||
coupsValables.add(creerTab(co[0], co[1]+1));
|
coupsValables.add(e);
|
||||||
}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;
|
return coupsValables;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<int[]> fusion(ArrayList<int[]> t, ArrayList<int[]> t2){
|
/**
|
||||||
for (int [] e :t2){
|
* Décision finale aléatoire du coup parmi ceux possibles
|
||||||
t.add(e);
|
* @return int [] des coordonnées du coup
|
||||||
}
|
*/
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ArrayList <int []> choix(){
|
public int [] choix(){
|
||||||
Random r=new Random();
|
Random r=new Random();
|
||||||
ArrayList <int[]> cases=coupsPossibles(this.getHeadCoordinate());
|
ArrayList <int[]> cases=coupsPossibles(this.getHeadCoordinate());
|
||||||
if (cases.size()==0){
|
if (cases.size()==0){
|
||||||
return this.suicide(cases);
|
return this.suicide();
|
||||||
}
|
}
|
||||||
int [] choix=cases.get(r.nextInt(cases.size()));
|
int [] choix=cases.get(r.nextInt(cases.size()));
|
||||||
ArrayList <int []> choisi =new ArrayList<int[]>();
|
return choix;
|
||||||
choisi.add(choix);
|
|
||||||
return choisi;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList <int []> suicide(ArrayList <int []> murs){
|
/**
|
||||||
|
* Permet de mettre fin à la partie quand le serpent est coincé, et que aucun n'est valable
|
||||||
|
* @return int [] des coordonnées du coup
|
||||||
|
*/
|
||||||
|
|
||||||
|
public int [] suicide(){
|
||||||
|
ArrayList <int []> murs=this.casesAutour(getHeadCoordinate());
|
||||||
Random r=new Random();
|
Random r=new Random();
|
||||||
ArrayList <int []> a=new ArrayList<> ();
|
return murs.get(r.nextInt(4));
|
||||||
a.add(murs.get(r.nextInt(4)));
|
|
||||||
return a;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comparaison des coordonnées de la tête par rapport à celle du coup
|
||||||
|
* @param t coup
|
||||||
|
* @param t2 tête
|
||||||
|
* @return Mouvement pour jouer le coup
|
||||||
|
*/
|
||||||
|
|
||||||
public Mouvement compare(int[] t,int[] t2){
|
public Mouvement compare(int[] t,int[] t2){
|
||||||
if (t[0]>t2[0]){
|
if (t[0]>t2[0]){
|
||||||
return Mouvement.BAS;
|
|
||||||
}else if (t[0]<t2[0]){
|
|
||||||
return Mouvement.HAUT;
|
|
||||||
}else if (t[1]<t2[1]){
|
|
||||||
return Mouvement.GAUCHE;
|
|
||||||
}else if (t[1]>t2[1]){
|
|
||||||
return Mouvement.DROITE;
|
return Mouvement.DROITE;
|
||||||
|
}else if (t[0]<t2[0]){
|
||||||
|
return Mouvement.GAUCHE;
|
||||||
|
}else if (t[1]<t2[1]){
|
||||||
|
return Mouvement.HAUT;
|
||||||
|
}else if (t[1]>t2[1]){
|
||||||
|
return Mouvement.BAS;
|
||||||
}
|
}
|
||||||
System.out.println("Problème Robot.compare");
|
|
||||||
return null;
|
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.getHeadCoordinate()){
|
|
||||||
t.remove(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user