package labyrinthe;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import dijkstra.Dijkstra;
import fr.enst.inf103.ui.MazeViewController;
import fr.enst.inf103.ui.MazeViewSource;
public class MazeController implements MazeViewController {
Maze maze;
public MazeController() { // � l'ouverture du programme, on cr�e un labyrinthe 10x10
maze = new Maze(10,10);
}
public void calculateShortestPath() {
maze.clear();
DBox depart = maze.getD();
if( depart != null){
Dijkstra dijkstraObjet = new Dijkstra();
ArrayList<VertexInterface> chemin = null;
chemin = dijkstraObjet.dijkstra((GraphInterface)maze, depart).getShortestPathsTo(this.maze.getA());
if(chemin.size() == 0) { // Pas de chemin : on affiche une boite de dialogue
JOptionPane.showMessageDialog(null, "Le labyrinthe n'a pas de chemin solution", "Pas de chemin", JOptionPane.INFORMATION_MESSAGE);
} else {
for(int i = 0 ; i < chemin.size() -1 ; i++) {
MBox caseCourante = (MBox)chemin.get(i);
maze.setSymbolForBox(caseCourante.getX(), caseCourante.getY(), "*");
}
}
}
else
{
JOptionPane.showMessageDialog(null, "Le labyrinthe n'a pas de case de d�part", "Pas de d�part", JOptionPane.INFORMATION_MESSAGE);
}
}
public MazeViewSource getMazeViewSource() {
return maze;
}
public MazeViewSource newMaze() {
maze = new Maze();
return maze;
}
public MazeViewSource openMaze(String fileName) {
try {
maze = new Maze(fileName);
return maze;
} catch (MazeReadingException e) {
System.out.print("Erreur : " + e.getMessage());
}
return null;
}
public void saveMazeAs(String fileName) {
try {
((Maze)maze).saveToTextFile(fileName);
} catch (Exception e) {
System.out.println("Erreur : " + e.getMessage());
}
}
}