mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
moved blobs of text into text_display mod
This commit is contained in:
@@ -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];
|
||||
|
||||
|
||||
@@ -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::<u8>("Course (1-9)?", 1, 9);
|
||||
if course.is_none() {
|
||||
println!("{}", BAD_NAV);
|
||||
text_display::bad_nav();
|
||||
return;
|
||||
}
|
||||
|
||||
let speed = prompt_value::<f32>("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<T: FromStr + PartialOrd>(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)
|
||||
")
|
||||
}
|
||||
|
||||
|
||||
@@ -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<Quadrant>,
|
||||
@@ -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);
|
||||
|
||||
|
||||
42
84_Super_Star_Trek/rust/src/text_display.rs
Normal file
42
84_Super_Star_Trek/rust/src/text_display.rs
Normal file
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user