From 558b9fc16227593fa401b856ddcaff980cccd053 Mon Sep 17 00:00:00 2001 From: Cutieguwu Date: Fri, 6 Dec 2024 14:38:55 -0500 Subject: [PATCH] Formatting convention corrections, some cleanup. --- src/characters.rs | 40 ++++++++--------- src/events.rs | 42 ++++++++---------- src/main.rs | 109 +++++++++++++++++++++++++--------------------- src/utils.rs | 40 ++++++++--------- 4 files changed, 115 insertions(+), 116 deletions(-) diff --git a/src/characters.rs b/src/characters.rs index 3d6bfee..5c36dc3 100644 --- a/src/characters.rs +++ b/src/characters.rs @@ -5,7 +5,7 @@ use crate::Gamemode; #[derive(Debug, Clone)] pub struct Character { pub name: &'static str, - pub standing: Standing + pub standing: Standing, } impl Character { @@ -47,7 +47,7 @@ pub struct Standing { pub society: i8, pub court: i8, pub friends: Vec<&'static People>, - pub enemies: Vec<&'static People> + pub enemies: Vec<&'static People>, } impl Standing { @@ -90,7 +90,7 @@ pub enum People { SarahGood(Option), SusannaWalcott(Option), ThomasPutnam(Option), - Tituba(Option) + Tituba(Option), } impl People { @@ -109,7 +109,7 @@ impl People { enemies: vec![ &People::ElizabethProctor(None), &People::JohnProctor(None), - &People::ReverendJohnHale(None) + &People::ReverendJohnHale(None), ] } })), @@ -122,12 +122,12 @@ impl People { &People::BettyParris(None), &People::ThomasPutnam(None), &People::ReverendParris(None), - &People::MercyLewis(None) + &People::MercyLewis(None), ], enemies: vec![ &People::ElizabethProctor(None), &People::JohnProctor(None), - &People::RebeccaNurse(None) + &People::RebeccaNurse(None), ] } })), @@ -138,11 +138,11 @@ impl People { court: 10, friends: vec![ &People::AbigailWilliams(None), - &People::ReverendParris(None) + &People::ReverendParris(None), ], enemies: vec![ &People::JohnProctor(None), - &People::ElizabethProctor(None) + &People::ElizabethProctor(None), ] } })), @@ -156,7 +156,7 @@ impl People { &People::JudgeHathorne(None), &People::ReverendParris(None), &People::EzekielCheever(None), - &People::MarshalHerrick(None) + &People::MarshalHerrick(None), ], enemies: vec![ &People::ElizabethProctor(None), @@ -164,7 +164,7 @@ impl People { &People::ReverendJohnHale(None), &People::FrancisNurse(None), &People::GilesCorey(None), - &People::MarthaCorey(None) + &People::MarthaCorey(None), ] } })), @@ -178,12 +178,12 @@ impl People { &People::GilesCorey(None), &People::JohnProctor(None), &People::RebeccaNurse(None), - &People::ReverendJohnHale(None) + &People::ReverendJohnHale(None), ], enemies: vec![ &People::AbigailWilliams(None), &People::DeputyGovernorDanforth(None), - &People::EzekielCheever(None) + &People::EzekielCheever(None), ] } })), @@ -197,14 +197,14 @@ impl People { &People::DeputyGovernorDanforth(None), &People::ReverendParris(None), &People::ReverendJohnHale(None), - &People::JudgeHathorne(None) + &People::JudgeHathorne(None), ], enemies: vec![ &People::ElizabethProctor(None), &People::JohnProctor(None), &People::FrancisNurse(None), - &People::GilesCorey(None) + &People::GilesCorey(None), ] } })), @@ -223,7 +223,7 @@ impl People { &People::AbigailWilliams(None), &People::ReverendJohnHale(None), &People::JudgeHathorne(None), - &People::ThomasPutnam(None) + &People::ThomasPutnam(None), ] } })), @@ -237,7 +237,7 @@ impl People { &People::FrancisNurse(None), &People::JohnProctor(None), &People::MarthaCorey(None), - &People::GilesCorey(None) + &People::GilesCorey(None), ], enemies: vec![ &People::ThomasPutnam(None), @@ -246,7 +246,7 @@ impl People { &People::JudgeHathorne(None), &People::EzekielCheever(None), &People::MarshalHerrick(None), - &People::AbigailWilliams(None) + &People::AbigailWilliams(None), ] } })), @@ -264,7 +264,7 @@ impl People { &People::EzekielCheever(None), &People::JudgeHathorne(None), &People::ReverendJohnHale(None), - &People::MarshalHerrick(None) + &People::MarshalHerrick(None), ] } })), @@ -296,7 +296,7 @@ impl Distribution for Standard { 19 => People::SusannaWalcott(None), 20 => People::ThomasPutnam(None), 21 => People::Tituba(None), - _ => unreachable!() + _ => unreachable!(), } } } @@ -305,5 +305,5 @@ impl Distribution for Standard { pub enum SurvivalStatus { PlayerMassacred, PlayerLived, - PlayerDied + PlayerDied, } \ No newline at end of file diff --git a/src/events.rs b/src/events.rs index c4613f5..265d0e4 100644 --- a/src/events.rs +++ b/src/events.rs @@ -2,26 +2,22 @@ use std::i8; use rand::{ distributions::{Distribution, Standard}, - rngs::ThreadRng + rngs::ThreadRng, }; use crate::characters::{ Character, People, - SurvivalStatus + SurvivalStatus, }; -pub fn get_event() -> Events { - Events::Trial(Trial) -} - #[allow(unused_variables)] pub trait EventHandle { - fn handle<'main>( + fn handle<'a>( self: &Self, - rng: &'main ThreadRng, - player: &'main mut Character, - characters: &'main mut Vec + rng: &'a ThreadRng, + player: &'a mut Character, + characters: &'a mut Vec, ) -> EventReturn { EventReturn::None } @@ -31,13 +27,13 @@ pub trait EventHandle { pub struct Trial; impl EventHandle for Trial { - fn handle<'main>( + fn handle<'a>( self: &Self, - rng: &'main ThreadRng, - player: &'main mut Character, - characters: &'main mut Vec + rng: &'a ThreadRng, + player: &'a mut Character, + characters: &'a mut Vec, ) -> EventReturn { - let mut player_status = SurvivalStatus::PlayerDied; + let mut player_status: SurvivalStatus = SurvivalStatus::PlayerDied; println!("You have been summoned before the court.\n"); @@ -53,11 +49,11 @@ impl EventHandle for Trial { pub struct WildAccusation; impl EventHandle for WildAccusation { - fn handle<'main>( + fn handle<'a>( self: &Self, - rng: &'main ThreadRng, - player: &'main mut Character, - characters: &'main mut Vec + rng: &'a ThreadRng, + player: &'a mut Character, + characters: &'a mut Vec, ) -> EventReturn { EventReturn::None } @@ -65,15 +61,15 @@ impl EventHandle for WildAccusation { #[derive(Debug)] pub enum Events { - Trial(Trial) + Trial(Trial), } impl Distribution for Standard { fn sample(&self, rng: &mut R) -> Events { match rng.gen_range(0..=0) { 0 => Events::Trial(Trial), - 1 => Events::Trial(Trial), - _ => unreachable!() + 1 => todo!(), + _ => unreachable!(), } } } @@ -81,5 +77,5 @@ impl Distribution for Standard { #[derive(Debug)] pub enum EventReturn { Survival(SurvivalStatus), - None + None, } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 06df7a6..7b083d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,76 +5,85 @@ mod utils; use characters::{ Character, People, - SurvivalStatus + SurvivalStatus, }; + use events::{ - get_event, EventHandle, EventReturn, Events, Trial + EventHandle, + EventReturn, + Events, + Trial, }; + use rand::{ random, - thread_rng + thread_rng, + rngs::ThreadRng }; + use utils::{ fmt_args, ArgType, - FlagType + FlagType, }; //Number of game rounds. const ROUNDS: u8 = 15; fn main() { - let cmd_args = dbg!(fmt_args()); - let mut characters = People::build_characters(); - let mut rng = thread_rng(); + let cmd_args: Vec = fmt_args(); + let mut characters: Vec = People::build_characters(); + let rng: ThreadRng = thread_rng(); // Set player default values based on gamemode. - let mut player: Character = dbg!(Character::build_player(Gamemode::get(&cmd_args))); - let mut end_status = SurvivalStatus::PlayerLived; + let mut player: Character = Character::build_player(Gamemode::get(&cmd_args)); + let mut end_status: SurvivalStatus = SurvivalStatus::PlayerLived; // Game loop. for r in 0..=ROUNDS { - let random_event: Events = random(); + end_status = run_event( + &rng, + &mut player, + &mut characters, + random(), + ); - match random_event { - Events::Trial(trial) => match trial.handle(&rng, &mut player, &mut characters) { - EventReturn::Survival(state) => match state { - SurvivalStatus::PlayerLived => (), - _ => {end_status = state} - } - _ => unreachable!() - } - } - - match end_status { - SurvivalStatus::PlayerLived => (), - _ => break - } - - // If player's standing is too low, bring to court. - if player.standing.clone().calculate() <= 0 { - match Trial.handle(&rng, &mut player, &mut characters) { - EventReturn::Survival(state) => match state { - SurvivalStatus::PlayerLived => (), - _ => {end_status = state} - }, - _ => unreachable!() - }; - }; - - if r == ROUNDS { - end_status = SurvivalStatus::PlayerLived - }; + if matches!(end_status, SurvivalStatus::PlayerLived) { break } if characters.len() == 0 { end_status = SurvivalStatus::PlayerMassacred + } else if r == ROUNDS { + end_status = SurvivalStatus::PlayerLived + } else if player.standing.clone().calculate() <= 0 { + //Standing too low, bring to court. + end_status = run_event( + &rng, + &mut player, + &mut characters, + Events::Trial(Trial), + ) }; + }; match end_status { SurvivalStatus::PlayerDied => println!("You died. And for what?"), SurvivalStatus::PlayerLived => println!("You survived, but at what cost? How many died to save you?"), - SurvivalStatus::PlayerMassacred => println!("You killed everyone. I ask, at what end will you yield. Care you not for the life of others?") + SurvivalStatus::PlayerMassacred => println!("You killed everyone. I ask, at what end will you yield. Care you not for the life of others?"), + }; +} + +fn run_event<'main>( + rng: &ThreadRng , + player: &mut Character, + characters: &mut Vec, + event: Events, +) -> SurvivalStatus { + match event { + Events::Trial(trial) => match trial.handle(rng, player, characters) { + EventReturn::Survival(state) => state, + _ => unreachable!(), + }, } } @@ -87,34 +96,34 @@ pub enum Gamemode { //Hard JohnProctor, //Just dead. - GoodyOsborne + GoodyOsborne, } impl Gamemode { fn get(cmd_args: &Vec) -> Gamemode { if cmd_args.len() != 1 { match &cmd_args[1] { - ArgType::Command(c) => match c.as_str() { + ArgType::Command(command) => match command.as_str() { "abigail" => Gamemode::Abigail, "john_proctor" => Gamemode::JohnProctor, "goody_osborne" => Gamemode::GoodyOsborne, - _ => Gamemode::MaryWarren + _ => Gamemode::MaryWarren, }, - ArgType::Flag(f) => match f { - FlagType::Long(l) => match l.as_str() { + ArgType::Flag(flag) => match flag { + FlagType::Long(flag_long) => match flag_long.as_str() { "abigail" => Gamemode::Abigail, "john-proctor" => Gamemode::JohnProctor, "goody-osborne" => Gamemode::GoodyOsborne, - _ => Gamemode::MaryWarren + _ => Gamemode::MaryWarren, }, - FlagType::Short(s) => match s.as_str() { + FlagType::Short(flag_short) => match flag_short.as_str() { "a" => Gamemode::Abigail, "j" => Gamemode::JohnProctor, "o" => Gamemode::GoodyOsborne, - _ => Gamemode::MaryWarren - } + _ => Gamemode::MaryWarren, + }, }, - _ => Gamemode::MaryWarren + _ => Gamemode::MaryWarren, } } else { Gamemode::MaryWarren diff --git a/src/utils.rs b/src/utils.rs index 51a10e6..de534ff 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,42 +1,38 @@ -use std::env::args; -use std::io; +use std::{ + env::args, + io, +}; -const DASH:char = 45 as u8 as char; +const DASH: char = 45 as u8 as char; #[derive(Debug, Clone)] pub enum ArgType { #[allow(dead_code)] Binary(String), Command(String), - Flag(FlagType) + Flag(FlagType), } #[derive(Debug, Clone)] pub enum FlagType { //Currently only handles single flags. eg. "-S", "-y" Short(String), - Long(String) + Long(String), } pub fn fmt_args() -> Vec { let mut args_vec:Vec = Vec::new(); - for x in args() { - match try_flag(x.clone()) { - None => args_vec.push(ArgType::Command(x)), - Some(f) => args_vec.push(ArgType::Flag(f)) - } - } + for obj in args() { match try_flag(obj.clone()) { + None => args_vec.push(ArgType::Command(obj)), + Some(flag) => args_vec.push(ArgType::Flag(flag)), + }} - args_vec[0] = { - ArgType::Binary({ - match args_vec[0].clone() { - ArgType::Command(c) => c, - err => panic!("Expected ArgType::Command at args_vec[0], found {:?}", err) - } - }) - }; + args_vec[0] = ArgType::Binary(match args_vec[0].clone() { + ArgType::Command(command) => command, + err => panic!("Expected ArgType::Command at args_vec[0], found {:?}", err) + }); args_vec } @@ -59,15 +55,13 @@ fn break_flag_short(mut arg: String) -> String { } fn break_flag_long(mut arg: String) -> String { - for _ in 1..=2 { - arg.remove(0); - }; + for _ in 1..=2 { arg.remove(0); }; arg } pub fn input() -> String { - let mut input_buffer = String::new(); + let mut input_buffer: String = String::new(); io::stdin() .read_line( &mut input_buffer)