From ababbd45a20be0a2c037175fa2d4c6f18d724fdf Mon Sep 17 00:00:00 2001 From: Zaechus Date: Sun, 12 Jan 2025 18:08:59 -0700 Subject: [PATCH] fix stack overflow caused by parsing again again; fix grammar issue when trying to open a non-container; improve tests --- src/game.rs | 10 +++++++++- src/item.rs | 4 ++-- src/world.ron | 2 +- tests/test.rs | 24 ++++++++++++++++++++---- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/game.rs b/src/game.rs index 2c4013d..9abc5eb 100644 --- a/src/game.rs +++ b/src/game.rs @@ -673,7 +673,7 @@ impl Game { // TODO: equip fn parse(&mut self, action: &Action) -> Outcome { match action { - Action::Again => self.parse(&self.last_command.action().clone()), // FIXME + Action::Again => self.parse_again(), Action::Attack(noun, obj) => Outcome::Active(self.parse_attack(noun, obj)), Action::Break(_) => Outcome::Active("You can't do that yet.".to_owned()), Action::Clarify(message) => Outcome::Idle(message.to_owned()), @@ -700,6 +700,14 @@ impl Game { } } + fn parse_again(&mut self) -> Outcome { + if let Action::Again = self.last_command.action() { + Outcome::Idle("Excuse me?".to_owned()) + } else { + self.parse(&self.last_command.action().clone()) + } + } + fn parse_attack(&mut self, noun: &str, obj: &str) -> String { find!(self, "attack", noun, in_room, obj, holding, attack); diff --git a/src/item.rs b/src/item.rs index e17e9eb..a50d212 100644 --- a/src/item.rs +++ b/src/item.rs @@ -87,7 +87,7 @@ impl Item { Container::Closed => { format!("The {} is already closed.", self.name()) } - _ => format!("You cannot do that to a {}.", self.name()), + _ => format!("You cannot do that to the {}.", self.name()), } } @@ -218,7 +218,7 @@ impl Item { "Opened.".to_owned() } } - _ => format!("You cannot do that to a {}.", self.name()), + _ => format!("You cannot do that to the {}.", self.name()), } } diff --git a/src/world.ron b/src/world.ron index d299e0b..db66ed0 100644 --- a/src/world.ron +++ b/src/world.ron @@ -40,7 +40,7 @@ dest: "BRIG", door: "BRIG DOOR", ), - "ROOT BEER BARRELS": ( // FIXME: english + "ROOT BEER BARRELS": ( desc: "You can detect the slight scent of root beer.", names: ["barrels", "root beer", "crates"], locations: ["HOLD 1", "HOLD 2"], diff --git a/tests/test.rs b/tests/test.rs index e1cdaf2..af82ec8 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -6,8 +6,7 @@ mod tests { fn test() { let mut game: Game = include_str!("world.ron").parse().unwrap(); - let excuse_me = ["", "a", "and", "a and", "and a"]; - for s in excuse_me { + for s in ["", "a", "and", "a and", "and a"] { assert_eq!(game.ask(s), "Excuse me?"); } @@ -19,13 +18,15 @@ mod tests { fn look() { let mut game: Game = include_str!("world.ron").parse().unwrap(); - let expected = "Center Room\nYou are in the center room.\nThere is a box here."; for s in [ "l", // alias "look", // look command "look around", // long form ] { - assert_eq!(game.ask(s), expected); + assert_eq!( + game.ask(s), + "Center Room\nYou are in the center room.\nThere is a box here." + ); } } @@ -101,6 +102,13 @@ mod tests { assert_eq!(game.ask("put apple in apple"), "Impossible."); assert_eq!(game.ask("put box in box"), "Impossible."); + // try to open/close non-container + assert_eq!( + game.ask("open the apple"), + "You cannot do that to the apple." + ); + assert_eq!(game.ask("close apple"), "You cannot do that to the apple."); + // close assert_eq!(game.ask("close box"), "Closed."); @@ -162,4 +170,12 @@ mod tests { "You hit the self with your dagger." ); } + + #[test] + fn again() { + let mut game: Game = include_str!("world.ron").parse().unwrap(); + + game.ask("take it"); + game.ask("again"); // make sure nothing funny happens + } }