flip.scala 2.34 KB
Newer Older
William Babonnaud's avatar
William Babonnaud committed
1 2 3 4
import scala.util.Random
import Array._
import java.awt.Color

Fardale's avatar
Fardale committed
5
class Flip extends JeuCouleur {
William Babonnaud's avatar
William Babonnaud committed
6 7

   // Génération d'une grille de départ aléatoire.
8
   def initialise() = {
Fardale's avatar
end  
Fardale committed
9
      map_init((i,j) => new MonBouton(i,j), boutons)
10 11 12 13 14 15
      def coloriage (b: MonBouton) = {
         var i = b.getTheX
         var j = b.getTheY
         b.active_couleur(Color.white)
      }
      map_apply(coloriage, boutons)
William Babonnaud's avatar
William Babonnaud committed
16
      var x = Random.nextBoolean
Fardale's avatar
end  
Fardale committed
17
      def f (b: MonBouton) = {
18 19
          var i = b.getTheX
          var j = b.getTheY 
20
          if (x) { var bl = clique_action_gauche(i,j) }
21 22
          x = Random.nextBoolean 
      }
Fardale's avatar
Fardale committed
23
      map_apply(f, boutons)
Fardale's avatar
end  
Fardale committed
24
      def init (b: MonBouton) = {
25 26 27 28 29
         var i = b.getTheX
         var j = b.getTheY
         sauvegardeMat(i)(j) = boutons(i)(j).getCouleur
      }
      map_apply(init, boutons)
William Babonnaud's avatar
William Babonnaud committed
30 31 32 33
   }

   // Génération aléatoire avec graine
   def initialise(n: Int) = {
Fardale's avatar
end  
Fardale committed
34
      map_init((i,j) => new MonBouton(i,j), boutons)
35 36 37 38 39 40
      def coloriage (b: MonBouton) = {
         var i = b.getTheX
         var j = b.getTheY
         b.active_couleur(Color.white)
      }
      map_apply(coloriage, boutons)
41 42
      var ran = new Random(n)
      var x = ran.nextBoolean
43
      def f (b: MonBouton) = {
44 45
         var i = b.getTheX
         var j = b.getTheY 
46 47 48
         if (x) { var bl = clique_action_gauche(i,j) } 
         x = ran.nextBoolean
      }
49
      map_apply(f, boutons)
Fardale's avatar
end  
Fardale committed
50
      def init (b: MonBouton) = {
51 52 53 54 55
         var i = b.getTheX
         var j = b.getTheY
         sauvegardeMat(i)(j) = boutons(i)(j).getCouleur
      }
      map_apply(init, boutons)
William Babonnaud's avatar
William Babonnaud committed
56 57 58 59
   }

   // Modification de la couleur d'un bouton.
   def chgBouton(i: Int, j: Int) = {
60 61 62 63 64
      if (boutons(i)(j).estColore(Color.white)) {
         boutons(i)(j).active_couleur(Color.black)
      } else {
         boutons(i)(j).active_couleur(Color.white)
      }
William Babonnaud's avatar
William Babonnaud committed
65 66 67
   }
   
   // Renvoire true si toutes les cases du terrain sont blanches
68 69
   def victoire = {
      var res = true
Fardale's avatar
end  
Fardale committed
70
      def f (b : MonBouton) = {
Fardale's avatar
Fardale committed
71 72
           var i = b.getTheX
           var j = b.getTheY
73
           res = (res && boutons(i)(j).estColore(Color.white))
William Babonnaud's avatar
William Babonnaud committed
74
      }
Fardale's avatar
Fardale committed
75
      map_apply(f, boutons)
76
      res
William Babonnaud's avatar
William Babonnaud committed
77 78 79
   }
   
   // Conséquence du clic d'un bouton.
80
   def clique_action_gauche(i: Int, j:Int) : Boolean = {
William Babonnaud's avatar
William Babonnaud committed
81
      chgBouton(i,j)
Fardale's avatar
Fardale committed
82 83
      var voisin = voisinDirect(i,j)
      for((x,y) <- voisin){chgBouton(x,y)}
84
      false 
William Babonnaud's avatar
William Babonnaud committed
85 86 87
   }

}