Advertisement
Guest User

Main2.scala

a guest
Apr 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. package fr.istic.si2.maze.labyEtudiant
  2.  
  3. import fr.istic.si2.maze.interfaces._
  4. import fr.istic.si2.mazelib.Matrix
  5. import fr.istic.si2.scribble._
  6.  
  7. /**
  8. * Labyrinthe très simple: en serpentin de l'entrée à la sortie.
  9. */
  10. // TODO Exo 5 (lire l'énoncé)
  11.  
  12. object MonLabyrinthe extends Labyrinthe {
  13.  
  14. private val sMatrice: Matrice[Cellule] = new Matrix
  15.  
  16. sealed trait T
  17.  
  18. private case class Matr(matrice : sMatrice.T ,entree: (Int,Int), sortie:(Int,Int)) extends T
  19.  
  20. /**
  21. * @param t Un labyrinthe
  22. * @return extrait le labyrinthe
  23. */
  24. private def extract(t:MonLabyrinthe.T) : Matr = {
  25. t match {
  26. case Matr(matrice, entree , sortie) => Matr(matrice, entree , sortie)
  27. }
  28. }
  29.  
  30. /**
  31. * @param nb un entier
  32. * @return nb est pair ou pas
  33. */
  34. def isPair(nb: Int) = nb % 2 == 0
  35.  
  36. //Valeur de la cellule de la matrice correcpont à Cellule(Ouvert, Ferme) le tuple (Ouvert, Ferme)
  37. override def nouveau(hauteur: Int, largeur: Int, entree: (Int, Int), sortie: (Int, Int)): T = {
  38.  
  39. val initMatrice : (Int, Int) => Cellule = {(i:Int, j:Int) =>
  40. if ( (j == 0 && isPair(i)) || (j == largeur-1 && !isPair(i)) ){ Cellule(Ouvert, Ouvert) }
  41. else {Cellule(Ferme, Ouvert) }
  42. }
  43.  
  44. val maMatrice = sMatrice.creer(hauteur, largeur, initMatrice)
  45.  
  46. Matr(maMatrice , entree , sortie )
  47. }
  48.  
  49. override def entree(laby: T): (Int, Int) = {
  50. extract(laby).entree
  51. }
  52.  
  53. override def sortie(laby: T): (Int, Int) = {
  54. extract(laby).sortie
  55. }
  56.  
  57. override def largeur(laby: T): Int = {
  58. val maMatrice = extract(laby).matrice
  59. sMatrice.nbColonnes(maMatrice)
  60. }
  61.  
  62. override def hauteur(laby: T): Int = {
  63. val maMatrice = extract(laby).matrice
  64. sMatrice.nbLignes(maMatrice)
  65. }
  66.  
  67.  
  68. override def cellule(laby: T, position: (Int, Int)): Option[Cellule] = {
  69. val maMatrice = extract(laby).matrice
  70. val valCellule = sMatrice.get(maMatrice,position)
  71.  
  72. valCellule match {
  73. case Some(cell) => position match{
  74. case (i,j) => if ( i > hauteur(laby) || j > largeur(laby) ) None
  75. else Some(cell)
  76. case _ => None }
  77. case None => None
  78. }
  79. }
  80.  
  81.  
  82. override def toImage(laby: T, etats: (Int, Int) => StatutCellule): Image = {
  83. val maMatrice = extract(laby).matrice
  84. val ptEntree = entree(laby)
  85. val ptSortie = sortie(laby)
  86.  
  87. val initMat : (Cellule,Int, Int) => Image = {(c:Cellule , i:Int, j:Int ) =>
  88.  
  89. if ( (i,j) == entree(laby) ) {Util.marqueurEntree}
  90. else if( (i,j) == sortie(laby) ) {Util.marqueurSortie}
  91.  
  92. else if( etats(i,j) == Courante ) {Util.celluleToImage( c , Courante)}
  93. else if( etats(i,j) == Visitee ) {Util.celluleToImage( c , Visitee)}
  94.  
  95. else {Util.celluleToImage( c , NonVisitee)}
  96. }
  97. sMatrice.toImage(maMatrice, initMat)
  98. }
  99.  
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement