-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from KyleFrisbie/3-player-command-module
built out rough input parser taking basic yes no commands, as well as…
- Loading branch information
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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(); | ||
|
||
// 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()); | ||
} |