diff --git a/Affichage.java b/Affichage.java index bd027a5..f3d1d6a 100644 --- a/Affichage.java +++ b/Affichage.java @@ -1,5 +1,3 @@ -import javax.swing.CellEditor; - public class Affichage { private Cellules cetteCellules; private PanneauModification cePanel; @@ -32,16 +30,17 @@ public class Affichage { //System.out.println("Etat des deux valeur : " + this.caseEntrortie[0] + " "+ this.caseEntrortie[1]); if(radio2Selected==true && this.caseEntrortie[0]==0){ - this.cetEtat=ModificationsTab.LIBRE; + this.cetEtat=Cellules.LIBRE; this.cetteCellules.setType(Cellules.ENTREE); this.cetteCellules.peindre(Cellules.ENTREE); this.caseEntrortie[0]=1; } else if(radio3Selected==true && this.caseEntrortie[1]==0){ - this.cetEtat=ModificationsTab.LIBRE; + this.cetEtat=Cellules.LIBRE; this.cetteCellules.setType(Cellules.SORTIE); this.cetteCellules.peindre(Cellules.SORTIE); this.caseEntrortie[1]=1; - } else if(this.cetEtat == ModificationsTab.LIBRE && radio1Selected==true){ + } else if(this.cetEtat == Cellules.LIBRE && radio1Selected==true){ + //System.out.println("cellules peinte en couloir"); if(this.cetteCellules.getType()==Cellules.ENTREE){ this.caseEntrortie[0]=0; } else if(this.cetteCellules.getType()==Cellules.SORTIE){ @@ -49,7 +48,8 @@ public class Affichage { } this.cetteCellules.setType(Cellules.COULOIR); this.cetteCellules.peindre(Cellules.COULOIR); - } else if(this.cetEtat == ModificationsTab.OCCUPE && radio1Selected==true){ + } else if(this.cetEtat == Cellules.OCCUPE && radio1Selected==true){ + //System.out.println("cellules peinte en mur"); this.cetteCellules.setType(Cellules.MUR); this.cetteCellules.peindre(Cellules.MUR); } diff --git a/AlgoAlea.java b/AlgoAlea.java new file mode 100644 index 0000000..2850ab7 --- /dev/null +++ b/AlgoAlea.java @@ -0,0 +1,93 @@ +import java.util.Random; + +import javax.swing.JFrame; +import javax.swing.JOptionPane; + +public class AlgoAlea { + + private boolean[][] map; + private Cellules[][] grille; + + private int cetteTaille; + private int comptErreur=0; + private int[] coordGate; + + private These notreThese; + + private JFrame cetteFrame; + + public AlgoAlea(int uneTaille, boolean[][] tableau, Cellules[][] uneGrille, int visuel, JFrame uneFrame) { + this.map = tableau; + this.grille = uneGrille; + this.cetteFrame = uneFrame; + this.cetteTaille = uneTaille; + + if(visuel==0){ + this.auto(); + } else if(visuel==1){ + this.manuel(); + } + } + + /* ==================================== AUtomatique ==================================== */ + + public void auto() { + //outils.PrintGrilleBool(this.map,this.cetteTaille); + //outils.PrintGrilleCell(this.grille, this.cetteTaille); + + int decompte = 0; + int[] tabRes = new int[1000]; + + Random ran = new Random(); + int nxt=0; + + while(decompte < 1000){ + int compteur = 0; + + 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){ + 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(); + } + + compteur+=1; + estInfini(compteur); + } + tabRes[decompte] = compteur; + decompte++; + } + JOptionPane.showMessageDialog(null, "Nous avons une moyenne de : "+outils.moyenneTabint(tabRes)+" pour ce labyrinthe", "Information", JOptionPane.INFORMATION_MESSAGE); + } + + /* ==================================== Manuelle ==================================== */ + + public void manuel() { + this.coordGate = outils.ParcoursCell(this.grille, this.cetteTaille); + this.notreThese = new These(coordGate[0], coordGate[1], this.cetteTaille, this.map); + + Attente attendre = new Attente(this.notreThese, this.grille, this.coordGate); + this.cetteFrame.addKeyListener(attendre); + } + + + /* ==================================== INFINI ==================================== */ + + public void estInfini(int unCompteur){ + if(unCompteur > 10000){ + this.comptErreur++; + } + if(this.comptErreur > 3){ + JOptionPane.showMessageDialog(null, "Le labyrinthe est trop probablement non-finissable", "Erreur", JOptionPane.ERROR_MESSAGE); + System.exit(1); + } + } +} diff --git a/Attente.java b/Attente.java new file mode 100644 index 0000000..5cde579 --- /dev/null +++ b/Attente.java @@ -0,0 +1,67 @@ +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.util.Random; +import javax.swing.*; +import java.awt.*; + +public class Attente implements KeyListener { + public static final boolean FINI = true; + public static final boolean ENCOURS = false; + + private Random rand; + private int number; + private int compteur; + + private These notreThese; + private Cellules[][] grilleCellules; + private int[] coordGate; + + public Attente(These unPerso, Cellules[][] grille, int[] coord){ + this.notreThese = unPerso; + this.grilleCellules = grille; + this.compteur=0; + this.rand = new Random(); + this.coordGate = coord; + } + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_SPACE) { + this.grilleCellules[notreThese.getCoord()[0]][notreThese.getCoord()[1]].peindre(Cellules.VUE); + + this.number = this.rand.nextInt(4); + if(this.number == 0){ + this.notreThese.goRight(); + } else if(this.number == 1){ + this.notreThese.goDown(); + } else if(this.number == 2){ + this.notreThese.goLeft(); + } else { + this.notreThese.goTop(); + } + + this.grilleCellules[notreThese.getCoord()[0]][notreThese.getCoord()[1]].peindre(Cellules.DESSUS); + + this.compteur++; + + if(this.notreThese.isArrived(this.coordGate[2], this.coordGate[3]) == These.ARRIVE){ + JOptionPane.showMessageDialog(null, "Labyrinthe treminé en "+this.compteur+" coups !", "Information", JOptionPane.INFORMATION_MESSAGE); + System.exit(1); + } + } + } + + @Override + public void keyReleased(KeyEvent e) { + // Ne rien faire ici + } + + @Override + public void keyTyped(KeyEvent e) { + // Ne rien faire ici + } + + public void checked(){ + System.out.println("true"); + } +} diff --git a/Cellules.java b/Cellules.java index 1b45704..4d9260d 100644 --- a/Cellules.java +++ b/Cellules.java @@ -9,6 +9,12 @@ public class Cellules extends JComponent{ public static final int ENTREE=2; public static final int SORTIE=3; + public static final int DESSUS=10; + public static final int VUE=11; + + public static final boolean LIBRE = false; + public static final boolean OCCUPE = true; + private Color backgroundColor; private int cetteLigne; @@ -58,6 +64,10 @@ public class Cellules extends JComponent{ backgroundColor = Color.BLUE; } else if(peinture==SORTIE){ backgroundColor = Color.RED; + } else if(peinture==VUE){ + backgroundColor = Color.YELLOW; + } else if(peinture==DESSUS){ + backgroundColor = Color.ORANGE; } repaint(); diff --git a/ChoixAlgo.java b/ChoixAlgo.java index 036eab9..6e60a7e 100644 --- a/ChoixAlgo.java +++ b/ChoixAlgo.java @@ -1,7 +1,146 @@ +import javax.swing.*; +import java.awt.*; + public class ChoixAlgo extends Fenetre { - public ChoixAlgo(boolean[][] uneGrille){ + private JFrame frameGrille; + private boolean[][] ceTableau; + private Cellules[][] cetteGrille; + private int cetteTaille; + + public ChoixAlgo(int uneTaille, JFrame fenGrille, boolean[][] uneGrille, Cellules[][] uneGrilleCell){ super(); - outils.PrintGrilleBool(uneGrille, uneGrille.length); + this.fenetre.setVisible(true); + this.fenetre.setTitle("Algorithmes et visualisation"); + + this.ceTableau = uneGrille; + this.cetteGrille = uneGrilleCell; + this.frameGrille = fenGrille; + this.cetteTaille = uneTaille; + + GridLayout gestionnaire = new GridLayout(6, 1); + this.fenetre.setLayout(gestionnaire); + } + + public void setUp(){ + /* ============================= TEXT PRESENTATION =========================*/ + + JLabel unText = new JLabel(" Etape 2 : Choix de l'Algorithme et de la Visualisation"); + JPanel unPanel = new JPanel(); + unPanel.setBackground(Color.CYAN); + unPanel.add(unText, BorderLayout.CENTER); + JPanel unPanel2 = new JPanel(); + unPanel2.setBackground(Color.CYAN); + unPanel2.setLayout(new BoxLayout(unPanel2, BoxLayout.Y_AXIS)); + unPanel2.add(Box.createVerticalGlue()); + unPanel2.add(unPanel); + unPanel2.add(Box.createVerticalGlue()); + this.fenetre.add(unPanel2, BorderLayout.CENTER); + + /*============================ Choix de l'algo ============================*/ + + JLabel text0 = new JLabel("Choix de l'Algorithme"); + JPanel cePanel0 = new JPanel(); + cePanel0.setBackground(Color.CYAN); + cePanel0.add(text0, BorderLayout.CENTER); + JPanel cePanel1 = new JPanel(); + cePanel1.setBackground(Color.CYAN); + cePanel1.setLayout(new BoxLayout(cePanel1, BoxLayout.Y_AXIS)); + cePanel1.add(Box.createVerticalGlue()); + cePanel1.add(cePanel0); + cePanel1.add(Box.createVerticalGlue()); + this.fenetre.add(cePanel1, BorderLayout.CENTER); + + /* ===== -_- ===== */ + + JPanel unPanel3 = new JPanel(); + JPanel unPanel4 = new JPanel(); + + JRadioButton radioAA = new JRadioButton("Algorithme Aléatoire"); + JRadioButton radioAD = new JRadioButton("Algorithme Déterminatif"); + + radioAA.setActionCommand("Aalea"); + radioAD.setActionCommand("Adeter"); + + radioAA.setBackground(Color.CYAN); + radioAA.setSelected(true); + radioAD.setBackground(Color.CYAN); + + ButtonGroup group = new ButtonGroup(); + group.add(radioAA);group.add(radioAD); + + unPanel4.add(radioAA); + unPanel4.add(radioAD); + unPanel3.setBackground(Color.CYAN); + unPanel3.setLayout(new BoxLayout(unPanel3, BoxLayout.Y_AXIS)); + unPanel3.add(Box.createVerticalGlue()); + unPanel3.add(unPanel4); + unPanel3.add(Box.createVerticalGlue()); + unPanel3.setBackground(Color.CYAN); + unPanel4.setBackground(Color.CYAN); + this.fenetre.add(unPanel3, BorderLayout.CENTER); + + /* ===== */ + + JLabel text1 = new JLabel(" Choix de la visualisation"); + JPanel cePanel2 = new JPanel(); + cePanel2.setBackground(Color.CYAN); + cePanel2.add(text1, BorderLayout.CENTER); + JPanel cePanel3 = new JPanel(); + cePanel3.setBackground(Color.CYAN); + cePanel3.setLayout(new BoxLayout(cePanel3, BoxLayout.Y_AXIS)); + cePanel3.add(Box.createVerticalGlue()); + cePanel3.add(cePanel2); + cePanel3.add(Box.createVerticalGlue()); + this.fenetre.add(cePanel3, BorderLayout.CENTER); + + /*============================ Choix de la vue ============================*/ + JPanel unPanel5 = new JPanel(); + JPanel unPanel6 = new JPanel(); + + JRadioButton radioVA = new JRadioButton("Visualisation Automatique"); + JRadioButton radioVD = new JRadioButton("Visualisation Manuelle"); + + radioVA.setActionCommand("Vauto"); + radioVD.setActionCommand("Vman"); + + radioVA.setBackground(Color.CYAN); + radioVA.setSelected(true); + radioVD.setBackground(Color.CYAN); + + ButtonGroup groupe = new ButtonGroup(); + groupe.add(radioVA);groupe.add(radioVD); + + unPanel6.add(radioVA); + unPanel6.add(radioVD); + + unPanel5.setBackground(Color.CYAN); + unPanel5.setLayout(new BoxLayout(unPanel5, BoxLayout.Y_AXIS)); + unPanel5.add(Box.createVerticalGlue()); + unPanel5.add(unPanel6); + unPanel5.add(Box.createVerticalGlue()); + unPanel5.setBackground(Color.CYAN); + unPanel6.setBackground(Color.CYAN); + this.fenetre.add(unPanel5, BorderLayout.CENTER); + + /*Création du dernier panneau */ + JPanel unPanel10 = new JPanel(); + JButton unButton0 = new JButton("Suivant"); + JPanel unPanel11 = new JPanel(); + unPanel11.add(unButton0, BorderLayout.CENTER); + unPanel10.setBackground(Color.CYAN); + unPanel11.setBackground(Color.CYAN); + unPanel10.setBackground(Color.CYAN); + unPanel10.setLayout(new BoxLayout(unPanel10, BoxLayout.Y_AXIS)); + unPanel10.add(Box.createVerticalGlue()); + unPanel10.add(unPanel11); + unPanel10.add(Box.createVerticalGlue()); + this.fenetre.add(unPanel10, BorderLayout.CENTER); + + /* Evenement */ + + this.fenetre.setVisible(true); + + GestionChoixAlgo gestion = new GestionChoixAlgo(this.cetteTaille, this.fenetre, group, groupe, this.ceTableau, this.cetteGrille ,this.frameGrille); + unButton0.addActionListener(gestion); } - } diff --git a/FenetreImpGrille.java b/FenetreImpGrille.java index 5d9766b..9c12ff2 100644 --- a/FenetreImpGrille.java +++ b/FenetreImpGrille.java @@ -2,6 +2,7 @@ import java.awt.*; public class FenetreImpGrille extends Fenetre { private boolean[][] grille; + private Cellules[][] grilleCell; public FenetreImpGrille(){ super(); @@ -11,6 +12,7 @@ public class FenetreImpGrille extends Fenetre { /* ================================================================ Déclaration des variables ============================================================================= */ int[][] ce_double_tab = new int[taille][taille]; this.grille = new boolean[taille][taille]; + this.grilleCell = new Cellules[taille][taille]; //System.out.println("LA TAILLE DE LA GRRILLE VAUT : "+tabGrille.length); /* =============================================================== Gestion des paramètres de la fenètre ========================================================================== */ @@ -42,20 +44,25 @@ public class FenetreImpGrille extends Fenetre { if((i*taille+j)==(Lentre*taille+Centre)){ Cellules cellules = new Cellules(i, j, Cellules.ENTREE); + this.grilleCell[i][j] = cellules; this.fenetre.add(cellules); } else if((i*taille+j)==(Lortie*taille+Cortie)){ Cellules cellules = new Cellules(i, j, Cellules.SORTIE); + this.grilleCell[i][j] = cellules; this.fenetre.add(cellules); } else if(ce_double_tab[i][j] == 1){ Cellules cellules = new Cellules(i, j, Cellules.MUR); + this.grilleCell[i][j] = cellules; this.fenetre.add(cellules); } else{ Cellules cellules = new Cellules(i, j, Cellules.COULOIR); + this.grilleCell[i][j] = cellules; this.fenetre.add(cellules); } } } this.fenetre.setVisible(true); - outils.PrintGrilleBool(this.grille, taille); + ChoixAlgo Suite = new ChoixAlgo(taille, this.fenetre, this.grille, this.grilleCell); + Suite.setUp(); } } diff --git a/FenetreRndmGrille.java b/FenetreRndmGrille.java index 5eeefa9..e2d81ca 100644 --- a/FenetreRndmGrille.java +++ b/FenetreRndmGrille.java @@ -52,9 +52,9 @@ public class FenetreRndmGrille extends Fenetre{ int nombreAleatoire = rand.nextInt(2); // génère un nombre entre 0 et 2 if(nombreAleatoire == 0){ - grille[i][j] = true; + grille[i][j] = Cellules.LIBRE; //true } else { - grille[i][j] = false; + grille[i][j] = Cellules.OCCUPE; //false } this.modif = new Modifications(interfacePanel, grille,this.tabCouleur); @@ -65,7 +65,7 @@ public class FenetreRndmGrille extends Fenetre{ this.fenetre.add(cellules); cellules.addMouseListener(modif); grilleCellules[i][j] = cellules; - grille[i][j] = ModificationsTab.LIBRE; + grille[i][j] = Cellules.LIBRE; } else if(compteur == ValeurSortie) { @@ -73,9 +73,9 @@ public class FenetreRndmGrille extends Fenetre{ this.fenetre.add(cellules); cellules.addMouseListener(modif); grilleCellules[i][j] = cellules; - grille[i][j] = ModificationsTab.LIBRE; + grille[i][j] = Cellules.LIBRE; } - else if(grille[i][j] == true) + else if(grille[i][j] == Cellules.LIBRE) { Cellules cellules = new Cellules(i, j, Cellules.COULOIR); this.fenetre.add(cellules); @@ -94,8 +94,6 @@ public class FenetreRndmGrille extends Fenetre{ compteur++; } } - outils.PrintGrilleBool(grille, taille); - this.fenetre.setVisible(true); } } diff --git a/GestionChoixAlgo.java b/GestionChoixAlgo.java new file mode 100644 index 0000000..f08380f --- /dev/null +++ b/GestionChoixAlgo.java @@ -0,0 +1,47 @@ +import javax.swing.*; +import java.awt.event.*; + +public class GestionChoixAlgo extends JFrame implements ActionListener { + private JFrame cetteFrame; + private ButtonGroup ceGroupe0; + private ButtonGroup ceGroupe1; + + private int cetteTaille; + + private JFrame frameGrille; + private boolean[][] cetteGrille; + private Cellules[][] cetteGrilleCell; + + public GestionChoixAlgo(int uneTaille, JFrame uneFrame, ButtonGroup group0, ButtonGroup group1, boolean[][] uneGrille, Cellules[][] grilleCell,JFrame uneFenetre){ + this.cetteFrame = uneFrame; + this.ceGroupe0 = group0; + this.ceGroupe1 = group1; + + this.cetteTaille = uneTaille; + + this.frameGrille=uneFenetre; + this.cetteGrille = uneGrille; + this.cetteGrilleCell=grilleCell; + } + + public void actionPerformed(ActionEvent e){ + this.cetteFrame.dispose(); + if(this.ceGroupe0.getSelection().getActionCommand()=="Aalea" && this.ceGroupe1.getSelection().getActionCommand()=="Vauto") + { + this.frameGrille.dispose(); + AlgoAlea algorithme = new AlgoAlea(this.cetteTaille, this.cetteGrille, this.cetteGrilleCell, 0, null); + } + else if(this.ceGroupe0.getSelection().getActionCommand()=="Adeter" && this.ceGroupe1.getSelection().getActionCommand()=="Vauto") + { + this.frameGrille.dispose(); + } + else if(this.ceGroupe0.getSelection().getActionCommand()=="Aalea" && this.ceGroupe1.getSelection().getActionCommand()=="Vman") + { + AlgoAlea algorithme = new AlgoAlea(this.cetteTaille, this.cetteGrille, this.cetteGrilleCell, 1, this.frameGrille); + } + else if(this.ceGroupe0.getSelection().getActionCommand()=="Adeter" && this.ceGroupe1.getSelection().getActionCommand()=="Vman") + { + + } + } +} \ No newline at end of file diff --git a/GestionExporter.java b/GestionExporter.java index c753458..5d14d9c 100644 --- a/GestionExporter.java +++ b/GestionExporter.java @@ -26,18 +26,20 @@ public class GestionExporter implements ActionListener{ public void actionPerformed(ActionEvent e){ this.cet_event=e.getActionCommand(); if (cet_event.equals(this.Reponses1)){ - this.frameGrille.dispose(); this.frameModif.dispose(); this.framePopup.dispose(); //outils.PrintGrilleBool(this.cetteGrille, this.cetteTaille); + PreEcriture precriture = new PreEcriture(this.cetteGrille, this.grilleCellules, this.cetteTaille); - ChoixAlgo suite = new ChoixAlgo(this.cetteGrille); + + ChoixAlgo suite = new ChoixAlgo(this.cetteTaille, this.frameGrille ,this.cetteGrille, this.grilleCellules); + suite.setUp(); } else if (cet_event.equals(this.Reponses2)){ - this.frameGrille.dispose(); this.frameModif.dispose(); this.framePopup.dispose(); - ChoixAlgo suite = new ChoixAlgo(this.cetteGrille); + ChoixAlgo suite = new ChoixAlgo(this.cetteTaille, this.frameGrille, this.cetteGrille, this.grilleCellules); + suite.setUp(); } } } \ No newline at end of file diff --git a/GestionModif.java b/GestionModif.java index ac08204..a1c59bb 100644 --- a/GestionModif.java +++ b/GestionModif.java @@ -27,6 +27,7 @@ public class GestionModif implements ActionListener{ } else if (cet_event.equals(this.Reponses2)){ Exporter newExport = new Exporter(this.cetteGrille, this.grilleCellules, this.cetteTaille, this.cetteFrameGrille, this.cetteFrameModif); + //outils.PrintGrilleBool(this.cetteGrille, this.cetteTaille); } } } \ No newline at end of file diff --git a/Lecture.java b/Lecture.java index 983c098..08f7413 100644 --- a/Lecture.java +++ b/Lecture.java @@ -105,9 +105,9 @@ public class Lecture { ceResultat = outils.concatenate(ceResultat, outils.reverse(ceTableauTemp)); } - for(int i=0; i= 0){ + //System.out.println("etat case gauche :"+this.cetteGrille[coordX-1][coordY]+" pose :"+(coordX-1)+" "+coordY); + if(this.cetteGrille[coordX][coordY-1] == LIBRE){ + this.coordY = this.coordY-1; + return true; + } + } + return false; + } + + public boolean goTop(){ + if(coordX-1 >= 0){ + //System.out.println("etat case top : "+this.cetteGrille[coordX][coordY-1] +" pose : "+coordX+" "+(coordY-1)); + if(this.cetteGrille[coordX-1][coordY] == LIBRE){ + this.coordX = this.coordX-1; + return true; + } + } + return false; + } + + // Gestion Fin + + public boolean isArrived(int finalX, int finalY){ + // renvoie un boolean + if(this.coordX == finalX && this.coordY == finalY){ + return ARRIVE; + } else { + return CHEMIN; + } + } + + // Miscelaneous + + public void printPlacement(){ + System.out.println("La position en X vaut : "+coordX+" et en y : "+coordY); + } + + public int[] getCoord(){ + int[] coordonnes = new int[2]; + coordonnes[0] = coordX; coordonnes[1]=coordY; + return coordonnes; + } +} diff --git a/outils.java b/outils.java index 962101d..277aba1 100644 --- a/outils.java +++ b/outils.java @@ -107,4 +107,12 @@ public class outils { } return resultat; } + + public static int moyenneTabint(int[] leTableau){ + int somme = 0; + for(int valeur : leTableau){ + somme = somme + valeur; + } + return somme/leTableau.length; + } } diff --git a/save.txt b/save.txt new file mode 100644 index 0000000..4bfe5af --- /dev/null +++ b/save.txt @@ -0,0 +1,34 @@ + public void manuel() { + Random ran = new Random(); + int nxt=0; + int compteur = 0; + + 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); + + /*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