Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package o1.adventure
- import scala.collection.mutable._
- /**
- * A `Player` object represents a player character controlled by the real-life user of the program.
- *
- * A player object's state is mutable: the player's location and possessions can change, for instance.
- *
- * @param startingArea the initial location of the player
- */
- class Player(startingArea: Area) {
- private var currentLocation = startingArea // gatherer: changes in relation to the previous location
- private var quitCommandGiven = false // one-way flag
- private var inventory = ArrayBuffer[Item]()
- /**
- * Determines if the player has indicated a desire to quit the game.
- */
- def hasQuit = this.quitCommandGiven
- /**
- * Returns the current location of the player.
- */
- def location = this.currentLocation
- /**
- * Attempts to move the player in the given direction. This is successful if there
- * is an exit from the player's current location towards the given direction.
- *
- * @param direction a direction name (may be a nonexistent direction)
- * @return a description of the results of the attempt
- */
- def go(direction: String) = {
- val destination = this.location.neighbor(direction)
- this.currentLocation = destination.getOrElse(this.currentLocation)
- if (destination.isDefined) "You go " + direction + "." else "You can't go " + direction + "."
- }
- /**
- * Causes the player to rest for a short while (this has no substantial effect in game terms).
- *
- * @return a description of what happened
- */
- def rest() = {
- "You rest for a while. Better get a move on, though."
- }
- /**
- * Signals that the player wants to quit the game.
- *
- * @return a description of what happened within the game as a result (which is the empty string, in this case)
- */
- def quit() = {
- this.quitCommandGiven = true
- ""
- }
- def drop(itemName: String): String = {
- if(this.inventory.contains(itemName)) {
- val toBeRemoved = inventory.filter( _.name == itemName)
- inventory = inventory diff toBeRemoved
- "You drop the " +
- toBeRemoved(0).toString() +
- "."
- } else {
- "You don't have that!"
- }
- }
- def examine(itemName: String): String = {
- if(this.has(itemName)){
- val the_item: Item = inventory.filter( _.name == itemName)(0)
- val first_line = "You look closely at " + itemName + ".\n"
- first_line + the_item.description
- }else{
- "If you want to examine something, you need to pick it up first."
- }
- }
- def get(itemName: String): String = {
- if(this.currentLocation.contains(itemName)) {
- "You pick up the " +
- get(this.currentLocation.removeItem(itemName).toString()) +
- "."
- } else {
- "There is no " + itemName + " here to pick up."
- }
- }
- def has(itemName: String): Boolean = this.inventory.contains(itemName)
- def makeInventory(): String = {
- if (this.inventory.isEmpty) {
- "The inventory is empty."
- } else {
- "You are carrying:\n" +
- inventory.mkString("\n")
- }
- }
- /*
- * def removeItem(itemName: String): Option[Item] = {
- if (items.contains(itemName)) {
- val toBeRemoved = items.filter( _.name == itemName)
- items = items diff toBeRemoved
- Some(toBeRemoved(0))
- } else None
- }
- */
- /**
- * Returns a brief description of the player's state, for debugging purposes.
- */
- override def toString = "Now at: " + this.location.name
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement