mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-24 20:10:15 -08:00
direction distance calculator
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use rand::Rng;
|
||||
|
||||
use crate::{model::*, view, input::{self, param_or_prompt_value}};
|
||||
use crate::{model::*, view, input::{self, param_or_prompt_value, prompt_two_values}};
|
||||
|
||||
pub fn perform_short_range_scan(galaxy: &Galaxy) {
|
||||
if galaxy.enterprise.damaged.contains_key(systems::SHORT_RANGE_SCAN) {
|
||||
@@ -313,6 +313,7 @@ pub fn access_computer(galaxy: &Galaxy, provided: Vec<String>) {
|
||||
run_damage_control(galaxy);
|
||||
},
|
||||
3 => show_starbase_data(galaxy),
|
||||
4 => direction_dist_calculator(galaxy),
|
||||
5 => view::galaxy_region_map(),
|
||||
_ => todo!() // todo implement others
|
||||
}
|
||||
@@ -334,6 +335,24 @@ fn show_starbase_data(galaxy: &Galaxy) {
|
||||
}
|
||||
}
|
||||
|
||||
fn direction_dist_calculator(galaxy: &Galaxy) {
|
||||
view::direction_dist_intro(&galaxy.enterprise);
|
||||
loop {
|
||||
let coords1 = prompt_two_values(view::prompts::INITIAL_COORDS, 1, 8).map(|(x, y)| Pos(x, y));
|
||||
if coords1.is_none() {
|
||||
continue;
|
||||
}
|
||||
let coords2 = prompt_two_values(view::prompts::TARGET_COORDS, 1, 8).map(|(x, y)| Pos(x, y));
|
||||
if coords2.is_none() {
|
||||
continue;
|
||||
}
|
||||
let dir = coords1.unwrap().direction(coords2.unwrap());
|
||||
let dist = coords1.unwrap().dist(coords2.unwrap());
|
||||
view::direction_distance(dir, dist);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_power_and_fire_phasers(galaxy: &mut Galaxy, provided: Vec<String>) {
|
||||
if galaxy.enterprise.damaged.contains_key(systems::PHASERS) {
|
||||
view::inoperable(&systems::name_for(systems::PHASERS));
|
||||
|
||||
@@ -9,7 +9,7 @@ pub fn prompt(prompt_text: &str) -> Vec<String> {
|
||||
|
||||
let mut buffer = String::new();
|
||||
if let Ok(_) = stdin.read_line(&mut buffer) {
|
||||
return buffer.trim_end().split(" ").map(|s| s.to_string()).collect();
|
||||
return buffer.trim_end().split([' ', ',']).map(|s| s.to_string()).collect();
|
||||
}
|
||||
Vec::new()
|
||||
}
|
||||
@@ -53,4 +53,21 @@ pub fn param_or_prompt_value<T: FromStr + PartialOrd>(params: &Vec<String>, para
|
||||
return res;
|
||||
}
|
||||
return prompt_value::<T>(prompt_text, min, max);
|
||||
}
|
||||
|
||||
pub fn prompt_two_values<T: FromStr + PartialOrd>(prompt_text: &str, min: T, max: T) -> Option<(T, T)> {
|
||||
let passed = prompt(prompt_text);
|
||||
if passed.len() != 2 {
|
||||
return None
|
||||
}
|
||||
match passed[0].parse::<T>() {
|
||||
Ok(n1) if (n1 >= min && n1 <= max) => {
|
||||
match passed[1].parse::<T>() {
|
||||
Ok(n2) if (n2 >= min && n2 <= max) =>
|
||||
Some((n1, n2)),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ pub mod prompts {
|
||||
pub const PHASERS: &str = "Number of units to fire";
|
||||
pub const WHEN_READY: &str = "Press Enter when ready to accept command";
|
||||
pub const COMMAND: &str = "Command?";
|
||||
pub const INITIAL_COORDS: &str = " Initial coordinates (X/Y)";
|
||||
pub const TARGET_COORDS: &str = " Final coordinates (X/Y)";
|
||||
|
||||
pub fn warp_factor(max_warp: f32) -> String {
|
||||
format!("Warp Factor (0-{})?", max_warp)
|
||||
@@ -610,4 +612,12 @@ Klingons left: {klingon_count}
|
||||
Mission must be completed in {time_remaining} stardates.
|
||||
The Federation is maintaining {star_bases} starbase{plural_starbase} in the galaxy.
|
||||
")
|
||||
}
|
||||
|
||||
pub fn direction_dist_intro(enterprise: &Enterprise) {
|
||||
let quadrant = enterprise.quadrant;
|
||||
let sector = enterprise.sector;
|
||||
println!("Direction/distance calculator:
|
||||
You are at quadrant {quadrant} sector {sector}
|
||||
Please enter")
|
||||
}
|
||||
@@ -33,7 +33,7 @@ Started after movement and display of stats was finished (no energy management o
|
||||
- [x] 1 - klingons, starbases, stardate and damage control
|
||||
- [ ] 2 - photon torpedo data: direction and distance to all local klingons
|
||||
- [x] 3 - starbase distance and dir locally
|
||||
- [ ] 4 - direction/distance calculator (useful for nav actions I guess)
|
||||
- [x] 4 - direction/distance calculator (useful for nav actions I guess)
|
||||
- [x] 5 - galactic name map
|
||||
|
||||
- [x] restarting the game
|
||||
|
||||
Reference in New Issue
Block a user