Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.istic.si2.maze.labyEtudiant
- import fr.istic.si2.maze.interfaces._
- import fr.istic.si2.mazelib.Matrix
- import fr.istic.si2.scribble._
- /**
- * Labyrinthe très simple: en serpentin de l'entrée à la sortie.
- */
- // TODO Exo 5 (lire l'énoncé)
- object MonLabyrinthe extends Labyrinthe {
- private val sMatrice: Matrice[Cellule] = new Matrix
- sealed trait T
- private case class Matr(matrice : sMatrice.T ,entree: (Int,Int), sortie:(Int,Int)) extends T
- /**
- * @param t Un labyrinthe
- * @return extrait le labyrinthe
- */
- private def extract(t:MonLabyrinthe.T) : Matr = {
- t match {
- case Matr(matrice, entree , sortie) => Matr(matrice, entree , sortie)
- }
- }
- /**
- * @param nb un entier
- * @return nb est pair ou pas
- */
- def isPair(nb: Int) = nb % 2 == 0
- //Valeur de la cellule de la matrice correcpont à Cellule(Ouvert, Ferme) le tuple (Ouvert, Ferme)
- override def nouveau(hauteur: Int, largeur: Int, entree: (Int, Int), sortie: (Int, Int)): T = {
- val initMatrice : (Int, Int) => Cellule = {(i:Int, j:Int) =>
- if ( (j == 0 && isPair(i)) || (j == largeur-1 && !isPair(i)) ){ Cellule(Ouvert, Ouvert) }
- else {Cellule(Ferme, Ouvert) }
- }
- val maMatrice = sMatrice.creer(hauteur, largeur, initMatrice)
- Matr(maMatrice , entree , sortie )
- }
- override def entree(laby: T): (Int, Int) = {
- extract(laby).entree
- }
- override def sortie(laby: T): (Int, Int) = {
- extract(laby).sortie
- }
- override def largeur(laby: T): Int = {
- val maMatrice = extract(laby).matrice
- sMatrice.nbColonnes(maMatrice)
- }
- override def hauteur(laby: T): Int = {
- val maMatrice = extract(laby).matrice
- sMatrice.nbLignes(maMatrice)
- }
- override def cellule(laby: T, position: (Int, Int)): Option[Cellule] = {
- val maMatrice = extract(laby).matrice
- val valCellule = sMatrice.get(maMatrice,position)
- valCellule match {
- case Some(cell) => position match{
- case (i,j) => if ( i > hauteur(laby) || j > largeur(laby) ) None
- else Some(cell)
- case _ => None }
- case None => None
- }
- }
- override def toImage(laby: T, etats: (Int, Int) => StatutCellule): Image = {
- val maMatrice = extract(laby).matrice
- val ptEntree = entree(laby)
- val ptSortie = sortie(laby)
- val initMat : (Cellule,Int, Int) => Image = {(c:Cellule , i:Int, j:Int ) =>
- if ( (i,j) == entree(laby) ) {Util.marqueurEntree}
- else if( (i,j) == sortie(laby) ) {Util.marqueurSortie}
- else if( etats(i,j) == Courante ) {Util.celluleToImage( c , Courante)}
- else if( etats(i,j) == Visitee ) {Util.celluleToImage( c , Visitee)}
- else {Util.celluleToImage( c , NonVisitee)}
- }
- sMatrice.toImage(maMatrice, initMat)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement