aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Model/Action.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-07 01:17:58 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-07 01:17:58 +0200
commitfdaec6534eb7ee902c75be3e4b732b6970abd859 (patch)
treef323d11dc1a33a93ee44417a58df62ef5f9ad98a /src/main/scala/Model/Action.scala
parent12cbf4d451d1002b8872b0028acbe6bd886ca9bd (diff)
downloadscalevalapokalypsi-fdaec6534eb7ee902c75be3e4b732b6970abd859.tar.gz
scalevalapokalypsi-fdaec6534eb7ee902c75be3e4b732b6970abd859.zip
Made the server work with the adventure model
* The model was imported from the wrong version, so that needs to be fixed. * The client side doesn't work at all right now. Use netcat for testing. * There are inconveniences and bugs in the model (eg. it lists the player among entities) * Players can just see each other, not interact in any way But it's a good base.
Diffstat (limited to 'src/main/scala/Model/Action.scala')
-rw-r--r--src/main/scala/Model/Action.scala31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/main/scala/Model/Action.scala b/src/main/scala/Model/Action.scala
new file mode 100644
index 0000000..84704ce
--- /dev/null
+++ b/src/main/scala/Model/Action.scala
@@ -0,0 +1,31 @@
+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
+
+ /** 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: Entity): Option[String] =
+ this.verb match
+ case "go" => Some(actor.go(this.modifiers))
+ case "rest" => Some(actor.rest())
+ case "get" => Some(actor.pickUp(this.modifiers))
+ case "drop" => Some(actor.drop(this.modifiers))
+ case "xyzzy" => Some("The grue tastes yummy.")
+ case "quit" => Some(actor.quit())
+ case other => None
+
+ /** Returns a textual description of the action object, for debugging purposes. */
+ override def toString = s"$verb (modifiers: $modifiers)"
+
+end Action
+