From fd30fe07cd9629764d4292127353e34c64797c98 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Sat, 6 Apr 2024 22:38:00 -0400 Subject: [PATCH] docs n stuff --- src/main.rs | 81 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index f0b5b79..0017b85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,47 +4,74 @@ use serde::{Deserialize, Serialize}; use serde_json::Result; use std::fs; +/// A CAH white card #[derive(Serialize, Deserialize)] struct CAHCardWhite { + /// Card text text: String, + /// ID of the pack it came from pack: u8, } +/// A CAH black card #[derive(Serialize, Deserialize)] struct CAHCardBlack { + /// Card text text: String, + /// Amount of cards to submit for judging pick: u8, + /// ID of the pack it came from pack: u8, } +/// A CAH pack #[derive(Serialize, Deserialize)] struct CAHCardSet { + /// Name of the pack name: String, + /// Pack Description description: Option, + /// Whether or not this is an official card pack official: bool, + /// White card data white: Option>, + /// Black card data black: Option>, } +/// A struct that represents a player #[derive(Default)] struct CAHPlayer { - // white: Vec, - // black: Vec, + /// The player's hand + white: Vec, + /// The player's wins + black: Vec, } +/// The game master #[derive(Default)] struct CAHGame { + /// White draw pile white: Vec, + /// Black draw pile black: Vec, - // white_discard: Vec, - // black_discard: Vec, - // game_active: bool, + /// White discard pile + white_discard: Vec, + /// Black discard pile + black_discard: Vec, + /// Indicates game active/game over + game_active: bool, + /// List of current players players: Vec, + // /// Reference to current card czar // czar: &CAHPlayer, - // current_black: Option, + /// Black card for the current round + current_black: Option, } impl CAHGame { + /// Build game decks from input data for game start. + /// This should only run once and at startup. fn build_decks(&mut self, cards_json: Vec) { for pack in cards_json { if let Some(white) = pack.white { @@ -56,6 +83,8 @@ impl CAHGame { } } + /// Log counts of current drawable cards + /// For testing fn deck_counts(&self) { println!( "Deck Counts:\n {} White cards\n {} Black cards", @@ -64,6 +93,7 @@ impl CAHGame { ); } + /// Draw one white card at random from play deck. fn draw_one_white(&mut self) -> Result { let deck = &mut self.white; @@ -78,6 +108,7 @@ impl CAHGame { } } + /// Draw one black card at random from play deck. fn draw_one_black(&mut self) -> Result { let deck = &mut self.black; @@ -92,13 +123,23 @@ impl CAHGame { }) } } + + /// Deal a black card and use it for the current round + /// Returns a reference to the card + fn deal_black(&mut self) -> Result<&Option> { + self.current_black = Some(self.draw_one_black()?); + Ok(&self.current_black) + } + + /// Create a new player and add them to the game. fn create_player(&mut self, player: CAHPlayer) { self.players.push(player); println!("Player Created!"); } } +/// Parse json for card data fn load_json(path: &str) -> Result> { let data: String = fs::read_to_string(path).expect("Error reading file"); let jayson: Vec = serde_json::from_str(&data)?; @@ -107,37 +148,43 @@ fn load_json(path: &str) -> Result> { } fn main() -> Result<()> { - // let cards_input_path: &str = "data/test.json"; + // choose decks let cards_input_path: &str = "data/cah-cards-full.json"; let cards_json: Vec = load_json(cards_input_path)?; + // setup let mut cah_game = CAHGame { ..Default::default() }; - cah_game.build_decks(cards_json); cah_game.deck_counts(); cah_game.create_player(CAHPlayer { ..Default::default() }); - // deal black - let black_card = cah_game.draw_one_black()?; + // sloppy ui stuff let div = "--------------------------"; - println!( - "{}\nPick {}\n {}\n{}", - div, - black_card.pick.to_string(), - black_card.text, - div, - ); + + // deal black + if let Some(black_card) = cah_game.deal_black()? { + println!( + "{}\nPick {}\n {}\n{}", + div, + black_card.pick.to_string(), + black_card.text, + div, + ); + } // deal white for i in 1..11 { let card = cah_game.draw_one_white()?; println!("{} {}", i, card.text); } + println!("{}", div); cah_game.deck_counts(); + + // fin Ok(()) }