Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.58 KB | None | 0 0
  1. package o1.adventure
  2.  
  3. import scala.collection.mutable._
  4.  
  5.  
  6. /**
  7.  * A `Player` object represents a player character controlled by the real-life user of the program.
  8.  *
  9.  * A player object's state is mutable: the player's location and possessions can change, for instance.
  10.  *
  11.  * @param startingArea  the initial location of the player
  12.  */
  13. class Player(startingArea: Area) {
  14.  
  15.   private var currentLocation = startingArea        // gatherer: changes in relation to the previous location
  16.   private var quitCommandGiven = false              // one-way flag
  17.   private var inventory = ArrayBuffer[Item]()
  18.    
  19.  
  20.   /**
  21.    * Determines if the player has indicated a desire to quit the game.
  22.    */
  23.   def hasQuit = this.quitCommandGiven
  24.  
  25.  
  26.   /**
  27.    * Returns the current location of the player.
  28.    */
  29.   def location = this.currentLocation
  30.  
  31.  
  32.   /**
  33.    * Attempts to move the player in the given direction. This is successful if there
  34.    * is an exit from the player's current location towards the given direction.
  35.    *
  36.    * @param direction  a direction name (may be a nonexistent direction)
  37.    * @return a description of the results of the attempt
  38.    */
  39.   def go(direction: String) = {
  40.     val destination = this.location.neighbor(direction)
  41.     this.currentLocation = destination.getOrElse(this.currentLocation)
  42.     if (destination.isDefined) "You go " + direction + "." else "You can't go " + direction + "."
  43.   }
  44.  
  45.  
  46.   /**
  47.    * Causes the player to rest for a short while (this has no substantial effect in game terms).
  48.    *
  49.    * @return a description of what happened
  50.    */
  51.   def rest() = {
  52.     "You rest for a while. Better get a move on, though."
  53.   }
  54.  
  55.  
  56.   /**
  57.    * Signals that the player wants to quit the game.
  58.    *
  59.    * @return a description of what happened within the game as a result (which is the empty string, in this case)
  60.    */
  61.   def quit() = {
  62.     this.quitCommandGiven = true
  63.     ""
  64.   }
  65.  
  66.  
  67.   def drop(itemName: String): String = {
  68.     if(this.inventory.contains(itemName)) {
  69.       val toBeRemoved = inventory.filter( _.name == itemName)
  70.       inventory = inventory diff toBeRemoved
  71.       "You drop the " +
  72.       toBeRemoved(0).toString() +
  73.       "."
  74.     } else {
  75.       "You don't have that!"
  76.     }
  77.   }
  78.  
  79.   def examine(itemName: String): String = {
  80.     if(this.has(itemName)){
  81.       val the_item: Item = inventory.filter( _.name == itemName)(0)
  82.       val first_line = "You look closely at " + itemName + ".\n"
  83.       first_line + the_item.description
  84.     }else{
  85.       "If you want to examine something, you need to pick it up first."
  86.     }
  87.   }
  88.  
  89.   def get(itemName: String): String = {
  90.     if(this.currentLocation.contains(itemName)) {
  91.       "You pick up the " +
  92.       get(this.currentLocation.removeItem(itemName).toString()) +
  93.       "."      
  94.     } else {
  95.       "There is no " + itemName + " here to pick up."
  96.     }
  97.   }
  98.  
  99.  
  100.   def has(itemName: String): Boolean = this.inventory.contains(itemName)
  101.  
  102.   def makeInventory(): String = {
  103.     if (this.inventory.isEmpty) {
  104.       "The inventory is empty."
  105.     } else {
  106.       "You are carrying:\n" +
  107.       inventory.mkString("\n")
  108.      
  109.     }
  110.   }
  111.  
  112.   /*
  113.    *   def removeItem(itemName: String): Option[Item] = {
  114.     if (items.contains(itemName)) {
  115.       val toBeRemoved = items.filter( _.name == itemName)
  116.       items = items diff toBeRemoved
  117.       Some(toBeRemoved(0))
  118.     } else None
  119.   }
  120.    */
  121.  
  122.  
  123.   /**
  124.    * Returns a brief description of the player's state, for debugging purposes.
  125.    */
  126.   override def toString = "Now at: " + this.location.name  
  127.  
  128.  
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement