Skip to content

Commit

Permalink
built out rough input parser taking basic yes no commands, as well as…
Browse files Browse the repository at this point in the history
… the command move
  • Loading branch information
KyleFrisbie committed Aug 21, 2022
1 parent a3d077d commit 4034892
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ authors = [
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "3.2.17", features = ["derive"] }
7 changes: 6 additions & 1 deletion src/main.rs
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();
}
22 changes: 22 additions & 0 deletions src/story_manager/mod.rs
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()));
}
62 changes: 62 additions & 0 deletions src/user_command/mod.rs
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());
}

0 comments on commit 4034892

Please sign in to comment.