diff --git a/84_Super_Star_Trek/rust/src/commands.rs b/84_Super_Star_Trek/rust/src/commands.rs index 5251109e..c27c6505 100644 --- a/84_Super_Star_Trek/rust/src/commands.rs +++ b/84_Super_Star_Trek/rust/src/commands.rs @@ -1,45 +1,11 @@ -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()]; - - let data : [String; 8] = [ - format!("Stardate {}", model.stardate), - format!("Condition {:?}", model.enterprise.condition), - format!("Quadrant {}", model.enterprise.quadrant), - format!("Sector {}", model.enterprise.sector), - format!("Photon torpedoes {}", model.enterprise.photon_torpedoes), - format!("Total energy {}", model.enterprise.total_energy), - format!("Shields {}", model.enterprise.shields), - format!("Klingons remaining {}", model.remaining_klingons()), - ]; - - println!("{:-^33}", ""); - for y in 0..=7 { - for x in 0..=7 { - let pos = Pos(x, y); - if &pos == &model.enterprise.sector { - print!("<*> ") - } else { - match quadrant.sector_status(&pos) { - SectorStatus::Star => print!(" * "), - SectorStatus::StarBase => print!(">!< "), - SectorStatus::Klingon => print!("+K+ "), - _ => print!(" "), - } - } - } - println!("{:>9}{}", "", data[y as usize]) - } - println!("{:-^33}", ""); -} +use crate::{model::{Galaxy, Pos, COURSES, EndPosition}, view}; 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 { - text_display::hit_edge(&end); + view::hit_edge(&end); } galaxy.enterprise.quadrant = end.quadrant; @@ -47,7 +13,7 @@ pub fn move_enterprise(course: u8, warp_speed: f32, galaxy: &mut Galaxy) { // if new_quadrant isnt old quadrant print intro - short_range_scan(&galaxy) + view::short_range_scan(&galaxy) } fn find_end_quadrant_sector(start_quadrant: Pos, start_sector: Pos, course: u8, warp_speed: f32) -> EndPosition { diff --git a/84_Super_Star_Trek/rust/src/main.rs b/84_Super_Star_Trek/rust/src/main.rs index bf6f5b96..ec923c8c 100644 --- a/84_Super_Star_Trek/rust/src/main.rs +++ b/84_Super_Star_Trek/rust/src/main.rs @@ -6,7 +6,7 @@ use crate::model::Condition; mod model; mod commands; -mod text_display; +mod view; fn main() { ctrlc::set_handler(move || { exit(0) }) @@ -14,17 +14,17 @@ fn main() { let mut galaxy = Galaxy::generate_new(); // todo: init options, starting state and notes - commands::short_range_scan(&galaxy); + view::short_range_scan(&galaxy); loop { match prompt("Command?").to_uppercase().as_str() { - "SRS" => commands::short_range_scan(&galaxy), + "SRS" => view::short_range_scan(&galaxy), "NAV" => gather_dir_and_speed_then_move(&mut galaxy), - _ => text_display::print_command_help() + _ => view::print_command_help() } if galaxy.enterprise.condition == Condition::Destroyed { // todo: also check if stranded - text_display::end_game_failure(&galaxy); + view::end_game_failure(&galaxy); break; } } @@ -34,13 +34,13 @@ fn gather_dir_and_speed_then_move(galaxy: &mut Galaxy) { let course = prompt_value::("Course (1-9)?", 1, 9); if course.is_none() { - text_display::bad_nav(); + view::bad_nav(); return; } let speed = prompt_value::("Warp Factor (0-8)?", 0.0, 8.0); if speed.is_none() { - text_display::bad_nav(); + view::bad_nav(); return; } diff --git a/84_Super_Star_Trek/rust/src/model.rs b/84_Super_Star_Trek/rust/src/model.rs index 5026bdd9..2e54555c 100644 --- a/84_Super_Star_Trek/rust/src/model.rs +++ b/84_Super_Star_Trek/rust/src/model.rs @@ -2,7 +2,7 @@ use std::{ops::{Mul, Add}, fmt::Display}; use rand::Rng; -use crate::text_display; +use crate::view; pub struct Galaxy { pub stardate: f32, @@ -48,12 +48,12 @@ impl Enterprise { return; } - text_display::enterprise_hit(&hit_strength, §or); + view::enterprise_hit(&hit_strength, §or); // absorb into shields if self.shields <= 0 { - text_display::enterprise_destroyed(); + view::enterprise_destroyed(); self.condition = Condition::Destroyed; } diff --git a/84_Super_Star_Trek/rust/src/text_display.rs b/84_Super_Star_Trek/rust/src/text_display.rs deleted file mode 100644 index 17ffcab1..00000000 --- a/84_Super_Star_Trek/rust/src/text_display.rs +++ /dev/null @@ -1,42 +0,0 @@ -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 diff --git a/84_Super_Star_Trek/rust/src/view.rs b/84_Super_Star_Trek/rust/src/view.rs new file mode 100644 index 00000000..5d8c1fd6 --- /dev/null +++ b/84_Super_Star_Trek/rust/src/view.rs @@ -0,0 +1,76 @@ +use crate::model::{Galaxy, Pos, EndPosition, SectorStatus}; + +pub fn short_range_scan(model: &Galaxy) { + let quadrant = &model.quadrants[model.enterprise.quadrant.as_index()]; + + let data : [String; 8] = [ + format!("Stardate {}", model.stardate), + format!("Condition {:?}", model.enterprise.condition), + format!("Quadrant {}", model.enterprise.quadrant), + format!("Sector {}", model.enterprise.sector), + format!("Photon torpedoes {}", model.enterprise.photon_torpedoes), + format!("Total energy {}", model.enterprise.total_energy), + format!("Shields {}", model.enterprise.shields), + format!("Klingons remaining {}", model.remaining_klingons()), + ]; + + println!("{:-^33}", ""); + for y in 0..=7 { + for x in 0..=7 { + let pos = Pos(x, y); + if &pos == &model.enterprise.sector { + print!("<*> ") + } else { + match quadrant.sector_status(&pos) { + SectorStatus::Star => print!(" * "), + SectorStatus::StarBase => print!(">!< "), + SectorStatus::Klingon => print!("+K+ "), + _ => print!(" "), + } + } + } + println!("{:>9}{}", "", data[y as usize]) + } + println!("{:-^33}", ""); +} + +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); +}