_chef kiss_
parent
7b8a347ab5
commit
1ddb5ffe18
76
src/main.rs
76
src/main.rs
|
@ -183,15 +183,31 @@ fn parse_item_name (s: &str) -> ItemName {
|
||||||
struct StateRoom1 {
|
struct StateRoom1 {
|
||||||
detected_keypad: bool,
|
detected_keypad: bool,
|
||||||
detected_note: bool,
|
detected_note: bool,
|
||||||
|
read_note: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (PartialEq)]
|
#[derive (PartialEq)]
|
||||||
enum Response {
|
enum Response {
|
||||||
|
// These are handled by the runtime
|
||||||
Print (String),
|
Print (String),
|
||||||
PrintMany (Vec <&'static str>),
|
PrintMany (Vec <&'static str>),
|
||||||
Sleep (u32),
|
Sleep (u32),
|
||||||
Quit,
|
Quit,
|
||||||
|
|
||||||
|
// These are just useful markers for the automated tests
|
||||||
JokeEnding,
|
JokeEnding,
|
||||||
|
FailedDetectionCheck,
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! require_detection {
|
||||||
|
($condition:expr $(,)?) => {
|
||||||
|
if ! $condition {
|
||||||
|
return vec! [
|
||||||
|
Response::FailedDetectionCheck,
|
||||||
|
undetected_item (),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive (Clone, Default)]
|
#[derive (Clone, Default)]
|
||||||
|
@ -200,6 +216,10 @@ struct State {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
|
fn step (&mut self, input: &str) -> Vec <Response> {
|
||||||
|
self.room_1 (input)
|
||||||
|
}
|
||||||
|
|
||||||
fn room_1 (&mut self, input: &str) -> Vec <Response> {
|
fn room_1 (&mut self, input: &str) -> Vec <Response> {
|
||||||
use 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."))
|
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 => {
|
ItemName::Keypad => {
|
||||||
if ! self.room_1.detected_keypad {
|
require_detection! (self.room_1.detected_keypad);
|
||||||
return just (undetected_item ());
|
|
||||||
}
|
|
||||||
|
|
||||||
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."))
|
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 => {
|
ItemName::Note => {
|
||||||
if ! self.room_1.detected_note {
|
require_detection! (self.room_1.detected_note);
|
||||||
return vec! [
|
|
||||||
undetected_item (),
|
self.room_1.read_note = true;
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
just (Response::PrintMany (vec! [
|
just (Response::PrintMany (vec! [
|
||||||
"You read the NOTE.",
|
"You read the NOTE.",
|
||||||
|
@ -319,6 +335,23 @@ impl State {
|
||||||
Response::Quit,
|
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! [
|
vec! [
|
||||||
undetected_item (),
|
undetected_item (),
|
||||||
|
@ -355,7 +388,7 @@ fn game () -> Result <()> {
|
||||||
|
|
||||||
'main_loop: loop {
|
'main_loop: loop {
|
||||||
let input = read_input ()?;
|
let input = read_input ()?;
|
||||||
let responses = state.room_1 (&input);
|
let responses = state.step (&input);
|
||||||
|
|
||||||
for response in responses.into_iter () {
|
for response in responses.into_iter () {
|
||||||
match response {
|
match response {
|
||||||
|
@ -383,6 +416,11 @@ fn main () -> Result <()> {
|
||||||
|
|
||||||
#[cfg (test)]
|
#[cfg (test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use super::{
|
||||||
|
Response,
|
||||||
|
State,
|
||||||
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_input () {
|
fn parse_input () {
|
||||||
use super::{
|
use super::{
|
||||||
|
@ -407,16 +445,24 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn joke_ending () {
|
fn joke_ending () {
|
||||||
use super::{
|
|
||||||
Response,
|
|
||||||
State,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut state = State::default ();
|
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::Quit));
|
||||||
assert! (responses.contains (&Response::JokeEnding));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue