mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
work on nav command under new model
This commit is contained in:
@@ -1,21 +1,25 @@
|
|||||||
use std::{io::{stdin, stdout, Write}, process::exit};
|
use std::{io::{stdin, stdout, Write}, process::exit, str::FromStr};
|
||||||
|
|
||||||
use model::Galaxy;
|
use model::Galaxy;
|
||||||
|
|
||||||
|
use crate::text_constants::BAD_NAV;
|
||||||
|
|
||||||
mod model;
|
mod model;
|
||||||
mod commands;
|
mod commands;
|
||||||
|
mod text_constants;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
ctrlc::set_handler(move || { exit(0) })
|
ctrlc::set_handler(move || { exit(0) })
|
||||||
.expect("Error setting Ctrl-C handler");
|
.expect("Error setting Ctrl-C handler");
|
||||||
|
|
||||||
let galaxy = Galaxy::generate_new();
|
let mut galaxy = Galaxy::generate_new();
|
||||||
// init ops, starting state and notes
|
// init ops, starting state and notes
|
||||||
commands::short_range_scan(&galaxy);
|
commands::short_range_scan(&galaxy);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match prompt("Command?").as_str() {
|
match prompt("Command?").as_str() {
|
||||||
"SRS" => commands::short_range_scan(&galaxy),
|
"SRS" => commands::short_range_scan(&galaxy),
|
||||||
|
"NAV" => gather_dir_and_speed_then_move(&mut galaxy),
|
||||||
_ => print_command_help()
|
_ => print_command_help()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,11 +29,31 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prompt(prompt: &str) -> String {
|
fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) {
|
||||||
|
let dir = prompt_value::<u8>("Course (1-9)?", 1, 9);
|
||||||
|
if dir.is_none() {
|
||||||
|
println!("{}", BAD_NAV);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let speed = prompt_value::<f32>("Course (1-9)?", 0.0, 8.0);
|
||||||
|
if speed.is_none() {
|
||||||
|
println!("{}", BAD_NAV);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let distance = (speed.unwrap() * 8.0) as i32;
|
||||||
|
// could be done with a step function - while distance > 0, move by digit.
|
||||||
|
// if passing a boundary, test for the next quadrant in that direction
|
||||||
|
// if present, change quadrant and move to border
|
||||||
|
// else stop.
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prompt(prompt_text: &str) -> String {
|
||||||
let stdin = stdin();
|
let stdin = stdin();
|
||||||
let mut stdout = stdout();
|
let mut stdout = stdout();
|
||||||
|
|
||||||
print!("{prompt} ");
|
print!("{prompt_text} ");
|
||||||
let _ = stdout.flush();
|
let _ = stdout.flush();
|
||||||
|
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
@@ -39,11 +63,15 @@ fn prompt(prompt: &str) -> String {
|
|||||||
"".into()
|
"".into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn prompt_value<T: FromStr + PartialOrd>(prompt_text: &str, min: T, max: T) -> Option<T> {
|
||||||
|
let passed = prompt(prompt_text);
|
||||||
|
match passed.parse::<T>() {
|
||||||
|
Ok(n) if (n >= min && n <= max) => Some(n),
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn print_command_help() {
|
fn print_command_help() {
|
||||||
println!("valid commands are just SRS and NAV at the mo")
|
println!("valid commands are just SRS and NAV at the mo")
|
||||||
}
|
}
|
||||||
|
|
||||||
// match text.parse::<u8>() {
|
|
||||||
// Ok(n) if (n >= 1 && n <= 8) => Some(Message::DirectionForNav(n)),
|
|
||||||
// _ => None
|
|
||||||
// }
|
|
||||||
2
84_Super_Star_Trek/rust/src/text_constants.rs
Normal file
2
84_Super_Star_Trek/rust/src/text_constants.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
pub const BAD_NAV: &str = " Lt. Sulu reports, 'Incorrect course data, sir!'";
|
||||||
Reference in New Issue
Block a user