From 80ac05e0053f9a38eba2b8e0177afe81077cc9fb Mon Sep 17 00:00:00 2001 From: Christopher Date: Wed, 1 Mar 2023 08:54:39 +1300 Subject: [PATCH] semi working nav (going weird directions) --- 84_Super_Star_Trek/rust/src/main.rs | 24 +++++++++++---------- 84_Super_Star_Trek/rust/src/model.rs | 31 +++++++++++----------------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/84_Super_Star_Trek/rust/src/main.rs b/84_Super_Star_Trek/rust/src/main.rs index 481d2378..a7265116 100644 --- a/84_Super_Star_Trek/rust/src/main.rs +++ b/84_Super_Star_Trek/rust/src/main.rs @@ -2,6 +2,8 @@ use std::{io::{stdin, stdout, Write}, process::exit, str::FromStr}; use model::{Galaxy, Pos}; +use crate::model::DIRECTIONS; + mod model; mod commands; @@ -14,7 +16,7 @@ fn main() { commands::short_range_scan(&galaxy); loop { - match prompt("Command?").as_str() { + match prompt("Command?").to_uppercase().as_str() { "SRS" => commands::short_range_scan(&galaxy), "NAV" => gather_dir_and_speed_then_move(&mut galaxy), _ => print_command_help() @@ -28,22 +30,25 @@ fn main() { fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) { const BAD_NAV: &str = " Lt. Sulu reports, 'Incorrect course data, sir!'"; - + let dir = prompt_value::("Course (1-9)?", 1, 9); if dir.is_none() { println!("{}", BAD_NAV); return; } - let speed = prompt_value::("Course (1-9)?", 0.0, 8.0); + let speed = prompt_value::("Warp Factor (0-8)?", 0.0, 8.0); if speed.is_none() { println!("{}", BAD_NAV); return; } - let distance = (speed.unwrap() * 8.0) as u8; + let distance = (speed.unwrap() * 8.0) as i8; let galaxy_pos = galaxy.enterprise.quadrant * 8u8 + galaxy.enterprise.sector; - let (mut nx, mut ny) = galaxy_pos.translate(dir.unwrap(), distance); + + let (dx, dy): (i8, i8) = DIRECTIONS[(dir.unwrap() - 1) as usize]; + let mut nx = (galaxy_pos.0 as i8) + dx * distance; + let mut ny = (galaxy_pos.1 as i8) + dy * distance; let mut hit_edge = false; if nx < 0 { @@ -77,12 +82,9 @@ fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) { galaxy.enterprise.quadrant = new_quadrant; galaxy.enterprise.sector = new_sector; - // 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. - // one way to sort this would be to convert current pos to a galaxy pos (e.g. sector.x, y * 8), - // add dist, then mod/divide to get quadrant and new sector + // if new_quadrant isnt old quadrant print intro + + commands::short_range_scan(&galaxy) } fn prompt(prompt_text: &str) -> String { diff --git a/84_Super_Star_Trek/rust/src/model.rs b/84_Super_Star_Trek/rust/src/model.rs index 36ec9628..2e097b9c 100644 --- a/84_Super_Star_Trek/rust/src/model.rs +++ b/84_Super_Star_Trek/rust/src/model.rs @@ -11,27 +11,9 @@ pub struct Galaxy { pub struct Pos(pub u8, pub u8); impl Pos { - const DIRECTIONS : [(i8, i8); 8] = [ - (1, 0), - (1, -1), - (0, -1), - (-1, -1), - (-1, 0), - (-1, 1), - (0, 1), - (1, 1), - ]; - pub fn as_index(&self) -> usize { (self.0 * 8 + self.1).into() } - - pub fn translate(&self, dir: u8, dist: u8) -> (i8, i8) { - let (dx, dy): (i8, i8) = Self::DIRECTIONS[dir as usize]; - let x = (self.0 as i8) + dx * dist as i8; - let y = (self.1 as i8) + dy * dist as i8; - (x, y) - } } impl Mul for Pos { @@ -52,10 +34,21 @@ impl Add for Pos { impl Display for Pos { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - todo!() + write!(f, "{} , {}", self.0, self.1) } } +pub const DIRECTIONS : [(i8, i8); 8] = [ + (1, 0), + (1, -1), + (0, -1), + (-1, -1), + (-1, 0), + (-1, 1), + (0, 1), + (1, 1), +]; + #[derive(PartialEq)] pub enum SectorStatus { Empty, Star, StarBase, Klingon