mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-25 12:25:10 -08:00
work aligning with mvu, along with a render quadrant view function
This commit is contained in:
@@ -1,34 +1,54 @@
|
||||
use model::{Galaxy, GameStatus, Pos, Quadrant};
|
||||
use std::io::stdin;
|
||||
|
||||
use model::{Galaxy, GameStatus};
|
||||
use update::Message;
|
||||
|
||||
mod model;
|
||||
mod view;
|
||||
mod update;
|
||||
|
||||
fn main() {
|
||||
let mut galaxy = Galaxy::generate_new();
|
||||
// create the model
|
||||
// start the loop
|
||||
loop {
|
||||
view(&galaxy);
|
||||
galaxy = wait_for_command(&galaxy);
|
||||
view::view(&galaxy);
|
||||
let command = wait_for_command(&galaxy.game_status);
|
||||
galaxy = update::update(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 wait_for_command(game_status: &GameStatus) -> Message {
|
||||
let stdin = stdin();
|
||||
loop {
|
||||
match game_status {
|
||||
_ => {
|
||||
println!("Command?");
|
||||
let mut buffer = String::new();
|
||||
if let Ok(_) = stdin.read_line(&mut buffer) {
|
||||
let text = buffer.trim_end();
|
||||
if let Some(msg) = as_message(text, game_status) {
|
||||
return msg
|
||||
}
|
||||
print_command_help();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn render_quadrant(enterprise_sector: &Pos, quadrant: &Quadrant) {
|
||||
|
||||
fn as_message(text: &str, game_status: &GameStatus) -> Option<Message> {
|
||||
if text == "" {
|
||||
return None
|
||||
}
|
||||
match game_status {
|
||||
_ => {
|
||||
match text {
|
||||
"SRS" => Some(Message::RequestShortRangeScan),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn wait_for_command(galaxy: &Galaxy) -> Galaxy {
|
||||
// listen for command from readline
|
||||
// handle bad commands
|
||||
// update model
|
||||
Galaxy::generate_new()
|
||||
fn print_command_help() {
|
||||
println!("valid commands are just SRS at the mo")
|
||||
}
|
||||
@@ -5,7 +5,8 @@ pub struct Galaxy {
|
||||
pub game_status: GameStatus
|
||||
}
|
||||
|
||||
pub struct Pos(u8, u8);
|
||||
#[derive(PartialEq)]
|
||||
pub struct Pos(pub u8, pub u8);
|
||||
|
||||
impl Pos {
|
||||
pub fn as_index(&self) -> usize {
|
||||
|
||||
15
84_Super_Star_Trek/rust/src/update.rs
Normal file
15
84_Super_Star_Trek/rust/src/update.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use crate::model::{Galaxy, GameStatus};
|
||||
|
||||
pub enum Message {
|
||||
RequestShortRangeScan
|
||||
}
|
||||
|
||||
pub fn update(message: Message, model: Galaxy) -> Galaxy {
|
||||
match message {
|
||||
Message::RequestShortRangeScan =>
|
||||
Galaxy {
|
||||
game_status: GameStatus::ShortRangeScan,
|
||||
..model
|
||||
}
|
||||
}
|
||||
}
|
||||
35
84_Super_Star_Trek/rust/src/view.rs
Normal file
35
84_Super_Star_Trek/rust/src/view.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use crate::model::{Galaxy, GameStatus, Quadrant, Pos, Klingon};
|
||||
|
||||
|
||||
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 {
|
||||
print!("<*> ")
|
||||
} else if quadrant.stars.contains(&pos) {
|
||||
print!(" * ")
|
||||
} else if quadrant.star_bases.contains(&pos) {
|
||||
print!(">!< ")
|
||||
} else if let Some(_) = find_klingon(&pos, &quadrant.klingons) {
|
||||
print!("+K+ ")
|
||||
}
|
||||
}
|
||||
print!("\n")
|
||||
}
|
||||
println!("{:-^33}", "");
|
||||
}
|
||||
|
||||
fn find_klingon<'a>(sector: &Pos, klingons: &'a Vec<Klingon>) -> Option<&'a Klingon> {
|
||||
klingons.into_iter().find(|k| &k.sector == sector)
|
||||
}
|
||||
Reference in New Issue
Block a user