diff --git a/AlgoAlea.java b/AlgoAlea.java index 7e452f2..d096518 100644 --- a/AlgoAlea.java +++ b/AlgoAlea.java @@ -1,10 +1,8 @@ import java.util.Random; - import javax.swing.JFrame; import javax.swing.JOptionPane; public class AlgoAlea { - private boolean[][] map; private Cellules[][] grille; diff --git a/AlgoDeter.java b/AlgoDeter.java new file mode 100644 index 0000000..77526df --- /dev/null +++ b/AlgoDeter.java @@ -0,0 +1,41 @@ +import java.util.Random; +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +public class AlgoDeter { + public static final int MUR = 1; + public static final int PARCOURU = 0; + + private int[][] Parcours; + private boolean[][] map; + private Cellules[][] grille; + private int[] coordGate; + + private int cetteTaille; + private int comptErreur=0; + + private These notreThese; + + private JFrame cetteFrame; + + public AlgoDeter(int uneTaille, boolean[][] tableau, Cellules[][] uneGrille, JFrame uneFrame) { + this.cetteTaille = uneTaille; + this.map = tableau; + this.grille = uneGrille; + this.cetteFrame = uneFrame; + } + + public void auto() { + int decompte = 0; + + this.coordGate = outils.ParcoursCell(this.grille, this.cetteTaille); + //this.notreThese = new These(this.coordGate[0], this.coordGate[1], this.cetteTaille, this.map); + + Parcours ceParcours = new Parcours(this.grille, this.coordGate[0], this.coordGate[1], this.cetteTaille); + } + + public void manuel(){ + this.coordGate = outils.ParcoursCell(this.grille, this.cetteTaille); + ParcoursManuel ceParcour = new ParcoursManuel(this.grille, this.coordGate[0], this.coordGate[1], this.cetteTaille); + } +} diff --git a/GestionChoixAlgo.java b/GestionChoixAlgo.java index a27f980..11ec529 100644 --- a/GestionChoixAlgo.java +++ b/GestionChoixAlgo.java @@ -35,6 +35,8 @@ public class GestionChoixAlgo extends JFrame implements ActionListener { else if(this.ceGroupe0.getSelection().getActionCommand()=="Adeter" && this.ceGroupe1.getSelection().getActionCommand()=="Vauto") { this.frameGrille.dispose(); + AlgoDeter algorithme = new AlgoDeter(this.cetteTaille, this.cetteGrille, this.cetteGrilleCell, null); + algorithme.auto(); } else if(this.ceGroupe0.getSelection().getActionCommand()=="Aalea" && this.ceGroupe1.getSelection().getActionCommand()=="Vman") { @@ -43,7 +45,8 @@ public class GestionChoixAlgo extends JFrame implements ActionListener { } else if(this.ceGroupe0.getSelection().getActionCommand()=="Adeter" && this.ceGroupe1.getSelection().getActionCommand()=="Vman") { - + AlgoDeter algorithme = new AlgoDeter(this.cetteTaille, this.cetteGrille, this.cetteGrilleCell, null); + algorithme.manuel(); } } } \ No newline at end of file diff --git a/Makefile b/Makefile index 16a4017..fc1f592 100644 --- a/Makefile +++ b/Makefile @@ -85,7 +85,7 @@ Ecriture.class : Ecriture.java ChoixAlgo.class : ChoixAlgo.java GestionChoixAlgo.class ${JC} ${JCFLAGS} ChoixAlgo.java -GestionChoixAlgo.class : GestionChoixAlgo.java AlgoAlea.class +GestionChoixAlgo.class : GestionChoixAlgo.java AlgoAlea.class AlgoDeter.class ${JC} ${JCFLAGS} GestionChoixAlgo.java AlgoAlea.class : AlgoAlea.java These.class Attente.class @@ -97,6 +97,15 @@ These.class : These.java Attente.class : Attente.java ${JC} ${JCFLAGS} Attente.java +AlgoDeter.class : AlgoDeter.java Parcours.class ParcoursManuel.class + ${JC} ${JCFLAGS} AlgoDeter.java + +Parcours.class : Parcours.java + ${JC} ${JCFLAGS} Parcours.java + +ParcoursManuel.class : ParcoursManuel.java + ${JC} ${JCFLAGS} ParcoursManuel.java + # ================================ ### REGLES OPTIONNELLES ### diff --git a/Parcours.java b/Parcours.java new file mode 100644 index 0000000..2bf82bb --- /dev/null +++ b/Parcours.java @@ -0,0 +1,51 @@ +import javax.swing.JOptionPane; + +public class Parcours { + private int taille; + private int ceCompteur = 0; + private Cellules[][] labyrinthe; + + public Parcours(Cellules[][] tableau, int X, int Y, int len){ + this.taille = len; + this.labyrinthe = tableau; + + this.Parcourir(X, Y); + } + + public boolean Parcourir(int coordX, int coordY){ + + boolean done = false; + + if(estValide(coordX, coordY)) { + + if (this.labyrinthe[coordX][coordY].getType() == Cellules.SORTIE){ + JOptionPane.showMessageDialog(null, "Nous avons trouvé un chemin en "+(this.ceCompteur)+" coups pour ce labyrinthe", "Information", JOptionPane.INFORMATION_MESSAGE); + return true; + } else { + this.labyrinthe[coordX][coordY].setType(Cellules.MUR); + //Appels récursifs + if(done != true){ + done = Parcourir(coordX + 1, coordY); + done = Parcourir(coordX, coordY + 1); + done = Parcourir(coordX - 1, coordY); + done = Parcourir(coordX, coordY - 1); + } + } + } + return done; + } + + private boolean estValide(int pos_x, int pos_y) { + this.ceCompteur++; + boolean result = false; + //Si la case passée en paramètre est dans les dimensions du labyrinthe + if (pos_x >= 0 && pos_x < this.taille && pos_y >= 0 && pos_y < this.taille){ + //Et si la case est un espace vide + if (this.labyrinthe[pos_x][pos_y].getType() == Cellules.COULOIR || this.labyrinthe[pos_x][pos_y].getType() == Cellules.ENTREE || this.labyrinthe[pos_x][pos_y].getType() == Cellules.SORTIE){ + result = true; + } + } + return result; + + } //Fin estValide() +} diff --git a/ParcoursManuel.java b/ParcoursManuel.java new file mode 100644 index 0000000..c504d7b --- /dev/null +++ b/ParcoursManuel.java @@ -0,0 +1,58 @@ +import javax.swing.JOptionPane; + +public class ParcoursManuel { + private int taille; + private int ceCompteur = 0; + private Cellules[][] labyrinthe; + + public ParcoursManuel(Cellules[][] tableau, int X, int Y, int len){ + this.taille = len; + this.labyrinthe = tableau; + + this.Parcourir(X, Y); + } + + public boolean Parcourir(int coordX, int coordY){ + + boolean done = false; + + if(estValide(coordX, coordY)) { + + if (this.labyrinthe[coordX][coordY].getType() == Cellules.SORTIE){ + JOptionPane.showMessageDialog(null, "Nous avons trouvé un chemin en "+this.ceCompteur+" coups pour ce labyrinthe", "Information", JOptionPane.INFORMATION_MESSAGE); + return true; + } else { + this.labyrinthe[coordX][coordY].setType(Cellules.MUR); + //Appels récursifs + if(done != true){ + done = Parcourir(coordX + 1, coordY); + done = Parcourir(coordX, coordY + 1); + done = Parcourir(coordX - 1, coordY); + done = Parcourir(coordX, coordY - 1); + } + } + } + return done; + } + + private boolean estValide(int pos_x, int pos_y) { + this.ceCompteur++; + boolean result = false; + //Si la case passée en paramètre est dans les dimensions du labyrinthe + if (pos_x >= 0 && pos_x < this.taille && pos_y >= 0 && pos_y < this.taille){ + //Et si la case est un espace vide + if (this.labyrinthe[pos_x][pos_y].getType() != Cellules.SORTIE && this.labyrinthe[pos_x][pos_y].getType() != Cellules.MUR && this.labyrinthe[pos_x][pos_y].getType() != Cellules.ENTREE){ + this.labyrinthe[pos_x][pos_y].peindre(Cellules.VUE); + } + if (this.labyrinthe[pos_x][pos_y].getType() == Cellules.COULOIR || this.labyrinthe[pos_x][pos_y].getType() == Cellules.ENTREE || this.labyrinthe[pos_x][pos_y].getType() == Cellules.SORTIE){ + result = true; + } + if (this.labyrinthe[pos_x][pos_y].getType() != Cellules.SORTIE && this.labyrinthe[pos_x][pos_y].getType() != Cellules.MUR && this.labyrinthe[pos_x][pos_y].getType() != Cellules.ENTREE){ + this.labyrinthe[pos_x][pos_y].peindre(Cellules.DESSUS); + } + + } + return result; + + } //Fin estValide() +} diff --git a/Retournement.java b/Retournement.java deleted file mode 100644 index e3eea98..0000000 --- a/Retournement.java +++ /dev/null @@ -1,15 +0,0 @@ -public class Retournement { - private int[] ceTableau; - - public Retournement(int[] unTableau){ - this.ceTableau = unTableau; - } - - public void setModifyTab(int emplacementTab, int emplacementGrille){ - this.ceTableau[emplacementTab] = emplacementGrille; - } - - public int[] getValeurPorte(){ - return this.ceTableau; - } -} \ No newline at end of file diff --git a/These.java b/These.java index 16e3456..dff5c56 100644 --- a/These.java +++ b/These.java @@ -61,6 +61,43 @@ public class These { return false; } + /* ======================== regarder ======================== */ + + public boolean debordementVerticaux(){ + //renvoie true s'il y a débordement sinon false + if(coordY < 0 && coordY > this.cetteTaille){ + return true; + } + return LIBRE; + } + + public boolean lookBot(){ + if(coordY+1 < this.cetteTaille){ + if(this.cetteGrille[coordX+1][coordY] == OCCUPE){ + return OCCUPE; + } + } + return LIBRE; + } + + public boolean lookLeft(){ + if(coordY+1 < this.cetteTaille){ + if(this.cetteGrille[coordX][coordY-1] == OCCUPE){ + return OCCUPE; + } + } + return LIBRE; + } + + public boolean lookTop(){ + if(coordY+1 < this.cetteTaille){ + if(this.cetteGrille[coordX-1][coordY] == OCCUPE){ + return OCCUPE; + } + } + return LIBRE; + } + // Gestion Fin public boolean isArrived(int finalX, int finalY){ diff --git a/save.txt b/save.txt index 4bfe5af..1188a5a 100644 --- a/save.txt +++ b/save.txt @@ -1,34 +1,43 @@ - public void manuel() { - Random ran = new Random(); - int nxt=0; - int compteur = 0; +import javax.swing.JOptionPane; - Attente attendre = new Attente(); - this.fenetre.requestFocus(); - - - this.coordGate = outils.ParcoursCell(this.grille, this.cetteTaille); - this.notreThese = new These(coordGate[0], coordGate[1], this.cetteTaille, this.map); - - while(this.notreThese.isArrived(coordGate[2], coordGate[3]) != These.ARRIVE){ - - Attente attendre = new Attente(); - this.cetteFrame.addKeyListener(attendre); +public class Parcours { - /*nxt = ran.nextInt(4); - if(nxt == 0){ - this.notreThese.goRight(); - } else if(nxt == 1){ - this.notreThese.goDown(); - } else if(nxt == 2){ - this.notreThese.goLeft(); - } else { - this.notreThese.goTop(); - } - - this.grille[notreThese.getCoord()[0]][notreThese.getCoord()[1]].peindre(Cellules.VUE); - - compteur++; - estInfini(compteur);*/ - } - } \ No newline at end of file + public boolean resolutionLab(int pos_x, int pos_y) { + boolean done = false; + + //Si la fonction estValide() renvoie true + if (estValide(pos_x, pos_y)) { + + grille[pos_x][pos_y] = 3; + //Si la case passée en paramètre est la dernière case du labyrinthe + if (pos_x == grille.length-1 && pos_y == grille[0].length-1) + return true; + else { + //Appels récursifs + done = resolutionLab(pos_x + 1, pos_y); + if (!done) + done = resolutionLab(pos_x, pos_y + 1); + if (!done) + done = resolutionLab(pos_x - 1, pos_y); + if (!done) + done = resolutionLab(pos_x, pos_y - 1); + } if (done) + grille[pos_x][pos_y] = 7; + } + + return done; + + } //Fin resolutionLab() + + private boolean estValide(int pos_x, int pos_y) {x + boolean result = false; + //Si la case passée en paramètre est dans les dimensions du labyrinthe + if (pos_x >= 0 && pos_x < grille.length && pos_y >= 0 && pos_y < grille[0].length) + //Et si la case est un espace vide + if (grille[pos_x][pos_y] == 0) + result = true; + return result; + + } //Fin estValide() + +}