From 4de67b497e0e229fe4a42f66f833640b6e50fd5a Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sun, 17 Nov 2024 13:45:44 +0200 Subject: Moved the project to an IDEA project & wrote part of README.txt --- src/main/scala/Model/Action.scala | 69 --------------------- src/main/scala/Model/Adventure.scala | 79 ----------------------- src/main/scala/Model/Area.scala | 117 ----------------------------------- src/main/scala/Model/Entity.scala | 103 ------------------------------ src/main/scala/Model/Item.scala | 20 ------ 5 files changed, 388 deletions(-) delete mode 100644 src/main/scala/Model/Action.scala delete mode 100644 src/main/scala/Model/Adventure.scala delete mode 100644 src/main/scala/Model/Area.scala delete mode 100644 src/main/scala/Model/Entity.scala delete mode 100644 src/main/scala/Model/Item.scala (limited to 'src/main/scala/Model') diff --git a/src/main/scala/Model/Action.scala b/src/main/scala/Model/Action.scala deleted file mode 100644 index 55f7f27..0000000 --- a/src/main/scala/Model/Action.scala +++ /dev/null @@ -1,69 +0,0 @@ -package o1game.Model - -/** The class `Action` represents actions that a player may take in a text adventure game. - * `Action` objects are constructed on the basis of textual commands and are, in effect, - * parsers for such commands. An action object is immutable after creation. - * @param input a textual in-game command such as “go east” or “rest” */ -class Action(input: String): - - private val commandText = input.trim.toLowerCase - private val verb = commandText.takeWhile( _ != ' ' ) - private val modifiers = commandText.drop(verb.length).trim - - def takesATurnFor(actor: Player): Boolean = - this.verb match - case "rest" => true - case "go" => actor.location.hasNeighbor(modifiers) - case "get" => actor.location.hasItem(this.modifiers) - case "drop" => actor.canDrop(this.modifiers) - case "say" => false - case other => false - - /** Causes the given player to take the action represented by this object, assuming - * that the command was understood. Returns a description of what happened as a result - * of the action (such as “You go west.”). The description is returned in an `Option` - * wrapper; if the command was not recognized, `None` is returned. */ - def execute(actor: Player): Option[String] = - val oldLocation = actor.location - val resOption: Option[(String, String)] = this.verb match - case "go" => Some(actor.go(this.modifiers)) - case "rest" => Some(actor.rest()) - case "get" => Some(actor.pickUp(this.modifiers)) - case "say" => - val to = "to" - val recipient = modifiers.reverse.takeWhile(_ != ' ').reverse - val recipientEntity = actor.location.getEntity(recipient) - val maybeTo = modifiers.slice( - modifiers.length - recipient.length - s"$to ".length, - modifiers.length - recipient.length - 1 - ) - val message = - modifiers.take(modifiers.length - recipient.length - 4) - if maybeTo == to then - recipientEntity.map(actor.sayTo(_, message)) - else - Some(actor.say(modifiers)) - case "drop" => Some(actor.drop(this.modifiers)) - case "xyzzy" => Some(( - "The grue tastes yummy.", - s"${actor.name} tastes some grue.") - ) - case other => None - -// println(resOption) -// println(actor.location.getEntities) - resOption.map(_(1)).filter(_.length > 0) - .foreach(s => - actor.location.getEntities.filter(_ != actor).foreach(_.observe(s)) - if oldLocation != actor.location then - oldLocation.getEntities.foreach(_.observe(s)) - ) - - resOption.map(_(0)) - - - /** Returns a textual description of the action object, for debugging purposes. */ - override def toString = s"$verb (modifiers: $modifiers)" - -end Action - diff --git a/src/main/scala/Model/Adventure.scala b/src/main/scala/Model/Adventure.scala deleted file mode 100644 index dfcb100..0000000 --- a/src/main/scala/Model/Adventure.scala +++ /dev/null @@ -1,79 +0,0 @@ -package o1game.Model - -import scala.collection.mutable.Map - -/** The class `Adventure` represents text adventure games. An adventure consists of a player and - * a number of areas that make up the game world. It provides methods for playing the game one - * turn at a time and for checking the state of the game. - * - * N.B. This version of the class has a lot of “hard-coded” information that pertains to a very - * specific adventure game that involves a small trip through a twisted forest. All newly created - * instances of class `Adventure` are identical to each other. To create other kinds of adventure - * games, you will need to modify or replace the source code of this class. */ -class Adventure(val playerNames: Vector[String]): - - private val middle = Area("Forest", "You are somewhere in the forest. There are a lot of trees here.\nBirds are singing.") - private val northForest = Area("Forest", "You are somewhere in the forest. A tangle of bushes blocks further passage north.\nBirds are singing.") - private val southForest = Area("Forest", "The forest just goes on and on.") - private val clearing = Area("Forest Clearing", "You are at a small clearing in the middle of forest.\nNearly invisible, twisted paths lead in many directions.") - private val tangle = Area("Tangle of Bushes", "You are in a dense tangle of bushes. It's hard to see exactly where you're going.") - private val home = Area("Home", "Home sweet home! Now the only thing you need is a working remote control.") - private val destination = home - - middle.setNeighbors(Vector("north" -> northForest, "east" -> tangle, "south" -> southForest, "west" -> clearing)) - northForest.setNeighbors(Vector("east" -> tangle, "south" -> middle, "west" -> clearing)) - southForest.setNeighbors(Vector("north" -> middle, "east" -> tangle, "south" -> southForest, "west" -> clearing)) - clearing.setNeighbors(Vector("north" -> northForest, "east" -> middle, "south" -> southForest, "west" -> northForest)) - tangle.setNeighbors(Vector("north" -> northForest, "east" -> home, "south" -> southForest, "west" -> northForest)) - home.setNeighbors(Vector("west" -> tangle)) - - clearing.addItem(Item("battery", "It's a small battery cell. Looks new.")) - southForest.addItem(Item( - "remote", - "It's the remote control for your TV.\n" + - "What it was doing in the forest, you have no idea.\n" + - "Problem is, there's no battery." - )) - - val players: Map[String, Player] = Map() - playerNames.foreach(this.addPlayer(_)) - - val entities: Map[String, Entity] = Map() - private val gruu = Entity("Gruu", northForest) - northForest.addEntity(gruu) - this.entities += gruu.name -> gruu - - /** Adds a player entity with the specified name to the game. - * - * @param name the name of the player entity to add - * @return the created player entity - */ - def addPlayer(name: String): Player = - val newPlayer = Player(name, middle) - middle.addEntity(newPlayer) - players += name -> newPlayer - newPlayer - - /** Gets the player entity with the specified name. - * - * @param name name of the player to find - * @return the player, if one with the name was found - */ - def getPlayer(name: String): Option[Player] = this.players.get(name) - - def getEntity[A >: Entity](name: String) = - this.players.getOrElse(name, this.entities.get(name)) - - /** Returns a message that is to be displayed to the player at the beginning of the game. */ - def welcomeMessage = "Generic welcome message" - - /** Plays a turn by executing the given in-game command, such as “go west”. Returns a textual - * report of what happened, or an error message if the command was unknown. In the latter - * case, no turns elapse. */ - def playTurnOfPlayer(playerName: String, command: String): Option[String] = - val action = Action(command) - val actor = this.players.get(playerName) - actor.flatMap(action.execute(_)) - -end Adventure - diff --git a/src/main/scala/Model/Area.scala b/src/main/scala/Model/Area.scala deleted file mode 100644 index 6721957..0000000 --- a/src/main/scala/Model/Area.scala +++ /dev/null @@ -1,117 +0,0 @@ -package o1game.Model - -import scala.collection.mutable.Map - -/** The class `Area` represents locations in a text adventure game world. A game world - * consists of areas. In general, an “area” can be pretty much anything: a room, a building, - * an acre of forest, or something completely different. What different areas have in - * common is that players can be located in them and that they can have exits leading to - * other, neighboring areas. An area also has a name and a description. - * @param name the name of the area - * @param description a basic description of the area (typically not including information about items) */ -class Area(val name: String, var description: String): - - private val neighbors = Map[String, Area]() - private val items: Map[String, Item] = Map() - private val entities: Map[String, Entity] = Map() - - /** Returns the area that can be reached from this area by moving in the given direction. The result - * is returned in an `Option`; `None` is returned if there is no exit in the given direction. */ - def neighbor(direction: String): Option[Area] = - this.neighbors.get(direction) - - def getNeighborNames: Iterable[String] = this.neighbors.keys - def getItemNames: Iterable[String] = this.items.keys - def getEntityNames: Iterable[String] = this.entities.values.map(_.name) - def getEntity(name: String): Option[Entity] = this.entities.get(name) - def getEntities: Iterable[Entity] = this.entities.values - - /** Tells whether this area has a neighbor in the given direction. - * - * @param direction the direction to check - * @return whether there is a neighbor in the direction - */ - def hasNeighbor(direction: String): Boolean = - this.neighbors.contains(direction) - - /** Adds an exit from this area to the given area. The neighboring area is reached by moving in - * the specified direction from this area. */ - def setNeighbor(direction: String, neighbor: Area) = - this.neighbors += direction -> neighbor - - /** Adds exits from this area to the given areas. Calling this method is equivalent to calling - * the `setNeighbor` method on each of the given direction–area pairs. - * @param exits contains pairs consisting of a direction and the neighboring area in that direction - * @see [[setNeighbor]] */ - def setNeighbors(exits: Vector[(String, Area)]) = - this.neighbors ++= exits - - /** Adds the specified item - * - * @param item the item to add - */ - def addItem(item: Item): Unit = this.items += item.name -> item - - /** Adds multiple items - * - * @param items a once iterable collection of items to add - */ - def addItems(items: IterableOnce[Item]) = - items.iterator.foreach(i => this.items += i.name -> i) - - def hasItem(itemName: String) = this.items.contains(itemName) - - - /** Removes the specified item if it exists. - * - * @param itemName the name of the item to remove - * @return an option containing the removed item - */ - def removeItem(itemName: String): Option[Item] = - this.items.remove(itemName) - - /** Adds the specified entity to the area. - * - * @param entity the entity to add. - */ - def addEntity(entity: Entity): Unit = - this.entities += entity.name.toLowerCase -> entity - - /** Removes the entity with the name `entityName`. - * - * @param entityName the name of the entity to remove - * @return an option containing the removed entity if it was in the area - */ - def removeEntity(entityName: String): Option[Entity] = - this.entities.remove(entityName.toLowerCase()) - - /** Returns a multi-line description of the area as a player sees it. This includes a basic - * description of the area as well as information about exits and items. If there are no - * items present, the return value has the form "DESCRIPTION\n\nExits available: - * DIRECTIONS SEPARATED BY SPACES". If there are one or more items present, the return - * value has the form "DESCRIPTION\nYou see here: ITEMS SEPARATED BY SPACES\n\nExits available: - * DIRECTIONS SEPARATED BY SPACES". The items and directions are listed in an arbitrary order. */ - def fullDescription: String = - val exitList = this.neighbors.keys.mkString(" ") - val itemList = this.items.keys.mkString(" ") - val entityList = this.getEntityNames.mkString(" ") - val itemDescription = - if this.items.nonEmpty then - s"\nYou see here: ${itemList}" - else "" - val entityDescription = - if this.entities.nonEmpty then - s"\nThere are entities: ${entityList}" - else "" - (this.description + - itemDescription + - entityDescription + - s"\n\nExits available: $exitList") - - - /** Returns a single-line description of the area for debugging purposes. */ - override def toString = - this.name + ": " + this.description.replaceAll("\n", " ").take(150) - -end Area - diff --git a/src/main/scala/Model/Entity.scala b/src/main/scala/Model/Entity.scala deleted file mode 100644 index d8e8559..0000000 --- a/src/main/scala/Model/Entity.scala +++ /dev/null @@ -1,103 +0,0 @@ -package o1game.Model - -import scala.collection.mutable.{Buffer,Map} - - - -/** A `Player` object represents a player character controlled by one real-life player - * of the program. - * - * A player object’s state is mutable: the player’s location and possessions can change, - * for instance. - * - * @param startingArea the player’s initial location */ -class Player(name: String, initialLocation: Area) extends Entity(name, initialLocation): - - private val observations: Buffer[String] = Buffer.empty - - override def observe(observation: String): Unit = - this.observations.append(observation) - - def readAndClearObservations(): Vector[String] = - val res = this.observations.toVector - observations.clear() - res - -end Player - -/** An in-game entity. - * - * @param name the name of the entity - * @param initialLocation the Area where the entity is instantiated - */ -class Entity(val name: String, initialLocation: Area): - private var currentLocation: Area = initialLocation - private var quitCommandGiven = false // one-way flag - private val inventory: Map[String, Item] = Map() - - /** Determines if the player has indicated a desire to quit the game. */ - def hasQuit = this.quitCommandGiven // TODO: This is probably unneccessary? - - /** Does nothing, except possibly in inherited classes. */ - def observe(observation: String): Unit = - println("no observation made.") - () - - /** Returns the player’s current location. */ - 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 direction name. Returns - * a description of the result: "You go DIRECTION." or "You can't go DIRECTION." */ - def go(direction: String): (String, String) = - val destination = this.location.neighbor(direction) - if destination.isDefined then - val removeSuccess = this.currentLocation.removeEntity(this.name) - assert(removeSuccess.isDefined) // Production - assertions off - this.currentLocation = destination.getOrElse(this.currentLocation) - destination.foreach(_.addEntity(this)) - (s"You go $direction.", s"$name goes $direction") - else - ( - s"You can't go $direction.", - s"$name tries to go $direction and stumbles in their feet." - ) - - def pickUp(itemName: String): (String, String) = - this.currentLocation.removeItem(itemName) match - case Some(i) => - this.inventory += i.name -> i - (s"You pick up the ${i.name}", s"$name picks up the ${i.name}") - case None => (s"There is no $itemName here to pick up.", "WHAAAT THIS SHOULDN'T HAPPEN???") - - def drop(itemName: String): (String, String) = - this.inventory.remove(itemName) match - case Some(item) => - this.currentLocation.addItem(item) - (s"You drop the $itemName", s"$name drops the $itemName") - case None => ("You don't have that!", s"$name reaches their backpack to drop $itemName but miserably fails to find it there.") - - def sayTo(entity: Entity, message: String): (String, String) = - entity.observe(s"Alice: \"$message\"") - (s"You say so to ${entity.name}.", "") - - def say(message: String): (String, String) = - ("You say that aloud.", s"$name: \"$message\"") - - /** Tells whether this entity can drop the specified item - * (if an action were to specify so). - * - * @param itemName the name to check - * @return whether this entity has this item and can drop it - */ - def canDrop(itemName: String): Boolean = this.inventory.contains(itemName) - - /** Causes the player to rest for a short while (this has no substantial effect in game terms). - * Returns a description of what happened. */ - def rest(): (String, String) = - ("You rest for a while. Better get a move on, though.", "") - - /** Returns a brief description of the player’s state, for debugging purposes. */ - override def toString = "Now at: " + this.location.name - -end Entity diff --git a/src/main/scala/Model/Item.scala b/src/main/scala/Model/Item.scala deleted file mode 100644 index 229828d..0000000 --- a/src/main/scala/Model/Item.scala +++ /dev/null @@ -1,20 +0,0 @@ -package o1game.Model - -import scala.annotation.targetName - -/** The class `Item` represents items in a text adventure game. Each item has a name - * and a longer description. (In later versions of the adventure game, items may - * have other features as well.) - * - * N.B. It is assumed, but not enforced by this class, that items have unique names. - * That is, no two items in a game world have the same name. - * - * @param name the item’s name - * @param description the item’s description */ -class Item(val name: String, val description: String): - - /** Returns a short textual representation of the item (its name, that is). */ - override def toString = this.name - -end Item - -- cgit v1.2.3