From 0538932ec81fc93fdbe270dbf3310bf107dd47a7 Mon Sep 17 00:00:00 2001 From: Adam <24621027+adoyle0@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:23:33 -0400 Subject: [PATCH] notes and sanity --- src/CAHd_game.rs | 65 ++++++++++++++++++++++++------------------------ src/main.rs | 12 ++++++--- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/CAHd_game.rs b/src/CAHd_game.rs index 94e62ac..427abc6 100644 --- a/src/CAHd_game.rs +++ b/src/CAHd_game.rs @@ -3,30 +3,6 @@ use rand::thread_rng; use serde::{Deserialize, Serialize}; use serde_json::Result; -/// Player roles -#[derive(Debug)] -pub enum PlayerRole { - /// Player is host - Host, - /// Player is a player in a game where another player is host - Player, - /// Player is just spectating - Spectator, -} - -/// A struct that represents a player -#[derive(Debug)] -pub struct CAHPlayer { - /// Player's username - pub player_name: String, - /// This player's role - pub role: PlayerRole, - /// The player's hand - pub white: Vec, - /// The player's wins - pub black: Vec, -} - /// A CAH white card #[derive(Debug, Serialize, Deserialize)] pub struct CAHCardWhite { @@ -62,6 +38,30 @@ pub struct CAHCardSet { black: Option>, } +/// Player roles +#[derive(Debug)] +pub enum PlayerRole { + /// Player is host + Host, + /// Player is a player in a game where another player is host + Player, + /// Player is just spectating + Spectator, +} + +/// A struct that represents a player +#[derive(Debug)] +pub struct CAHPlayer { + /// Player's username + pub player_name: String, + /// This player's role + pub role: PlayerRole, + /// The player's hand + pub white: Vec, + /// The player's wins + pub black: Vec, +} + /// The game master #[derive(Debug, Default)] pub struct CAHGame { @@ -88,7 +88,7 @@ pub struct CAHGame { 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) { + fn build_decks(&mut self, cards_json: Vec) -> Result<()> { for pack in cards_json { if let Some(white) = pack.white { self.white.extend(white) @@ -97,6 +97,8 @@ impl CAHGame { self.black.extend(black) } } + + Ok(()) } pub fn new(name: String, host: CAHPlayer, decks: Vec) -> Result { let mut game = CAHGame { @@ -105,9 +107,9 @@ impl CAHGame { println!("Creating game {}", &name); game.game_name = name; - game.build_decks(decks); - let _ = game.create_player(host); - let _ = game.deal_black()?; + game.build_decks(decks)?; + game.create_player(host)?; + game.deal_black()?; Ok(game) } @@ -154,11 +156,10 @@ 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> { + fn deal_black(&mut self) -> Result<()> { self.current_black = Some(self.draw_one_black()?); - Ok(&self.current_black) + Ok(()) } /// Create a new player and add them to the game. @@ -173,7 +174,7 @@ impl CAHGame { hand_buf.push(self.draw_one_white()?); } println!("Dealing hand for {}", &player.player_name); - let _ = &player.white.extend(hand_buf); + player.white.extend(hand_buf); self.players.push(player); diff --git a/src/main.rs b/src/main.rs index 69a0cf9..5c258cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,11 @@ fn load_json(path: &str) -> Result> { fn main() -> Result<()> { // choose decks let cards_input_path: &str = "data/cah-cards-full.json"; + + // TODO: this should be a master card database and pointers + // to the cards should be passed to the game instead of actual cards let chosen_packs: Vec = load_json(cards_input_path)?; + println!("{}", &chosen_packs.len()); let test_player0 = CAHPlayer { player_name: "Adam".to_string(), @@ -33,19 +37,21 @@ fn main() -> Result<()> { }; // make some games + // use hashmap? let mut games: Vec = vec![]; // create game with/for player 0 + // TODO: make this a new game request struct games.push(CAHGame::new( "Test0".to_string(), test_player0, chosen_packs, )?); - // ready player 1 - let _ = games[0].create_player(test_player1); + // a new game request struct but this player is a player + games[0].create_player(test_player1)?; // start round - let _ = games[0].game_start(); + games[0].game_start()?; Ok(()) }