SAE21_2022/AlgoAlea.java

94 lines
2.9 KiB
Java
Raw Normal View History

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);
}
}
}