start cleaning up by making a mess again

This commit is contained in:
Adam 2024-04-09 04:51:30 -04:00
parent 789a9e2bbc
commit d15c9d08f8
4 changed files with 200 additions and 163 deletions

4
Cargo.lock generated
View file

@ -19,9 +19,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.12"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5"
checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c"
dependencies = [
"cfg-if",
"libc",

19
parse.js Normal file
View file

@ -0,0 +1,19 @@
const data = require("./data/cah-cards-full.json");
let whites = [];
let blacks = [];
for (pack of data) {
console.log(pack.name);
console.log("white cards: " + pack.white.length);
if (pack.white) {
whites.concat(pack.white);
}
console.log("black cards: " + pack.black.length);
if (pack.white) {
blacks.concat(pack.black);
}
}
console.log(whites.length);
console.log(blacks.length);

141
src/CAHd_game.rs Normal file
View file

@ -0,0 +1,141 @@
use rand::prelude::IteratorRandom;
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use serde_json::Result;
/// 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)]
pub 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)]
pub struct CAHPlayer {
/// Is this player host?
pub host: bool,
/// The player's hand
pub white: Vec<CAHCardWhite>,
/// The player's wins
pub black: Vec<CAHCardBlack>,
}
/// The game master
#[derive(Default)]
struct CAHGame {
/// White draw pile
white: Vec<CAHCardWhite>,
/// Black draw pile
black: Vec<CAHCardBlack>,
/// 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,
/// 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 {
self.white.extend(white)
}
if let Some(black) = pack.black {
self.black.extend(black)
}
}
}
/// Log counts of current drawable cards
/// For testing
fn deck_counts(&self) {
println!(
"Deck Counts:\n {} White cards\n {} Black cards",
self.white.len(),
self.black.len()
);
}
/// Draw one white card at random from play deck.
fn draw_one_white(&mut self) -> Result<CAHCardWhite> {
let deck = &mut self.white;
// this feels sloppy
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
Ok(deck.swap_remove(index))
} else {
Ok(CAHCardWhite {
text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
pack: 0,
})
}
}
/// Draw one black card at random from play deck.
fn draw_one_black(&mut self) -> Result<CAHCardBlack> {
let deck = &mut self.black;
// this feels sloppy
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
Ok(deck.swap_remove(index))
} else {
Ok(CAHCardBlack {
text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
pick: 0,
pack: 0,
})
}
}
/// 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!");
}
}

View file

@ -1,143 +1,9 @@
use rand::prelude::IteratorRandom;
use rand::thread_rng;
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 {
/// 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 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,
/// 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 {
self.white.extend(white)
}
if let Some(black) = pack.black {
self.black.extend(black)
}
}
}
/// Log counts of current drawable cards
/// For testing
fn deck_counts(&self) {
println!(
"Deck Counts:\n {} White cards\n {} Black cards",
self.white.len(),
self.black.len()
);
}
/// Draw one white card at random from play deck.
fn draw_one_white(&mut self) -> Result<CAHCardWhite> {
let deck = &mut self.white;
// this feels sloppy
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
Ok(deck.swap_remove(index))
} else {
Ok(CAHCardWhite {
text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
pack: 0,
})
}
}
/// Draw one black card at random from play deck.
fn draw_one_black(&mut self) -> Result<CAHCardBlack> {
let deck = &mut self.black;
// this feels sloppy
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
Ok(deck.swap_remove(index))
} else {
Ok(CAHCardBlack {
text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
pick: 0,
pack: 0,
})
}
}
/// 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!");
}
}
#[allow(non_snake_case)]
pub mod CAHd_game;
use crate::CAHd_game::*;
/// Parse json for card data
fn load_json(path: &str) -> Result<Vec<CAHCardSet>> {
@ -152,38 +18,49 @@ fn main() -> Result<()> {
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 {
// player 0 (host)
let adam: CAHPlayer = CAHPlayer {
host: true,
..Default::default()
};
cah_game.build_decks(cards_json);
cah_game.deck_counts();
cah_game.create_player(CAHPlayer {
..Default::default()
});
// create game with/for player 0
println!("{:?}", adam.host);
// 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()
// });
//
// sloppy ui stuff
let div = "--------------------------";
// let 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,
);
}
// 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();
// for i in 1..11 {
// let card = cah_game.draw_one_white()?;
// println!("{} {}", i, card.text);
// }
// println!("{}", div);
//
// cah_game.deck_counts();
// fin
Ok(())