-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
built out rough input parser taking basic yes no commands, as well as… #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
mod story_manager; | ||
use story_manager::{introduction, tutorial}; | ||
mod user_command; | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
introduction(); | ||
tutorial(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use crate::user_command::{ | ||
get_user_input, get_valid_action, get_valid_response, VALID_AFFIRMATIVE_RESPONSES, | ||
}; | ||
|
||
pub fn introduction() { | ||
println!("Welcome to Dust Plight!") | ||
} | ||
|
||
pub fn tutorial() { | ||
// General Tutorial Intro | ||
println!("This tutorial will guide you through a basic scenario, like you might encounter while playing the game. | ||
// \nIn each state of the game, you will be presented with an environment described to you based on your current position. It will be up to you to determine your next action based on this description. Are you interested in running the tutorial?"); | ||
|
||
|
||
let response = get_user_input(&|input| get_valid_response(input.to_string())); | ||
if VALID_AFFIRMATIVE_RESPONSES.contains(&response.as_str()) { | ||
println!("Great! Let's get started with the tutorial.") | ||
} | ||
|
||
println!("Excellent! Now that you're ready to go, let's practice some movement."); | ||
get_user_input(&|input| get_valid_action(input.to_string())); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::io::{self, BufRead}; | ||
|
||
use clap::Parser; | ||
|
||
pub static VALID_AFFIRMATIVE_RESPONSES: [&str; 2] = ["yes", "y"]; | ||
pub static VALID_NEGATIVE_RESPONSES: [&str; 2] = ["no", "n"]; | ||
static VALID_ACTIONS: [&str; 1] = ["move"]; | ||
|
||
pub fn get_valid_response(input: String) -> (String, bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are awesome helper funcs, I like the way it's organized |
||
let response_is_valid = is_valid(&input, &VALID_AFFIRMATIVE_RESPONSES) | ||
|| is_valid(&input, &VALID_NEGATIVE_RESPONSES); | ||
if response_is_valid { | ||
return (input, true); | ||
} | ||
return ( | ||
format!( | ||
"Please provide a valid response: {}, {}.", | ||
VALID_AFFIRMATIVE_RESPONSES.join(", "), | ||
VALID_NEGATIVE_RESPONSES.join(", ") | ||
), | ||
false, | ||
); | ||
} | ||
|
||
pub fn get_valid_action(input: String) -> (String, bool) { | ||
let action_is_valid = is_valid(&input, &VALID_ACTIONS); | ||
if action_is_valid { | ||
return (input, true); | ||
} | ||
return ( | ||
format!( | ||
"Please provide a valid action: {}.", | ||
VALID_ACTIONS.join(", ") | ||
), | ||
false, | ||
); | ||
} | ||
|
||
pub fn get_user_input(f: &dyn Fn(&String) -> (String, bool)) -> String { | ||
let stdin = io::stdin(); | ||
|
||
let mut buffer = String::with_capacity(2048); | ||
// Lock our standard input to eliminate synchronization overhead (unlocks when dropped) | ||
let mut stdin = io::stdin().lock(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious about this, and maybe I just haven't learned enough about Rust yet, but - line 40 defines an immutable variable, right? but then line 44 defines the same variable name, but mutable, so is this still a "shadow" variable since the first one was supposed to be immutable? |
||
|
||
// Read our first line. | ||
stdin.read_line(&mut buffer); | ||
|
||
let mut result = f(&buffer); | ||
while !result.1 { | ||
println!("{}", result.0); | ||
stdin.read_line(&mut buffer); | ||
result = f(&buffer); | ||
buffer.clear(); | ||
} | ||
|
||
return result.0; | ||
} | ||
|
||
fn is_valid(command: &str, valid_commands: &[&str]) -> bool { | ||
return valid_commands.contains(&command.trim()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love how this is organized and modular too 👌