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/scalevalapokalypsi/Model/Adventure.scala | 79 ++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/scalevalapokalypsi/Model/Adventure.scala (limited to 'src/scalevalapokalypsi/Model/Adventure.scala') diff --git a/src/scalevalapokalypsi/Model/Adventure.scala b/src/scalevalapokalypsi/Model/Adventure.scala new file mode 100644 index 0000000..2bf6cfe --- /dev/null +++ b/src/scalevalapokalypsi/Model/Adventure.scala @@ -0,0 +1,79 @@ +package scalevalapokalypsi.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 + -- cgit v1.2.3