1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
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]):
/** the name of the game */
val title = "A Forest Adventure"
/*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, Entity] = Map()
playerNames.foreach(name => players += name -> Entity(name, middle))
def getPlayer(name: String): Option[Entity] = this.players.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
|