From 8903e77d838e21c221a482a450a7a16ced314fca Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 28 Feb 2023 19:45:54 +1300 Subject: [PATCH] reduced down to command and response (collapsed MVU) --- 84_Super_Star_Trek/rust/Cargo.toml | 1 + .../rust/src/{view.rs => commands.rs} | 17 +--- 84_Super_Star_Trek/rust/src/main.rs | 80 +++++-------------- 84_Super_Star_Trek/rust/src/model.rs | 12 +-- 84_Super_Star_Trek/rust/src/update.rs | 36 --------- 5 files changed, 29 insertions(+), 117 deletions(-) rename 84_Super_Star_Trek/rust/src/{view.rs => commands.rs} (54%) delete mode 100644 84_Super_Star_Trek/rust/src/update.rs diff --git a/84_Super_Star_Trek/rust/Cargo.toml b/84_Super_Star_Trek/rust/Cargo.toml index 3b1d02f5..01457249 100644 --- a/84_Super_Star_Trek/rust/Cargo.toml +++ b/84_Super_Star_Trek/rust/Cargo.toml @@ -6,4 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ctrlc = "3.2.5" rand = "0.8.5" diff --git a/84_Super_Star_Trek/rust/src/view.rs b/84_Super_Star_Trek/rust/src/commands.rs similarity index 54% rename from 84_Super_Star_Trek/rust/src/view.rs rename to 84_Super_Star_Trek/rust/src/commands.rs index 9527289d..264f7909 100644 --- a/84_Super_Star_Trek/rust/src/view.rs +++ b/84_Super_Star_Trek/rust/src/commands.rs @@ -1,22 +1,13 @@ -use crate::model::{Galaxy, GameStatus, Quadrant, Pos, SectorStatus}; +use crate::model::{Galaxy, Pos, SectorStatus}; +pub fn short_range_scan(model: &Galaxy) { + let quadrant = &model.quadrants[model.enterprise.sector.as_index()]; -pub fn view(model: &Galaxy) { - match model.game_status { - GameStatus::ShortRangeScan => { - let quadrant = &model.quadrants[model.enterprise.sector.as_index()]; - render_quadrant(&model.enterprise.sector, quadrant); - }, - _ => () - } -} - -fn render_quadrant(enterprise_sector: &Pos, quadrant: &Quadrant) { println!("{:-^33}", ""); for y in 0..=7 { for x in 0..=7 { let pos = Pos(x, y); - if &pos == enterprise_sector { + if &pos == &model.enterprise.sector { print!("<*> ") } else { match quadrant.sector_status(&pos) { diff --git a/84_Super_Star_Trek/rust/src/main.rs b/84_Super_Star_Trek/rust/src/main.rs index 43e3045c..cc6948ac 100644 --- a/84_Super_Star_Trek/rust/src/main.rs +++ b/84_Super_Star_Trek/rust/src/main.rs @@ -1,44 +1,27 @@ -use std::io::{stdin, stdout, Write}; +use std::{io::{stdin, stdout, Write}, process::exit}; -use model::{Galaxy, GameStatus}; -use update::Message; +use model::Galaxy; mod model; -mod view; -mod update; +mod commands; fn main() { - let mut galaxy = Galaxy::generate_new(); - loop { - view::view(&galaxy); - let command = wait_for_command(&galaxy.game_status); - galaxy = update::update(command, galaxy) - } -} + ctrlc::set_handler(move || { exit(0) }) + .expect("Error setting Ctrl-C handler"); + + let galaxy = Galaxy::generate_new(); + // init ops, starting state and notes + commands::short_range_scan(&galaxy); -fn wait_for_command(game_status: &GameStatus) -> Message { loop { - match game_status { - GameStatus::NeedDirectionForNav => { - let text = prompt("Course (1-9)?"); - if let Some(msg) = as_message(&text, game_status) { - return msg - } - }, - GameStatus::NeedSpeedForNav(_) => { - let text = prompt("Warp Factor (0-8)?"); - if let Some(msg) = as_message(&text, game_status) { - return msg - } - }, - _ => { - let text = prompt("Command?"); - if let Some(msg) = as_message(&text, game_status) { - return msg - } - print_command_help(); - } + match prompt("Command?").as_str() { + "SRS" => commands::short_range_scan(&galaxy), + _ => print_command_help() } + + // process the next command, based on it render something or update the galaxy or whatever + // this would be: read command, and based on it run dedicated function + // the function might get passed a mutable reference to the galaxy } } @@ -56,30 +39,11 @@ fn prompt(prompt: &str) -> String { "".into() } -fn as_message(text: &str, game_status: &GameStatus) -> Option { - match game_status { - GameStatus::NeedDirectionForNav => { - match text.parse::() { - Ok(n) if (n >= 1 && n <= 8) => Some(Message::DirectionForNav(n)), - _ => None - } - }, - GameStatus::NeedSpeedForNav(dir) => { - match text.parse::() { - Ok(n) if (n >= 1 && n <= 8) => Some(Message::DirectionAndSpeedForNav(*dir, n)), - _ => None - } - } - _ => { - match text { - "SRS" => Some(Message::RequestShortRangeScan), - "NAV" => Some(Message::RequestNavigation), - _ => None - } - } - } -} - fn print_command_help() { println!("valid commands are just SRS and NAV at the mo") -} \ No newline at end of file +} + +// match text.parse::() { +// Ok(n) if (n >= 1 && n <= 8) => Some(Message::DirectionForNav(n)), +// _ => None +// } \ No newline at end of file diff --git a/84_Super_Star_Trek/rust/src/model.rs b/84_Super_Star_Trek/rust/src/model.rs index a05d8566..a0f80751 100644 --- a/84_Super_Star_Trek/rust/src/model.rs +++ b/84_Super_Star_Trek/rust/src/model.rs @@ -2,8 +2,7 @@ use rand::Rng; pub struct Galaxy { pub quadrants: Vec, - pub enterprise: Enterprise, - pub game_status: GameStatus + pub enterprise: Enterprise } #[derive(PartialEq)] @@ -35,12 +34,6 @@ pub struct Enterprise { pub sector: Pos, } -pub enum GameStatus { - ShortRangeScan, - NeedDirectionForNav, - NeedSpeedForNav(u8), -} - impl Galaxy { pub fn generate_new() -> Self { let quadrants = Self::generate_quadrants(); @@ -51,8 +44,7 @@ impl Galaxy { Galaxy { quadrants: quadrants, - enterprise: Enterprise { quadrant: enterprise_quadrant, sector: enterprise_sector }, - game_status: GameStatus::ShortRangeScan + enterprise: Enterprise { quadrant: enterprise_quadrant, sector: enterprise_sector } } } diff --git a/84_Super_Star_Trek/rust/src/update.rs b/84_Super_Star_Trek/rust/src/update.rs deleted file mode 100644 index 5788c778..00000000 --- a/84_Super_Star_Trek/rust/src/update.rs +++ /dev/null @@ -1,36 +0,0 @@ -use crate::model::{Galaxy, GameStatus}; - -pub enum Message { - RequestShortRangeScan, - RequestNavigation, - DirectionForNav (u8), - DirectionAndSpeedForNav (u8, u8), -} - -pub fn update(message: Message, model: Galaxy) -> Galaxy { - match message { - Message::RequestShortRangeScan => - Galaxy { - game_status: GameStatus::ShortRangeScan, - ..model - }, - Message::RequestNavigation => { - Galaxy { - game_status: GameStatus::NeedDirectionForNav, - ..model - } - }, - Message::DirectionForNav(dir) => { - Galaxy { - game_status: GameStatus::NeedSpeedForNav(dir), - ..model - } - }, - Message::DirectionAndSpeedForNav(dir, speed) => { - Galaxy { - game_status: GameStatus::ShortRangeScan, - ..model - } - } - } -} \ No newline at end of file