docs n stuff

This commit is contained in:
Adam 2024-04-06 22:38:00 -04:00
parent 7c2c258014
commit fd30fe07cd

View file

@ -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<String>,
/// Whether or not this is an official card pack
official: bool,
/// White card data
white: Option<Vec<CAHCardWhite>>,
/// Black card data
black: Option<Vec<CAHCardBlack>>,
}
/// A struct that represents a player
#[derive(Default)]
struct CAHPlayer {
// white: Vec<CAHCardWhite>,
// black: Vec<CAHCardBlack>,
/// The player's hand
white: Vec<CAHCardWhite>,
/// The player's wins
black: Vec<CAHCardBlack>,
}
/// The game master
#[derive(Default)]
struct CAHGame {
/// White draw pile
white: Vec<CAHCardWhite>,
/// Black draw pile
black: Vec<CAHCardBlack>,
// white_discard: Vec<CAHCardWhite>,
// black_discard: Vec<CAHCardBlack>,
// game_active: bool,
/// White discard pile
white_discard: Vec<CAHCardWhite>,
/// Black discard pile
black_discard: Vec<CAHCardBlack>,
/// Indicates game active/game over
game_active: bool,
/// List of current players
players: Vec<CAHPlayer>,
// /// Reference to current card czar
// czar: &CAHPlayer,
// current_black: Option<CAHCardBlack>,
/// Black card for the current round
current_black: Option<CAHCardBlack>,
}
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<CAHCardSet>) {
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<CAHCardWhite> {
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<CAHCardBlack> {
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<CAHCardBlack>> {
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<Vec<CAHCardSet>> {
let data: String = fs::read_to_string(path).expect("Error reading file");
let jayson: Vec<CAHCardSet> = serde_json::from_str(&data)?;
@ -107,37 +148,43 @@ fn load_json(path: &str) -> Result<Vec<CAHCardSet>> {
}
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<CAHCardSet> = 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(())
}