change everything
This commit is contained in:
parent
f8883df83d
commit
dd1bfe56c6
1 changed files with 69 additions and 76 deletions
143
src/main.rs
143
src/main.rs
|
@ -1,4 +1,5 @@
|
|||
use rand::seq::SliceRandom;
|
||||
use rand::prelude::IteratorRandom;
|
||||
use rand::thread_rng;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Result;
|
||||
use std::fs;
|
||||
|
@ -25,39 +26,32 @@ struct CAHCardSet {
|
|||
black: Option<Vec<CAHCardBlack>>,
|
||||
}
|
||||
|
||||
// struct CAHPlayer {
|
||||
// white_cards: Vec<CAHCardWhite>,
|
||||
// black_cards: Vec<CAHCardBlack>,
|
||||
// is_czar: bool,
|
||||
// }
|
||||
#[derive(Default)]
|
||||
struct CAHPlayer {
|
||||
// white: Vec<CAHCardWhite>,
|
||||
// black: Vec<CAHCardBlack>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct CAHGame {
|
||||
white_cards: Vec<CAHCardWhite>,
|
||||
black_cards: Vec<CAHCardBlack>,
|
||||
white: Vec<CAHCardWhite>,
|
||||
black: Vec<CAHCardBlack>,
|
||||
// white_discard: Vec<CAHCardWhite>,
|
||||
// black_discard: Vec<CAHCardBlack>,
|
||||
// game_active: bool,
|
||||
// players: Vec<CAHPlayer>,
|
||||
// czar: Option<*mut CAHPlayer>,
|
||||
players: Vec<CAHPlayer>,
|
||||
// czar: &CAHPlayer,
|
||||
// current_black: Option<CAHCardBlack>,
|
||||
}
|
||||
|
||||
impl CAHGame {
|
||||
fn build_decks(&mut self, cards_json: Vec<CAHCardSet>) {
|
||||
// clear deck before adding cards
|
||||
self.white_cards = Vec::new();
|
||||
self.black_cards = Vec::new();
|
||||
|
||||
for pack in cards_json {
|
||||
if let Some(white_cards) = pack.white {
|
||||
for card in white_cards {
|
||||
self.white_cards.push(card)
|
||||
}
|
||||
}
|
||||
if let Some(black_cards) = pack.black {
|
||||
for card in black_cards {
|
||||
self.black_cards.push(card)
|
||||
if let Some(white) = pack.white {
|
||||
self.white.extend(white)
|
||||
}
|
||||
if let Some(black) = pack.black {
|
||||
self.black.extend(black)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -65,31 +59,43 @@ impl CAHGame {
|
|||
fn deck_counts(&self) {
|
||||
println!(
|
||||
"Deck Counts:\n {} White cards\n {} Black cards",
|
||||
self.white_cards.len(),
|
||||
self.black_cards.len()
|
||||
self.white.len(),
|
||||
self.black.len()
|
||||
);
|
||||
}
|
||||
|
||||
fn pick_one_white(&self) {
|
||||
let card = self.white_cards.choose(&mut rand::thread_rng()).unwrap();
|
||||
println!(
|
||||
"{}\n {}",
|
||||
card.pack,
|
||||
card.text,
|
||||
);
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn pick_one_black(&self) {
|
||||
let card = self.black_cards.choose(&mut rand::thread_rng()).unwrap();
|
||||
let div = "--------------------------";
|
||||
println!(
|
||||
"{}\n Pack: {}\n Pick: {}\n Text: {}\n{}",
|
||||
div,
|
||||
card.pack,
|
||||
card.pick.to_string(),
|
||||
card.text,
|
||||
div
|
||||
);
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn create_player(&mut self, player: CAHPlayer) {
|
||||
self.players.push(player);
|
||||
println!("Player Created!");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -100,51 +106,38 @@ fn load_json(path: &str) -> Result<Vec<CAHCardSet>> {
|
|||
Ok(jayson)
|
||||
}
|
||||
|
||||
fn packs_report(json: Vec<CAHCardSet>) {
|
||||
let line = "---------------------------";
|
||||
for pack in json {
|
||||
println!("{}", &line);
|
||||
let name = pack.name;
|
||||
println!("Name: {:?}", name);
|
||||
let desc = pack.description;
|
||||
println!("Description: {:?}", desc);
|
||||
let official = pack.official;
|
||||
println!("Official: {:?}", official);
|
||||
let white = pack.white.unwrap().len();
|
||||
println!("White Cards: {:?}", white);
|
||||
let black = pack.black.unwrap().len();
|
||||
println!("Black Cards: {:?}", black);
|
||||
}
|
||||
println!("{}", line);
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
// let cards_input_path: &str = "data/test.json";
|
||||
let cards_input_path: &str = "data/cah-cards-full.json";
|
||||
let cards_json: Vec<CAHCardSet> = load_json(cards_input_path)?;
|
||||
packs_report(cards_json.clone());
|
||||
|
||||
let mut cah_game = CAHGame {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
cah_game.build_decks(cards_json);
|
||||
|
||||
cah_game.deck_counts();
|
||||
cah_game.create_player(CAHPlayer {
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
cah_game.pick_one_black();
|
||||
// deal black
|
||||
let black_card = cah_game.draw_one_black()?;
|
||||
let div = "--------------------------";
|
||||
println!(
|
||||
"{}\nPick {}\n {}\n{}",
|
||||
div,
|
||||
black_card.pick.to_string(),
|
||||
black_card.text,
|
||||
div,
|
||||
);
|
||||
|
||||
cah_game.pick_one_white();
|
||||
cah_game.pick_one_white();
|
||||
cah_game.pick_one_white();
|
||||
cah_game.pick_one_white();
|
||||
cah_game.pick_one_white();
|
||||
cah_game.pick_one_white();
|
||||
cah_game.pick_one_white();
|
||||
cah_game.pick_one_white();
|
||||
cah_game.pick_one_white();
|
||||
cah_game.pick_one_white();
|
||||
|
||||
Ok(())
|
||||
// deal white
|
||||
for i in 1..11 {
|
||||
let card = cah_game.draw_one_white()?;
|
||||
println!("{} {}", i, card.text);
|
||||
}
|
||||
|
||||
cah_game.deck_counts();
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue