work aligning with mvu, along with a render quadrant view function

This commit is contained in:
Christopher
2023-02-28 12:36:29 +13:00
parent c4b8da053b
commit 92b4d60e84
4 changed files with 90 additions and 19 deletions

View File

@@ -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")
}

View File

@@ -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 {

View 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
}
}
}

View 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)
}