Advertisement
Guest User

Vesa

a guest
Nov 23rd, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.90 KB | None | 0 0
  1. package o1.adventure
  2.  
  3. import scala.collection.mutable.Map
  4.  
  5. /**
  6.  * The class `Area` represents locations in a text adventure game world.
  7.  * A game world consists of areas. In general, an "area" can be pretty much
  8.  * anything: a room, a building, an acre of forest, or something completely
  9.  * different. What different areas have in common is that players can be located
  10.  * in them and that they can have exits leading to other, neighboring areas.
  11.  * An area also has a name and a description.
  12.  *
  13.  * @param name         the name of the area
  14.  * @param description  a basic description of the area (typically not including
  15.  *                     information about items)
  16.  */
  17. class Area(var name: String, var description: String) {
  18.  
  19.   private val neighbors = Map[String, Area]()
  20.   private var item_array: Array[Item] = Array();
  21.  
  22.   def addItem(item: Item): Unit = {
  23.     item_array :+ item
  24.   }
  25.  
  26.   def contains(itemName: String): Boolean = {
  27.     item_array.exists( _.name == itemName)
  28.   }
  29.  
  30.   def removeItem(itemName: String): Option[Item] = {
  31.     if(this.contains(itemName)){
  32.       val toBeRemoved = item_array.filter( _.name == itemName)
  33.       item_array = item_array diff toBeRemoved
  34.       Some(toBeRemoved(0))
  35.     }else{
  36.       return None
  37.     }
  38.   }
  39.   /**
  40.    * Returns the area that can be reached from this area by moving in the
  41.    * given direction.
  42.    *
  43.    * @return the neighboring area, wrapped in an `Option`; `None` if there
  44.    *         is no exit in the given direction
  45.    */
  46.   def neighbor(direction: String) = this.neighbors.get(direction)
  47.  
  48.  
  49.   /**
  50.    * Adds an exit from this area to the given area. The neighboring area
  51.    * is reached by moving in the specified direction from this area.
  52.    *
  53.    * @param direction  the direction of the exit from this area
  54.    * @param neighbor   the area that the exit leads to
  55.    */
  56.   def setNeighbor(direction: String, neighbor: Area) = {
  57.     this.neighbors += direction -> neighbor
  58.   }
  59.  
  60.  
  61.   /**
  62.    * Adds exits from this area to the given areas.
  63.    * Calling this method is equivalent to calling the `setNeighbor`
  64.    * method on each of the given direction--area pairs.
  65.    *
  66.    * @param exits  pairs of directions and the neighboring areas in that direction
  67.    * @see [[setNeighbor]]
  68.    */
  69.   def setNeighbors(exits: Vector[(String, Area)]) = {
  70.     this.neighbors ++= exits
  71.   }
  72.  
  73.  
  74.   /**
  75.    * Returns a multi-line description of the area as a player sees it.
  76.    * This includes a basic description of the area as well as information
  77.    * about exits and items.
  78.    */
  79.   def fullDescription = {
  80.     val exitList = "\n\nExits available: " + this.neighbors.keys.mkString(" ")
  81.     this.description + exitList
  82.   }
  83.  
  84.  
  85.   /**
  86.    * Returns a single-line description of the area for debugging purposes.  
  87.    */
  88.   override def toString = this.name + ": " + this.description.replaceAll("\n", " ").take(150)
  89.  
  90.  
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement