Skip to content
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

Merged
merged 1 commit into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Collaborator

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 👌

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
Copy link
Collaborator

Choose a reason for hiding this comment

The 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());
}