diff --git a/84_Super_Star_Trek/rust/src/commands.rs b/84_Super_Star_Trek/rust/src/commands.rs index 5cd90150..5251109e 100644 --- a/84_Super_Star_Trek/rust/src/commands.rs +++ b/84_Super_Star_Trek/rust/src/commands.rs @@ -1,4 +1,4 @@ -use crate::model::{Galaxy, Pos, SectorStatus, COURSES, Quadrant}; +use crate::{model::{Galaxy, Pos, SectorStatus, COURSES, Quadrant, EndPosition}, text_display}; pub fn short_range_scan(model: &Galaxy) { let quadrant = &model.quadrants[model.enterprise.quadrant.as_index()]; @@ -39,11 +39,7 @@ pub fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) { let end = find_end_quadrant_sector(galaxy.enterprise.quadrant, galaxy.enterprise.sector, course, warp_speed); if end.hit_edge { - println!("Lt. Uhura report message from Starfleet Command: - 'Permission to attempt crossing of galactic perimeter - is hereby *Denied*. Shut down your engines.' - Chief Engineer Scott reports, 'Warp engines shut down - at sector {} of quadrant {}.'", end.quadrant, end.sector); + text_display::hit_edge(&end); } galaxy.enterprise.quadrant = end.quadrant; @@ -54,12 +50,6 @@ pub fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) { short_range_scan(&galaxy) } -struct EndPosition { - quadrant: Pos, - sector: Pos, - hit_edge: bool -} - fn find_end_quadrant_sector(start_quadrant: Pos, start_sector: Pos, course: u8, warp_speed: f32) -> EndPosition { let (dx, dy): (i8, i8) = COURSES[(course - 1) as usize]; diff --git a/84_Super_Star_Trek/rust/src/main.rs b/84_Super_Star_Trek/rust/src/main.rs index c91ca8bc..bf6f5b96 100644 --- a/84_Super_Star_Trek/rust/src/main.rs +++ b/84_Super_Star_Trek/rust/src/main.rs @@ -6,6 +6,7 @@ use crate::model::Condition; mod model; mod commands; +mod text_display; fn main() { ctrlc::set_handler(move || { exit(0) }) @@ -19,31 +20,27 @@ fn main() { 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() + _ => text_display::print_command_help() } if galaxy.enterprise.condition == Condition::Destroyed { // todo: also check if stranded - println!("Is is stardate {}. - There were {} Klingon battle cruisers left at - the end of your mission. - ", galaxy.stardate, galaxy.remaining_klingons()); + text_display::end_game_failure(&galaxy); break; } } } fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) { - const BAD_NAV: &str = " Lt. Sulu reports, 'Incorrect course data, sir!'"; let course = prompt_value::("Course (1-9)?", 1, 9); if course.is_none() { - println!("{}", BAD_NAV); + text_display::bad_nav(); return; } let speed = prompt_value::("Warp Factor (0-8)?", 0.0, 8.0); if speed.is_none() { - println!("{}", BAD_NAV); + text_display::bad_nav(); return; } @@ -75,18 +72,3 @@ fn prompt_value(prompt_text: &str, min: T, max: T) -> O _ => None } } - -fn print_command_help() { - println!("Enter one of the following: - NAV (To set course) - SRS (For short range sensor scan) - LRS (For long range sensor scan) - PHA (To fire phasers) - TOR (To fire photon torpedoes) - SHE (To raise or lower shields) - DAM (For damage control reports) - COM (To call on library-computer) - XXX (To resign your command) - ") -} - diff --git a/84_Super_Star_Trek/rust/src/model.rs b/84_Super_Star_Trek/rust/src/model.rs index 94e9128f..5026bdd9 100644 --- a/84_Super_Star_Trek/rust/src/model.rs +++ b/84_Super_Star_Trek/rust/src/model.rs @@ -2,6 +2,8 @@ use std::{ops::{Mul, Add}, fmt::Display}; use rand::Rng; +use crate::text_display; + pub struct Galaxy { pub stardate: f32, pub quadrants: Vec, @@ -46,12 +48,12 @@ impl Enterprise { return; } - println!("{hit_strength} unit hit on Enterprise from sector {sector}"); + text_display::enterprise_hit(&hit_strength, §or); // absorb into shields if self.shields <= 0 { - println!("The Enterprise has been destroyed. The Federation will be conquered."); + text_display::enterprise_destroyed(); self.condition = Condition::Destroyed; } @@ -66,6 +68,12 @@ pub enum Condition { Destroyed, } +pub struct EndPosition { + pub quadrant: Pos, + pub sector: Pos, + pub hit_edge: bool +} + #[derive(PartialEq, Clone, Copy, Debug)] pub struct Pos(pub u8, pub u8); diff --git a/84_Super_Star_Trek/rust/src/text_display.rs b/84_Super_Star_Trek/rust/src/text_display.rs new file mode 100644 index 00000000..17ffcab1 --- /dev/null +++ b/84_Super_Star_Trek/rust/src/text_display.rs @@ -0,0 +1,42 @@ +use crate::model::{Galaxy, Pos, EndPosition}; + +pub fn print_command_help() { + println!("Enter one of the following: + NAV (To set course) + SRS (For short range sensor scan) + LRS (For long range sensor scan) + PHA (To fire phasers) + TOR (To fire photon torpedoes) + SHE (To raise or lower shields) + DAM (For damage control reports) + COM (To call on library-computer) + XXX (To resign your command) + ") +} + +pub fn end_game_failure(galaxy: &Galaxy) { + println!("Is is stardate {}. +There were {} Klingon battle cruisers left at +the end of your mission. +", galaxy.stardate, galaxy.remaining_klingons()); +} + +pub fn enterprise_destroyed() { + println!("The Enterprise has been destroyed. The Federation will be conquered."); +} + +pub fn bad_nav() { + println!(" Lt. Sulu reports, 'Incorrect course data, sir!'") +} + +pub fn enterprise_hit(hit_strength: &u16, from_sector: &Pos) { + println!("{hit_strength} unit hit on Enterprise from sector {from_sector}"); +} + +pub fn hit_edge(end: &EndPosition) { + println!("Lt. Uhura report message from Starfleet Command: + 'Permission to attempt crossing of galactic perimeter + is hereby *Denied*. Shut down your engines.' + Chief Engineer Scott reports, 'Warp engines shut down + at sector {} of quadrant {}.'", end.quadrant, end.sector); +} \ No newline at end of file