mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-27 13:14:15 -08:00
reduced down to command and response (collapsed MVU)
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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) {
|
||||
@@ -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<Message> {
|
||||
match game_status {
|
||||
GameStatus::NeedDirectionForNav => {
|
||||
match text.parse::<u8>() {
|
||||
Ok(n) if (n >= 1 && n <= 8) => Some(Message::DirectionForNav(n)),
|
||||
_ => None
|
||||
}
|
||||
},
|
||||
GameStatus::NeedSpeedForNav(dir) => {
|
||||
match text.parse::<u8>() {
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
// match text.parse::<u8>() {
|
||||
// Ok(n) if (n >= 1 && n <= 8) => Some(Message::DirectionForNav(n)),
|
||||
// _ => None
|
||||
// }
|
||||
@@ -2,8 +2,7 @@ use rand::Rng;
|
||||
|
||||
pub struct Galaxy {
|
||||
pub quadrants: Vec<Quadrant>,
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user