From 8ed5bb7a820ab291ba034b5fada23261fa0ee7a2 Mon Sep 17 00:00:00 2001 From: Cpt-Adok Date: Fri, 24 May 2024 16:03:11 +0200 Subject: [PATCH] Robot qui marche pas encore --- src/Personnages/Robot.java | 158 +++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/Personnages/Robot.java diff --git a/src/Personnages/Robot.java b/src/Personnages/Robot.java new file mode 100644 index 0000000..f6caf4f --- /dev/null +++ b/src/Personnages/Robot.java @@ -0,0 +1,158 @@ +package personnages; + +import java.util.ArrayList; +import java.util.Random; + +import types.*; +import environnements.*; +import connexion.*; + +public class Robot extends Personnage { + + Map m; + Mouvement move; + String name; + + public Robot(String name, int[] coordinate,Map m) { + super(coordinate); + this.name = name; + this.m=m; + move=this.compare(this.getHeadCoordinate(), this.choix().get(0)); + } + + + @Override + public boolean round(Map map, String channel){ + System.out.println("Est-ce que ça passe par là au moins ?"); + int[] coordinate = this.getHeadCoordinate(); + if (channel != null) Channel.envoyerMessage(this.getMove()); + + if(map.isGameOver(coordinate) || this.applyEffects(map.getEffect(coordinate))) return true; + + map.deleteItems(coordinate); + this.increaseRound(); + return false; + } + + public Mouvement getMove(){ + if (this.move!=null){ + return move; + } + return null; + } + + public boolean estPossible(int x,int y){ + Object [][] grille=this.m.getGrid(); + //System.out.println(x+" <= x , length grille : "+(grille.length-1)+" "+y+"<= y , length grille [0] : "+(grille[0].length-1)); + if (x>grille.length-1 || y>grille[0].length-1){ + return false; + } else if (grille[x][y]==Effect.IMPASSABLE){ + return false; + } + return true; + } + + public int [] creerTab(int x,int y){ + int [] t=new int [] {x,y}; + return t; + } + + public ArrayList coupsPossibles(int [] co) { + ArrayList coupsValables=new ArrayList (); + 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 casesAutour(){ + ArrayList t =this.coupsPossibles(this.getCoordinate().get(0)); + ArrayList t2 = new ArrayList<> (); + for (int i=0;i fusion(ArrayList t, ArrayList t2){ + for (int [] e :t2){ + t.add(e); + } + return t; + } + + public ArrayList choix(){ + ArrayList cases=casesAutour(); + ArrayList > w=new ArrayList<>(); + for (int i=0;i max=w.get(0); + for (ArrayList e :w){ + if (e.size()>max.size()){ + max=e; + } + } + if (w.size()==0){ + return this.suicide(cases); + } + return max; + } + + public ArrayList suicide(ArrayList murs){ + Random r=new Random(); + ArrayList a=new ArrayList<> (); + a.add(murs.get(r.nextInt(murs.size()))); + return a; + } + + public Mouvement compare(int[] t,int[] t2){ + if (t[0]>t2[0]){ + return Mouvement.BAS; + }else if (t[0]t2[1]){ + return Mouvement.DROITE; + }else if (t[0]==t2[0] || t[1]==t2[1]){ + return moveRandom(); + } + return null; + } + + public ArrayList killDouble(ArrayList t){ + for (int i=0;i