Advertisement
Guest User

Untitled

a guest
Oct 26th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.76 KB | None | 0 0
  1. package o1.viinaharava
  2.  
  3. import o1.grid.Coords
  4. import scala.collection.mutable.Buffer
  5.  
  6. /**
  7.  * The class `Glass` represents glasses in a game of Viinaharava.
  8.  * A glass object is mutable: it is initially full of water but
  9.  * can be modified to contain booze instead and can be drunk to
  10.  * empty its contents.
  11.  *
  12.  * @param board    the game board that the glass is located on
  13.  * @param location the location of the glass on the game board
  14.  */
  15. class Glass(val board: GameBoard, val location: Coords) {
  16.  
  17.   private var isFull = true
  18.   private var isWater = true    
  19.   private var danger = 0    
  20.  
  21.  
  22.   /**
  23.    * Determines whether the glass is empty.
  24.    *
  25.    * @return `true` if empty, `false` if full
  26.    */
  27.   def isEmpty = !this.isFull
  28.  
  29.  
  30.   /**
  31.    * Determines whether the glass is a glass of booze.
  32.    * A glass counts as a booze class even if the booze has already been drunk.
  33.    *
  34.    * @return `true` if the glass contains (or contained) booze, `false` otherwise
  35.    */
  36.   def isBooze = !this.isWater
  37.  
  38.  
  39.   /**
  40.    * Returns the number of neigboring booze glasses (diagonals included).
  41.    *
  42.    * Note to students: This method only returns the danger level of the glass.
  43.    * It does NOT change the level; that is the job of method [[pourBooze]].
  44.    */
  45.   def dangerLevel = this.danger
  46.  
  47.  
  48.   /**
  49.    * Returns this glass's neighboring glasses (diagonals included)
  50.    * on the game board.
  51.    */
  52.   def neighbors = board.neighbors(location: Coords, true)
  53.  
  54.  
  55.   /**
  56.    * If the glass is a water glass, fills it with booze instead.
  57.    * This raises the danger levels of neighboring glasses.
  58.    * If the glass was a booze glass to begin with, this method
  59.    * does nothing.
  60.    *
  61.    * @see [[dangerLevel]]
  62.    */
  63.   def pourBooze() = {
  64.     if(this.isWater) {
  65.       for(a <- this.neighbors) {
  66.         a.danger += 1
  67.       }
  68.       this.isWater = false
  69.     }
  70.   }
  71.  
  72.  
  73.   /**
  74.    * Virtually drinks the contents of the glass and returns a value indicating
  75.    * if it contained water or not.
  76.    *
  77.    * In case of a glass of water, "drinking" it just means that the glass is emptied.
  78.    * (If the water glass was already empty, this method does nothing but return `true`.)
  79.    *
  80.    * In case of a glass of booze, "drinking" it not only sets the glass to be empty but also
  81.    * empties all the other glasses of booze on the board. (This ends the game
  82.    * of Viinaharava and all the locations of booze glasses are revealed as a result.)
  83.    *
  84.    * @return `true` if the glass was a water glass, `false` if it was a booze glass
  85.    */
  86.   def drink() = {
  87.     if (this.isWater) {
  88.       isFull = false
  89.       true
  90.     } else {
  91.       isFull = false
  92.       for(a <- board.boozeGlasses) a.isFull = false
  93.       false
  94.     }
  95.   }
  96.    
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement