Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
132
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._
  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 items = ArrayBuffer[Item]()
  21.  
  22.  
  23.   /**
  24.    * Returns the area that can be reached from this area by moving in the
  25.    * given direction.
  26.    *
  27.    * @return the neighboring area, wrapped in an `Option`; `None` if there
  28.    *         is no exit in the given direction
  29.    */
  30.   def neighbor(direction: String) = this.neighbors.get(direction)
  31.  
  32.  
  33.   /**
  34.    * Adds an exit from this area to the given area. The neighboring area
  35.    * is reached by moving in the specified direction from this area.
  36.    *
  37.    * @param direction  the direction of the exit from this area
  38.    * @param neighbor   the area that the exit leads to
  39.    */
  40.   def setNeighbor(direction: String, neighbor: Area) = {
  41.     this.neighbors += direction -> neighbor
  42.   }
  43.  
  44.  
  45.   /**
  46.    * Adds exits from this area to the given areas.
  47.    * Calling this method is equivalent to calling the `setNeighbor`
  48.    * method on each of the given direction--area pairs.
  49.    *
  50.    * @param exits  pairs of directions and the neighboring areas in that direction
  51.    * @see [[setNeighbor]]
  52.    */
  53.   def setNeighbors(exits: Vector[(String, Area)]) = {
  54.     this.neighbors ++= exits
  55.   }
  56.  
  57.  
  58.   /**
  59.    * Returns a multi-line description of the area as a player sees it.
  60.    * This includes a basic description of the area as well as information
  61.    * about exits and items.
  62.    */
  63.   def fullDescription = {
  64.     val exitList = "\n\nExits available: " + this.neighbors.keys.mkString(" ")
  65.     this.description + exitList + "\n" +
  66.     "You see here: " + this.items.mkString
  67.   }
  68.  
  69.  
  70.   def addItem(item: Item): Unit = {
  71.     this.items += item
  72.   }
  73.  
  74.   def contains(itemName: String): Boolean = items.contains(itemName)
  75.  
  76.   def removeItem(itemName: String): Option[Item] = {
  77.     if (items.contains(itemName)) {
  78.       val toBeRemoved = items.filter( _.name == itemName)
  79.       items = items diff toBeRemoved
  80.       Some(toBeRemoved(0))
  81.     } else None
  82.   }
  83.  
  84.  
  85.  
  86.  
  87.   /**
  88.    * Returns a single-line description of the area for debugging purposes.  
  89.    */
  90.   override def toString = this.name + ": " + this.description.replaceAll("\n", " ").take(150)
  91.  
  92.  
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement