_chef kiss_

main
_ 2021-10-24 22:37:10 +00:00
parent 7b8a347ab5
commit 1ddb5ffe18
1 changed files with 61 additions and 15 deletions

View File

@ -183,15 +183,31 @@ fn parse_item_name (s: &str) -> ItemName {
struct StateRoom1 {
detected_keypad: bool,
detected_note: bool,
read_note: bool,
}
#[derive (PartialEq)]
enum Response {
// These are handled by the runtime
Print (String),
PrintMany (Vec <&'static str>),
Sleep (u32),
Quit,
// These are just useful markers for the automated tests
JokeEnding,
FailedDetectionCheck,
}
macro_rules! require_detection {
($condition:expr $(,)?) => {
if ! $condition {
return vec! [
Response::FailedDetectionCheck,
undetected_item (),
];
}
};
}
#[derive (Clone, Default)]
@ -200,6 +216,10 @@ struct State {
}
impl State {
fn step (&mut self, input: &str) -> Vec <Response> {
self.room_1 (input)
}
fn room_1 (&mut self, input: &str) -> Vec <Response> {
use Response::*;
@ -251,18 +271,14 @@ impl State {
just (line_response ("The EMERGENCY EXIT reads, \"Emergency exit. Push bar to open. Alarm will sound. Door will unlock in 10 seconds.\". The EMERGENCY EXIT is period-accurate for an American Wal-Mart c. 2020 C.E."))
},
ItemName::Keypad => {
if ! self.room_1.detected_keypad {
return just (undetected_item ());
}
require_detection! (self.room_1.detected_keypad);
just (line_response ("The DOOR is locked by an electronic KEYPAD. A soft amber power light indicates that the KEYPAD is likely functional. The KEYPAD buttons are the digits 0-9, Enter, and Clear. Experience tells you that the key code is likely 4 or 5 digits long."))
},
ItemName::Note => {
if ! self.room_1.detected_note {
return vec! [
undetected_item (),
];
}
require_detection! (self.room_1.detected_note);
self.room_1.read_note = true;
just (Response::PrintMany (vec! [
"You read the NOTE.",
@ -319,6 +335,23 @@ impl State {
Response::Quit,
]
},
ItemName::Keypad => {
require_detection! (self.room_1.detected_keypad);
if ! self.room_1.read_note {
return just (line_response ("You can't USE the KEYPAD, you don't know the code for it. You would normally try guessing, but it would take the programmer all day to implement that level of interaction."));
}
panic! ("You USE the code on the KEYPAD. The door opens, and the game immediately crashes.");
},
ItemName::Note => {
require_detection! (self.room_1.detected_note);
just (line_response ("You can't think of any way to USE the NOTE that would be better than LOOKing at it to read it."))
},
ItemName::Table => {
just (line_response ("You can't think of any way to USE the TABLE that would be better than LOOKing at it."))
},
_ => {
vec! [
undetected_item (),
@ -355,7 +388,7 @@ fn game () -> Result <()> {
'main_loop: loop {
let input = read_input ()?;
let responses = state.room_1 (&input);
let responses = state.step (&input);
for response in responses.into_iter () {
match response {
@ -383,6 +416,11 @@ fn main () -> Result <()> {
#[cfg (test)]
mod test {
use super::{
Response,
State,
};
#[test]
fn parse_input () {
use super::{
@ -407,16 +445,24 @@ mod test {
#[test]
fn joke_ending () {
use super::{
Response,
State,
};
let mut state = State::default ();
let responses = state.room_1 ("use emergency exit");
let responses = state.step ("use emergency exit");
assert! (responses.contains (&Response::Quit));
assert! (responses.contains (&Response::JokeEnding));
}
#[test]
fn detection_check () {
let mut state = State::default ();
let responses = state.step ("look keypad");
assert! (responses.contains (&Response::FailedDetectionCheck));
state.step ("look door");
let responses = state.step ("look keypad");
assert! (! responses.contains (&Response::FailedDetectionCheck));
}
}