mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
started work on a rust implementation of super star trek
This commit is contained in:
8
84_Super_Star_Trek/rust/Cargo.toml
Normal file
8
84_Super_Star_Trek/rust/Cargo.toml
Normal file
@@ -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]
|
||||
34
84_Super_Star_Trek/rust/src/main.rs
Normal file
34
84_Super_Star_Trek/rust/src/main.rs
Normal file
@@ -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()
|
||||
}
|
||||
43
84_Super_Star_Trek/rust/src/model.rs
Normal file
43
84_Super_Star_Trek/rust/src/model.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
pub struct Galaxy {
|
||||
pub quadrants: Vec<Quadrant>,
|
||||
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<Pos>,
|
||||
pub star_bases: Vec<Pos>,
|
||||
pub klingons: Vec<Klingon>
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user