diff --git a/data/characters.ron b/data/characters.ron new file mode 100644 index 0000000..519b720 --- /dev/null +++ b/data/characters.ron @@ -0,0 +1,323 @@ +[ + Character ( + id: AbigailWilliams, + name: "Abigail Williams", + standing: Standing ( + society: 10, + court: 10, + friends: [ + BettyParris, + DeputyGovernorDanforth, + ReverendParris, + ], + enemies: [ + ElizabethProctor, + JohnProctor, + ReverendJohnHale, + ] + ) + ), + Character ( + id: AnnPutnam, + name: "Ann Putnam", + standing: Standing ( + society: 10, + court: 7, + friends: [ + BettyParris, + ThomasPutnam, + ReverendParris, + MercyLewis, + ], + enemies: [ + ElizabethProctor, + JohnProctor, + RebeccaNurse, + ] + ) + ), + Character ( + id: BettyParris, + name: "Betty Parris", + standing: Standing ( + society: 10, + court: 10, + friends: [ + AbigailWilliams, + ReverendParris, + ], + enemies: [ + JohnProctor, + ElizabethProctor, + ] + ) + ), + Character ( + id: DeputyGovernorDanforth, + name: "Deputy Governor Danforth", + standing: Standing ( + society: 10, + court: 100, + friends: [ + AbigailWilliams, + JudgeHathorne, + ReverendParris, + EzekielCheever, + MarshalHerrick, + ], + enemies: [ + ElizabethProctor, + JohnProctor, + ReverendJohnHale, + FrancisNurse, + GilesCorey, + MarthaCorey, + ] + ) + ), + Character ( + id: ElizabethProctor, + name: "Elizabeth Proctor", + standing: Standing ( + society: 10, + court: 5, + friends: [ + FrancisNurse, + GilesCorey, + JohnProctor, + RebeccaNurse, + ReverendJohnHale, + ], + enemies: [ + AbigailWilliams, + DeputyGovernorDanforth, + EzekielCheever, + ] + ) + ), + Character ( + id: EzekielCheever, + name: "Ezekiel Cheever", + standing: Standing ( + society: 12, + court: 50, + friends: [ + MarshalHerrick, + DeputyGovernorDanforth, + ReverendParris, + ReverendJohnHale, + JudgeHathorne, + + ], + enemies: [ + ElizabethProctor, + JohnProctor, + FrancisNurse, + GilesCorey, + ] + ) + ), + Character ( + id: FrancisNurse, + name: "Francis Nurse", + standing: Standing ( + society: 10, + court: 10, + friends: [ + RebeccaNurse, + JohnProctor, + GilesCorey, + ], + enemies: [ + AnnPutnam, + AbigailWilliams, + ReverendJohnHale, + JudgeHathorne, + ThomasPutnam, + ] + ) + ), + Character ( + id: RebeccaNurse, + name: "Rebecca Nurse", + standing: Standing ( + society: 20, + court: 8, + friends: [ + ElizabethProctor, + FrancisNurse, + JohnProctor, + MarthaCorey, + GilesCorey, + ], + enemies: [ + ThomasPutnam, + AnnPutnam, + ReverendJohnHale, + JudgeHathorne, + EzekielCheever, + MarshalHerrick, + AbigailWilliams, + ] + ) + ), + Character ( + id: GilesCorey, + name: "Giles Corey", + standing: Standing ( + society: 2, + court: 5, + friends: [ + JohnProctor, + MarthaCorey, + FrancisNurse, + ], + enemies: [ + EzekielCheever, + JudgeHathorne, + ReverendJohnHale, + MarshalHerrick, + ] + ) + ), + Character ( + id: GoodyOsborne, + name: "Goody Osborne", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: JohnProctor, + name: "John Proctor", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: JudgeHathorne, + name: "Judge Hathorne", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: MaryWarren, + name: "Mary Warren", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: MarshalHerrick, + name: "Marshal Herrick", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: MarthaCorey, + name: "Martha Corey", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: MercyLewis, + name: "Mercy Lewis", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: RebeccaNurse, + name: "Rebecca Nurse", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: ReverendJohnHale, + name: "Reverend John Hale", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: ReverendParris, + name: "Reverend Parris", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: SarahGood, + name: "Sarah Good", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: SusannaWalcott, + name: "Susanna Walcott", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: ThomasPutnam, + name: "Thomas Putnam", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), + Character ( + id: Tituba, + name: "Tituba", + standing: Standing ( + society: 10, + court: 10, + friends: [], + enemies: [] + ) + ), +] \ No newline at end of file diff --git a/src/characters.rs b/src/characters.rs index 34b56b6..c143c76 100644 --- a/src/characters.rs +++ b/src/characters.rs @@ -1,53 +1,56 @@ +// Copyright (c) 2024 Cutieguwu | Olivia Brooks +// +// -*- coding: utf-8 -*- +// @Title: Culling of the Sheep +// @Author: Cutieguwu | Olivia Brooks +// @Description: Components for characters. +// +// @Script: characters.rs +// @Date Created: 03 Dec, 2024 +// @Last Modified: 10 Dec, 2024 +// @Last Modified by: Cutieguwu | Olivia Brooks +// -------------------------------------------- + +use std::fs::File; + use rand::distributions::{Distribution, Standard}; +use ron::de::from_reader; +use serde::Deserialize; use crate::Gamemode; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Deserialize, PartialEq)] pub struct Character { - pub name: &'static str, + pub id: People, + pub name: String, pub standing: Standing, } impl Character { - pub fn build_player(gamemode: Gamemode) -> Character { - Character { - name: "You", - standing: match gamemode { - Gamemode::Abigail => Standing { - society: 10, - court: 10, - friends: vec![], - enemies: vec![] - }, - Gamemode::MaryWarren => Standing { - society: 5, - court: 5, - friends: vec![], - enemies: vec![] - }, - Gamemode::JohnProctor => Standing { - society: 2, - court: 2, - friends: vec![], - enemies: vec![] - }, - Gamemode::GoodyOsborne => Standing { - society: -15, - court: -5, - friends: vec![], - enemies: vec![] - } - } - } + pub fn build_player<'a>( + characters: &'a mut Vec, + gamemode: Gamemode, + ) -> Character { + let character_model: People = match gamemode { + Gamemode::Abigail => People::AbigailWilliams, + Gamemode::MaryWarren => People::MaryWarren, + Gamemode::JohnProctor => People::JohnProctor, + Gamemode::GoodyOsborne => People::GoodyOsborne, + }; + let position: usize = characters.iter() + .position(|x| x.id == character_model) + .expect("Failed to locate character to model player by"); + + characters.swap_remove(position) } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Deserialize, PartialEq)] pub struct Standing { pub society: i8, pub court: i8, - pub friends: Vec<&'static People>, - pub enemies: Vec<&'static People>, + pub friends: Vec, + pub enemies: Vec, } impl Standing { @@ -67,241 +70,80 @@ impl Standing { } } -#[derive(Debug)] +#[derive(Debug, Clone, Deserialize, PartialEq)] pub enum People { - AbigailWilliams(Option), - AnnPutnam(Option), - BettyParris(Option), - DeputyGovernorDanforth(Option), - ElizabethProctor(Option), - EzekielCheever(Option), - FrancisNurse(Option), - GilesCorey(Option), - GoodyOsborne(Option), - JohnProctor(Option), - JudgeHathorne(Option), - MaryWarren(Option), - MarshalHerrick(Option), - MarthaCorey(Option), - MercyLewis(Option), - RebeccaNurse(Option), - ReverendParris(Option), - ReverendJohnHale(Option), - SarahGood(Option), - SusannaWalcott(Option), - ThomasPutnam(Option), - Tituba(Option), + AbigailWilliams, + AnnPutnam, + BettyParris, + DeputyGovernorDanforth, + ElizabethProctor, + EzekielCheever, + FrancisNurse, + GilesCorey, + GoodyOsborne, + JohnProctor, + JudgeHathorne, + MaryWarren, + MarshalHerrick, + MarthaCorey, + MercyLewis, + RebeccaNurse, + ReverendJohnHale, + ReverendParris, + SarahGood, + SusannaWalcott, + ThomasPutnam, + Tituba, + Player, } impl People { - pub fn build_characters() -> Vec { - vec![ - People::AbigailWilliams(Some(Character { - name: "Abigail Williams", - standing: Standing { - society: 10, - court: 10, - friends: vec![ - &People::BettyParris(None), - &People::DeputyGovernorDanforth(None), - &People::ReverendParris(None), - ], - enemies: vec![ - &People::ElizabethProctor(None), - &People::JohnProctor(None), - &People::ReverendJohnHale(None), - ] - } - })), - People::AnnPutnam(Some(Character { - name: "Ann Putnam", - standing: Standing { - society: 10, - court: 7, - friends: vec![ - &People::BettyParris(None), - &People::ThomasPutnam(None), - &People::ReverendParris(None), - &People::MercyLewis(None), - ], - enemies: vec![ - &People::ElizabethProctor(None), - &People::JohnProctor(None), - &People::RebeccaNurse(None), - ] - } - })), - People::BettyParris(Some(Character { - name: "Betty Parris", - standing: Standing { - society: 10, - court: 10, - friends: vec![ - &People::AbigailWilliams(None), - &People::ReverendParris(None), - ], - enemies: vec![ - &People::JohnProctor(None), - &People::ElizabethProctor(None), - ] - } - })), - People::DeputyGovernorDanforth(Some(Character { - name: "Deputy Governor Danforth", - standing: Standing { - society: 10, - court: 100, - friends: vec![ - &People::AbigailWilliams(None), - &People::JudgeHathorne(None), - &People::ReverendParris(None), - &People::EzekielCheever(None), - &People::MarshalHerrick(None), - ], - enemies: vec![ - &People::ElizabethProctor(None), - &People::JohnProctor(None), - &People::ReverendJohnHale(None), - &People::FrancisNurse(None), - &People::GilesCorey(None), - &People::MarthaCorey(None), - ] - } - })), - People::ElizabethProctor(Some(Character{ - name: "Elizabeth Proctor", - standing: Standing { - society: 10, - court: 5, - friends: vec![ - &People::FrancisNurse(None), - &People::GilesCorey(None), - &People::JohnProctor(None), - &People::RebeccaNurse(None), - &People::ReverendJohnHale(None), - ], - enemies: vec![ - &People::AbigailWilliams(None), - &People::DeputyGovernorDanforth(None), - &People::EzekielCheever(None), - ] - } - })), - People::EzekielCheever(Some(Character { - name: "Ezekiel Cheever", - standing: Standing { - society: 12, - court: 50, - friends: vec![ - &People::MarshalHerrick(None), - &People::DeputyGovernorDanforth(None), - &People::ReverendParris(None), - &People::ReverendJohnHale(None), - &People::JudgeHathorne(None), - - ], - enemies: vec![ - &People::ElizabethProctor(None), - &People::JohnProctor(None), - &People::FrancisNurse(None), - &People::GilesCorey(None), - ] - } - })), - People::FrancisNurse(Some(Character { - name: "Francis Nurse", - standing: Standing { - society: 10, - court: 10, - friends: vec![ - &People::RebeccaNurse(None), - &People::JohnProctor(None), - &People::GilesCorey(None), - ], - enemies: vec![ - &People::AnnPutnam(None), - &People::AbigailWilliams(None), - &People::ReverendJohnHale(None), - &People::JudgeHathorne(None), - &People::ThomasPutnam(None), - ] - } - })), - People::RebeccaNurse(Some(Character { - name: "Rebecca Nurse", - standing: Standing { - society: 20, - court: 8, - friends: vec![ - &People::ElizabethProctor(None), - &People::FrancisNurse(None), - &People::JohnProctor(None), - &People::MarthaCorey(None), - &People::GilesCorey(None), - ], - enemies: vec![ - &People::ThomasPutnam(None), - &People::AnnPutnam(None), - &People::ReverendJohnHale(None), - &People::JudgeHathorne(None), - &People::EzekielCheever(None), - &People::MarshalHerrick(None), - &People::AbigailWilliams(None), - ] - } - })), - People::GilesCorey(Some(Character { - name: "Giles Corey", - standing: Standing { - society: 2, - court: 5, - friends: vec![ - &People::JohnProctor(None), - &People::MarthaCorey(None), - &People::FrancisNurse(None), - ], - enemies: vec![ - &People::EzekielCheever(None), - &People::JudgeHathorne(None), - &People::ReverendJohnHale(None), - &People::MarshalHerrick(None), - ] - } - })), - ] + pub fn load_characters() -> Vec { + let file_path = format!("{}/data/characters.ron", env!("CARGO_MANIFEST_DIR")); + let file = File::open(file_path).expect("Error opening character file"); + + // Return deserialized file content. + match from_reader(file) { + Ok(x) => x, + Err(e) => { + println!("Failed to load config: {}", e); + + std::process::exit(1); + } + } } } impl Distribution for Standard { fn sample(&self, rng: &mut R) -> People { match rng.gen_range(0..=21) { - 0 => People::AbigailWilliams(None), - 1 => People::AnnPutnam(None), - 2 => People::BettyParris(None), - 3 => People::DeputyGovernorDanforth(None), - 4 => People::ElizabethProctor(None), - 5 => People::EzekielCheever(None), - 6 => People::FrancisNurse(None), - 7 => People::GilesCorey(None), - 8 => People::GoodyOsborne(None), - 9 => People::JohnProctor(None), - 10 => People::JudgeHathorne(None), - 11 => People::MarshalHerrick(None), - 12 => People::MarthaCorey(None), - 13 => People::MaryWarren(None), - 14 => People::RebeccaNurse(None), - 15 => People::ReverendJohnHale(None), - 16 => People::ReverendParris(None), - 18 => People::SarahGood(None), - 19 => People::SusannaWalcott(None), - 20 => People::ThomasPutnam(None), - 21 => People::Tituba(None), + 0 => People::AbigailWilliams, + 1 => People::AnnPutnam, + 2 => People::BettyParris, + 3 => People::DeputyGovernorDanforth, + 4 => People::ElizabethProctor, + 5 => People::EzekielCheever, + 6 => People::FrancisNurse, + 7 => People::GilesCorey, + 8 => People::GoodyOsborne, + 9 => People::JohnProctor, + 10 => People::JudgeHathorne, + 11 => People::MarshalHerrick, + 12 => People::MarthaCorey, + 13 => People::MaryWarren, + 14 => People::RebeccaNurse, + 15 => People::ReverendJohnHale, + 16 => People::ReverendParris, + 18 => People::SarahGood, + 19 => People::SusannaWalcott, + 20 => People::ThomasPutnam, + 21 => People::Tituba, _ => unreachable!(), } } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum SurvivalStatus { PlayerMassacred, PlayerLived, diff --git a/src/events.rs b/src/events.rs index 265d0e4..cd2e7b9 100644 --- a/src/events.rs +++ b/src/events.rs @@ -1,25 +1,41 @@ +// Copyright (c) 2024 Cutieguwu | Olivia Brooks +// +// -*- coding: utf-8 -*- +// @Title: Culling of the Sheep +// @Author: Cutieguwu | Olivia Brooks +// @Description: Components for events. +// +// @Script: events.rs +// @Date Created: 03 Dec, 2024 +// @Last Modified: 08 Dec, 2024 +// @Last Modified by: Cutieguwu | Olivia Brooks +// -------------------------------------------- + + use std::i8; -use rand::{ - distributions::{Distribution, Standard}, - rngs::ThreadRng, -}; +use rand::distributions::{Distribution, Standard}; +use rand::rngs::ThreadRng; +use rand::seq::SliceRandom; -use crate::characters::{ - Character, - People, - SurvivalStatus, -}; +use crate::characters::{Character, SurvivalStatus}; +use crate::utils::input; + +const BREAKLINE: &'static str = "-------------------------------------"; + +pub fn print_breakline() { + println!("\n{}\n", BREAKLINE) +} #[allow(unused_variables)] pub trait EventHandle { fn handle<'a>( self: &Self, - rng: &'a ThreadRng, + rng: &'a mut ThreadRng, player: &'a mut Character, - characters: &'a mut Vec, - ) -> EventReturn { - EventReturn::None + characters: &'a mut Vec, + ) -> SurvivalStatus { + todo!() } } @@ -29,19 +45,81 @@ pub struct Trial; impl EventHandle for Trial { fn handle<'a>( self: &Self, - rng: &'a ThreadRng, + rng: &'a mut ThreadRng, player: &'a mut Character, - characters: &'a mut Vec, - ) -> EventReturn { - let mut player_status: SurvivalStatus = SurvivalStatus::PlayerDied; + characters: &'a mut Vec, + ) -> SurvivalStatus { + let doppelgangers = characters.clone(); + + print_breakline(); println!("You have been summoned before the court.\n"); + println!("[0] You may appease the court by admitting to witchcraft."); + println!("[1] Or you may die."); - if player.standing.clone().calculate() >= {0 as i8} { - player_status = SurvivalStatus::PlayerLived + // Get choice from player, return early if they choose to die. + loop { + match input().as_str() { + // Attempt to accuse + "0" => break, + // Die a witch + "1" => return SurvivalStatus::PlayerDied, + _ => () + }; } - EventReturn::Survival(player_status) + print_breakline(); + + println!("Someone must have coerced you into following Satan."); + println!("Attempt to save yourself; who will you accuse?\n"); + + // Randomly select four characters. + let accusable: Vec<&Character> = doppelgangers + .choose_multiple(rng, 4) + .collect(); + + for character in &accusable { + println!( + "[{:?}] {}", + accusable + .iter() + .position(|x| x == character) + .expect("Could not find character."), + character.name + ) + } + + let accusee_index: u8 = loop { + let temp: Option = match input().as_str() { + "0" => Some(0), + "1" => Some(1), + "2" => Some(2), + "3" => Some(3), + _ => None + }; + + if temp != None { + break temp.unwrap() as u8 + } + }; + + push_enemy( + player, + accusable[accusee_index as usize], + characters + ); + + print_breakline(); + + println!("The court will now decide your fate considering."); + + // Have the player stand trial. + // A wild accusation may not be enough to save them. + if player.standing.clone().calculate() > {0 as i8} { + SurvivalStatus::PlayerLived + } else { + SurvivalStatus::PlayerDied + } } } @@ -49,33 +127,83 @@ impl EventHandle for Trial { pub struct WildAccusation; impl EventHandle for WildAccusation { + #[allow(unused_variables)] fn handle<'a>( self: &Self, - rng: &'a ThreadRng, + rng: &'a mut ThreadRng, player: &'a mut Character, - characters: &'a mut Vec, - ) -> EventReturn { - EventReturn::None + characters: &'a mut Vec, + ) -> SurvivalStatus { + let doppelgangers = characters.clone(); + let accuser: &Character = doppelgangers + .choose(rng) + .expect("Failed to fetch character as accuser."); + + let accusee: &Character = doppelgangers + .choose(rng) + .expect("Failed to fetch character as accusee."); + + print_breakline(); + + println!("{:?} accused {:?}", accuser, accusee); + + push_enemy(accuser, accusee, characters); + + SurvivalStatus::PlayerLived } } +fn push_enemy<'a>( + accuser: &Character, + accusee: &Character, + characters: &'a mut Vec, +) -> Vec { + let characters = characters; + // If accuser not an enemy, add them as such. + if None == accusee.standing.enemies + .iter() + .find(|x| *x == &accuser.id) + { + accusee.standing.enemies.to_owned().push(accuser.id.clone()); + + let position: usize = characters.iter() + .position(|x| x.id == accusee.id) + .unwrap(); + + characters[position] = accusee.clone(); + }; + + characters.to_vec() +} + #[derive(Debug)] pub enum Events { Trial(Trial), + WildAccusation(WildAccusation), } -impl Distribution for Standard { - fn sample(&self, rng: &mut R) -> Events { - match rng.gen_range(0..=0) { - 0 => Events::Trial(Trial), - 1 => todo!(), - _ => unreachable!(), +impl EventHandle for Events { + fn handle<'a>( + self: &Self, + rng: &'a mut ThreadRng, + player: &'a mut Character, + characters: &'a mut Vec, + ) -> SurvivalStatus { + match self { + Events::Trial(trial) => trial.handle(rng, player, characters), + Events::WildAccusation(wild_accusation) => { + wild_accusation.handle(rng, player, characters) + }, } } } -#[derive(Debug)] -pub enum EventReturn { - Survival(SurvivalStatus), - None, - } \ No newline at end of file +impl Distribution for Standard { + fn sample(&self, rng: &mut R) -> Events { + match rng.gen_range(0..=1) { + 0 => Events::Trial(Trial), + 1 => Events::WildAccusation(WildAccusation), + _ => unreachable!(), + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7b083d1..dc24ced 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,90 +1,79 @@ +// Copyright (c) 2024 Cutieguwu | Olivia Brooks +// +// -*- coding: utf-8 -*- +// @Title: Culling of the Sheep +// @Author: Cutieguwu | Olivia Brooks +// @Description: A game to demonstrate the Salem Witch Trials. +// +// @Script: main.rs +// @Date Created: 26 Nov, 2024 +// @Last Modified: 09 Dec, 2024 +// @Last Modified by: Cutieguwu | Olivia Brooks +// -------------------------------------------- + + mod characters; mod events; mod utils; -use characters::{ - Character, - People, - SurvivalStatus, -}; +use rand::{random, thread_rng}; +use rand::rngs::ThreadRng; -use events::{ - EventHandle, - EventReturn, - Events, - Trial, -}; +use utils::{fmt_args, ArgType, FlagType}; -use rand::{ - random, - thread_rng, - rngs::ThreadRng -}; - -use utils::{ - fmt_args, - ArgType, - FlagType, -}; +use crate::characters::{Character, People, SurvivalStatus}; +use crate::events::{EventHandle, Events, print_breakline}; //Number of game rounds. const ROUNDS: u8 = 15; fn main() { - 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 = Character::build_player(Gamemode::get(&cmd_args)); + let mut characters: Vec = People::load_characters(); let mut end_status: SurvivalStatus = SurvivalStatus::PlayerLived; - + // Set player default values based on gamemode. + let mut player: Character = Character::build_player( + &mut characters, + Gamemode::get(fmt_args()), + ); + let mut rng: ThreadRng = thread_rng(); + // Game loop. for r in 0..=ROUNDS { - end_status = run_event( - &rng, + end_status = random::().handle( + &mut rng, &mut player, &mut characters, - random(), ); - if matches!(end_status, SurvivalStatus::PlayerLived) { break } + dbg!(&characters); - 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), - ) + if characters.len() == 1 { + end_status = SurvivalStatus::PlayerMassacred; }; - + + if end_status == SurvivalStatus::PlayerDied + || end_status == SurvivalStatus::PlayerMassacred + || r == ROUNDS + { + break + } }; + print_breakline(); + + // Game end result. 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?"), - }; -} - -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!(), + SurvivalStatus::PlayerLived => { + println!("You survived, but at what cost?"); + println!("How many died to save you?"); }, - } + SurvivalStatus::PlayerMassacred => { + println!("You killed everyone."); + println!("I ask, at what end will you yield."); + println!("Care you not for the life of others?"); + }, + }; } #[derive(Debug)] @@ -100,7 +89,7 @@ pub enum Gamemode { } impl Gamemode { - fn get(cmd_args: &Vec) -> Gamemode { + fn get(cmd_args: Vec) -> Gamemode { if cmd_args.len() != 1 { match &cmd_args[1] { ArgType::Command(command) => match command.as_str() { diff --git a/src/utils.rs b/src/utils.rs index b3b2a63..c03c816 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,20 @@ -use std::{ - env::args, - io, ops::Deref, -}; +// Copyright (c) 2024 Cutieguwu | Olivia Brooks +// +// -*- coding: utf-8 -*- +// @Title: Culling of the Sheep +// @Author: Cutieguwu | Olivia Brooks +// @Description: Utils for cli argument handling. +// +// @Script: characters.rs +// @Date Created: 03 Dec, 2024 +// @Last Modified: 10 Dec, 2024 +// @Last Modified by: Cutieguwu | Olivia Brooks +// -------------------------------------------- + + +use std::env::args; +use std::io; +use std::str::Chars; const DASH: char = 45 as u8 as char; @@ -23,19 +36,19 @@ pub enum FlagType { pub fn fmt_args() -> Vec { let mut args_vec:Vec = Vec::new(); - for obj in args() { + for obj in args() { args_vec.push(match try_flag(&obj) { None => ArgType::Command(obj), Some(flag) => ArgType::Flag(flag), - }) + }); }; args_vec[0] = ArgType::Binary(match &args_vec[0] { - ArgType::Command(command) => command.clone(), + ArgType::Command(command) => command.to_owned(), err => panic!( "Expected ArgType::Command at args_vec[0], found {:?}", err, - ) + ), }); args_vec @@ -43,12 +56,12 @@ pub fn fmt_args() -> Vec { fn try_flag<'a>(arg: &'a String) -> Option { // Short term var to reduce number of chars() calls. - let mut arg_chars = arg.chars(); + let arg_chars: Chars = arg.chars(); - if arg_chars.nth(1).unwrap() == DASH { + if arg_chars.clone().nth(1).unwrap() == DASH { //eg. --my-flag Some(FlagType::Long(break_flag_long(arg.clone()))) - } else if arg_chars.nth(0).unwrap() == DASH { + } else if arg_chars.clone().nth(0).unwrap() == DASH { //eg. -Syu Some(FlagType::Short(break_flag_short(arg.clone()))) } else { @@ -74,8 +87,9 @@ pub fn input() -> String { let mut input_buffer: String = String::new(); io::stdin() - .read_line( &mut input_buffer) + .read_line(&mut input_buffer) .expect("Failed to read line"); - input_buffer + // Clear special characters and return as String + input_buffer.trim_end_matches(&['\r', '\n'][..]).to_string() } \ No newline at end of file