change everything
This commit is contained in:
parent
f8883df83d
commit
dd1bfe56c6
1 changed files with 69 additions and 76 deletions
145
src/main.rs
145
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::{Deserialize, Serialize};
|
||||||
use serde_json::Result;
|
use serde_json::Result;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
@ -25,39 +26,32 @@ struct CAHCardSet {
|
||||||
black: Option<Vec<CAHCardBlack>>,
|
black: Option<Vec<CAHCardBlack>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// struct CAHPlayer {
|
#[derive(Default)]
|
||||||
// white_cards: Vec<CAHCardWhite>,
|
struct CAHPlayer {
|
||||||
// black_cards: Vec<CAHCardBlack>,
|
// white: Vec<CAHCardWhite>,
|
||||||
// is_czar: bool,
|
// black: Vec<CAHCardBlack>,
|
||||||
// }
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct CAHGame {
|
struct CAHGame {
|
||||||
white_cards: Vec<CAHCardWhite>,
|
white: Vec<CAHCardWhite>,
|
||||||
black_cards: Vec<CAHCardBlack>,
|
black: Vec<CAHCardBlack>,
|
||||||
// white_discard: Vec<CAHCardWhite>,
|
// white_discard: Vec<CAHCardWhite>,
|
||||||
// black_discard: Vec<CAHCardBlack>,
|
// black_discard: Vec<CAHCardBlack>,
|
||||||
// game_active: bool,
|
// game_active: bool,
|
||||||
// players: Vec<CAHPlayer>,
|
players: Vec<CAHPlayer>,
|
||||||
// czar: Option<*mut CAHPlayer>,
|
// czar: &CAHPlayer,
|
||||||
|
// current_black: Option<CAHCardBlack>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CAHGame {
|
impl CAHGame {
|
||||||
fn build_decks(&mut self, cards_json: Vec<CAHCardSet>) {
|
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 {
|
for pack in cards_json {
|
||||||
if let Some(white_cards) = pack.white {
|
if let Some(white) = pack.white {
|
||||||
for card in white_cards {
|
self.white.extend(white)
|
||||||
self.white_cards.push(card)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if let Some(black_cards) = pack.black {
|
if let Some(black) = pack.black {
|
||||||
for card in black_cards {
|
self.black.extend(black)
|
||||||
self.black_cards.push(card)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,31 +59,43 @@ impl CAHGame {
|
||||||
fn deck_counts(&self) {
|
fn deck_counts(&self) {
|
||||||
println!(
|
println!(
|
||||||
"Deck Counts:\n {} White cards\n {} Black cards",
|
"Deck Counts:\n {} White cards\n {} Black cards",
|
||||||
self.white_cards.len(),
|
self.white.len(),
|
||||||
self.black_cards.len()
|
self.black.len()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pick_one_white(&self) {
|
fn draw_one_white(&mut self) -> Result<CAHCardWhite> {
|
||||||
let card = self.white_cards.choose(&mut rand::thread_rng()).unwrap();
|
let deck = &mut self.white;
|
||||||
println!(
|
|
||||||
"{}\n {}",
|
// this feels sloppy
|
||||||
card.pack,
|
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
|
||||||
card.text,
|
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) {
|
fn draw_one_black(&mut self) -> Result<CAHCardBlack> {
|
||||||
let card = self.black_cards.choose(&mut rand::thread_rng()).unwrap();
|
let deck = &mut self.black;
|
||||||
let div = "--------------------------";
|
|
||||||
println!(
|
// this feels sloppy
|
||||||
"{}\n Pack: {}\n Pick: {}\n Text: {}\n{}",
|
if let Some(index) = (0..deck.len()).choose(&mut thread_rng()) {
|
||||||
div,
|
Ok(deck.swap_remove(index))
|
||||||
card.pack,
|
} else {
|
||||||
card.pick.to_string(),
|
Ok(CAHCardBlack {
|
||||||
card.text,
|
text: "Error.\n\nbtw if you see this tell me I'm lazy :)".to_string(),
|
||||||
div
|
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)
|
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<()> {
|
fn main() -> Result<()> {
|
||||||
// let cards_input_path: &str = "data/test.json";
|
// let cards_input_path: &str = "data/test.json";
|
||||||
let cards_input_path: &str = "data/cah-cards-full.json";
|
let cards_input_path: &str = "data/cah-cards-full.json";
|
||||||
let cards_json: Vec<CAHCardSet> = load_json(cards_input_path)?;
|
let cards_json: Vec<CAHCardSet> = load_json(cards_input_path)?;
|
||||||
packs_report(cards_json.clone());
|
|
||||||
|
|
||||||
let mut cah_game = CAHGame {
|
let mut cah_game = CAHGame {
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
cah_game.build_decks(cards_json);
|
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()?;
|
||||||
|
let div = "--------------------------";
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
cah_game.deck_counts();
|
cah_game.deck_counts();
|
||||||
|
|
||||||
cah_game.pick_one_black();
|
|
||||||
|
|
||||||
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue