package chocobar.bpl; import java.util.*; import exception.*; import chocobar.GenericChocoGraph; import graph.GenericGraph; import graph.Vertice; import conf.ChocoConfig; public class ChocoGraph implements GenericChocoGraph { private int width; private int height; Graph configGraph; private Vertice lastExistingVertice; private int gccount; /** * Constructeur d'un graphe de jeu de Chocobar avec les barres * de taille (width X height) **/ public ChocoGraph(){ this.gccount=0; this.width=ChocoConfig.size; this.height=ChocoConfig.height; this.configGraph=null; } /** * Fonction qui verifie s'il existe deja un ChocoBar * de même configuration que celle passee en param * dans cet objet **/ private void genChocoFromPoint(ChocoBar oldCBar,int x, int y){ // creer une config : ajouter breakpoint (x, y) ChocoBar currentCBar=new ChocoBar(oldCBar); try { try{ currentCBar.addBreakPoint(x, y); } catch (OutOfRangeBreakPointException e1){ return; } } catch (AlreadyBrokenPointException e2){ // on doit pas y arriver ici } //System.out.print("Does the following bar exists? "); ChocoBarSet cbs=(ChocoBarSet)this.configGraph.getVerticeModel(); int fatherIdx=this.configGraph.indexOfVertice(oldCBar); if (cbs.contains(currentCBar)){ // on ajoute le du oldCbar (pere) vers // cette config (fils) // le pere est le dernier ajouté int sonIdx=this.configGraph.indexOfVertice(currentCBar); try{ this.configGraph.addEdge(fatherIdx,sonIdx); } catch (Exception e){ e.printStackTrace(); } // cette config existe deja donc finir return; } else { // c'est une nouvelle config // ajouter cette config au graphe, // par defaut, nouveaux arcs sont aussi rajoutes this.configGraph.addVertice(currentCBar); int sonIdx=this.configGraph.indexOfVertice(currentCBar); if (currentCBar.isFinalConfig()){ this.configGraph.setFinalState(sonIdx); } constructArcsFromFatherAncestorsTo(fatherIdx,sonIdx); /* try { this.constructArcsTo(currentCBar); } catch (Exception e) { e.printStackTrace(); } */ // trouver des arcs vers ce nouveau vertice //int verticeId=this.configGraph.indexOfVertice(currentCBar); // generer les config suivantes a partir d'une liste // des points frontieres Vector frontline=currentCBar.getBreakLine(); Iterator i=frontline.iterator(); while (i.hasNext()){ IntPoint currentP=(IntPoint)i.next(); genChocoFromPoint(currentCBar,(int)currentP.getX(),(int)currentP.getY()); } } } /** * Generation du graphe de jeu : creation de la racine **/ public GenericGraph genChoco(boolean isList){ // creation du noeud racine avec une barre toute neuve ChocoBar rootCBar=new ChocoBar(this.width,this.height); System.out.println("Root ChocoBar:"); rootCBar.display(); this.configGraph=new Graph(new ChocoBarSet(),isList); this.configGraph.setInitialState(0); //System.out.println(this.configGraph.toDot()); this.configGraph.addVertice(rootCBar); // on génère le graphe a partir de la racine this.genChocoFromPoint(rootCBar,this.width-1,this.height-1); return this.configGraph; } /** * Fonction de generation d'arc des sommets ascendants vers * un nouveau sommet sonIdx **/ public void constructArcsFromFatherAncestorsTo(int fatherIdx,int sonIdx){ ChocoBar fatherCB=(ChocoBar)this.configGraph.getVertice(fatherIdx); ChocoBar sonCB=(ChocoBar)this.configGraph.getVertice(sonIdx); Collection grandFathersIdx=null; // verifier si c'est bien un successeur if (sonCB.isNextOf(fatherCB)){ try{ this.configGraph.addEdge(fatherIdx,sonIdx);} catch (Exception e){ e.printStackTrace(); System.exit(-10); } try{ grandFathersIdx=this.configGraph.getIncident(fatherIdx);} catch (Exception e){ e.printStackTrace(); System.exit(-10); } if (grandFathersIdx!=null){ //condition d'arret Iterator i=grandFathersIdx.iterator(); while(i.hasNext()){ int grandFatherIdx=((Integer)i.next()).intValue(); constructArcsFromFatherAncestorsTo(grandFatherIdx,sonIdx); } } } } /** * Fonction qui renvoie le graph de cet objet **/ public Graph getGraph(){ return (this.configGraph); } /** * Fonction qui affiche tous les ChocoBar dans ce graphe **/ public void displayAll(){ int cpt=0; ChocoBarSet cbs=(ChocoBarSet)this.configGraph.getVerticeModel(); for (int i=0;i