From c4b8da053bc37e940ab998593cf0fb8cd136eb11 Mon Sep 17 00:00:00 2001 From: Christopher Date: Tue, 28 Feb 2023 11:37:54 +1300 Subject: [PATCH] started work on a rust implementation of super star trek --- 84_Super_Star_Trek/rust/Cargo.toml | 8 ++++++ 84_Super_Star_Trek/rust/src/main.rs | 34 ++++++++++++++++++++++ 84_Super_Star_Trek/rust/src/model.rs | 43 ++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 84_Super_Star_Trek/rust/Cargo.toml create mode 100644 84_Super_Star_Trek/rust/src/main.rs create mode 100644 84_Super_Star_Trek/rust/src/model.rs diff --git a/84_Super_Star_Trek/rust/Cargo.toml b/84_Super_Star_Trek/rust/Cargo.toml new file mode 100644 index 00000000..1ec69633 --- /dev/null +++ b/84_Super_Star_Trek/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/84_Super_Star_Trek/rust/src/main.rs b/84_Super_Star_Trek/rust/src/main.rs new file mode 100644 index 00000000..673b6700 --- /dev/null +++ b/84_Super_Star_Trek/rust/src/main.rs @@ -0,0 +1,34 @@ +use model::{Galaxy, GameStatus, Pos, Quadrant}; + +mod model; + +fn main() { + let mut galaxy = Galaxy::generate_new(); + // create the model + // start the loop + loop { + view(&galaxy); + galaxy = wait_for_command(&galaxy); + } + // rather than using a loop, recursion and passing the ownership might be better +} + +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) { + +} + +fn wait_for_command(galaxy: &Galaxy) -> Galaxy { + // listen for command from readline + // handle bad commands + // update model + Galaxy::generate_new() +} \ 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 new file mode 100644 index 00000000..9ca50189 --- /dev/null +++ b/84_Super_Star_Trek/rust/src/model.rs @@ -0,0 +1,43 @@ + +pub struct Galaxy { + pub quadrants: Vec, + pub enterprise: Enterprise, + pub game_status: GameStatus +} + +pub struct Pos(u8, u8); + +impl Pos { + pub fn as_index(&self) -> usize { + (self.0 * 8 + self.1).into() + } +} + +pub struct Quadrant { + pub stars: Vec, + pub star_bases: Vec, + pub klingons: Vec +} + +pub struct Klingon { + pub sector: Pos +} + +pub struct Enterprise { + pub quadrant: Pos, + pub sector: Pos, +} + +pub enum GameStatus { + ShortRangeScan +} + +impl Galaxy { + pub fn generate_new() -> Self { + Galaxy { + quadrants: Vec::new(), + enterprise: Enterprise { quadrant: Pos(0,0), sector: Pos(0,0) }, + game_status: GameStatus::ShortRangeScan + } + } +} \ No newline at end of file