fake it til you make it

This commit is contained in:
Adam 2024-04-12 02:04:58 -04:00
parent 2a6c17f55c
commit e393e1ad99
2 changed files with 43 additions and 22 deletions

View file

@ -65,6 +65,8 @@ pub struct CAHCardSet {
/// The game master
#[derive(Debug, Default)]
pub struct CAHGame {
/// The name of the game
game_name: String,
/// White draw pile
white: Vec<CAHCardWhite>,
/// Black draw pile
@ -96,23 +98,17 @@ impl CAHGame {
}
}
}
pub fn new(host: CAHPlayer, decks: Vec<CAHCardSet>) -> Result<CAHGame> {
pub fn new(name: String, host: CAHPlayer, decks: Vec<CAHCardSet>) -> Result<CAHGame> {
let mut game = CAHGame {
..Default::default()
};
println!("Creating game {}", &name);
game.game_name = name;
game.build_decks(decks);
game.create_player(host);
let _ = game.create_player(host);
let _ = game.deal_black()?;
let mut hand_buf = vec![];
for _ in 0..10 {
hand_buf.push(game.draw_one_white()?);
}
game.players[0].white.extend(hand_buf);
println!("Player hand dealt");
println!("Game Created!");
Ok(game)
}
@ -166,8 +162,18 @@ impl CAHGame {
}
/// Create a new player and add them to the game.
fn create_player(&mut self, player: CAHPlayer) {
pub fn create_player(&mut self, mut player: CAHPlayer) -> Result<()> {
println!("Creating player {} as {:?}", &player.player_name, &player.role);
let mut hand_buf = vec![];
for _ in 0..10 {
hand_buf.push(self.draw_one_white()?);
}
println!("Dealing hand for {}", &player.player_name);
let _ = &player.white.extend(hand_buf);
self.players.push(player);
println!("Player Created!");
Ok(())
}
}

View file

@ -1,5 +1,5 @@
use serde_json::Result;
use std::{alloc::Layout, fs};
use std::fs;
#[allow(non_snake_case)]
pub mod CAHd_game;
@ -16,20 +16,35 @@ fn load_json(path: &str) -> Result<Vec<CAHCardSet>> {
fn main() -> Result<()> {
// choose decks
let cards_input_path: &str = "data/cah-cards-full.json";
let cards_json: Vec<CAHCardSet> = load_json(cards_input_path)?;
let chosen_packs: Vec<CAHCardSet> = load_json(cards_input_path)?;
// create game with/for player 0
let game = CAHGame::new(
CAHPlayer {
let test_player0 = CAHPlayer {
player_name: "Adam".to_string(),
role: PlayerRole::Host,
white: vec![],
black: vec![],
},
cards_json,
);
};
let test_player1 = CAHPlayer {
player_name: "Ferris".to_string(),
role: PlayerRole::Player,
white: vec![],
black: vec![],
};
// make some games
let mut games: Vec<CAHGame> = vec![];
// create game with/for player 0
games.push(CAHGame::new(
"Test0".to_string(),
test_player0,
chosen_packs,
)?);
// ready player 1
let _ = games[0].create_player(test_player1);
println!("{:#?}", game?.players[0]);
Ok(())
}