From 955e37cfb390f5ea4983f52db3ccb1b451e3dcb6 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Thu, 16 Apr 2026 21:06:56 +0100 Subject: [PATCH] game:playerbuilder: First stage of game complete. The first stage of the game is collecting the relevant players who want to play. We can't deal out any cards, nor do an election, till this is done. The way I've codified this is by having an opaque PlayerBuilder struct with some helpers to add "new players" - essentially just empty decks of cards. Once we're done, we "freeze" or "fix" this set of players by returning a Boxed array representing the contents. Ridiculously simple API. --- src/game/mod.rs | 1 + src/game/playerbuilder.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/game/playerbuilder.rs diff --git a/src/game/mod.rs b/src/game/mod.rs index 2d8ad78..61e02e3 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -1 +1,2 @@ pub mod deck; +pub mod playerbuilder; diff --git a/src/game/playerbuilder.rs b/src/game/playerbuilder.rs new file mode 100644 index 0000000..e7ce89d --- /dev/null +++ b/src/game/playerbuilder.rs @@ -0,0 +1,31 @@ +use crate::game::deck::Deck; + +/// Player Builder, which allows the adding of new players. +pub struct PlayerBuilder { + players: Vec, +} + +/// A fixed set of players that cannot grow - occurs once players have been +/// picked. +pub type FixedPlayers = Box<[Deck]>; + +impl PlayerBuilder { + /// Construct a new player builder. + pub fn new() -> Self { + Self { + players: Vec::new(), + } + } + + /// Add a new player with an empty deck to the builder, returning its ID. + pub fn add(&mut self) -> usize { + let id = self.players.len(); + self.players.push(Deck::new_empty()); + id + } + + /// Fix the current number of players for later game stages. + pub fn into_fixed(self) -> FixedPlayers { + self.players.into_boxed_slice() + } +}