pub struct State<P, Q> { /* private fields */ }Expand description
State split into a public zone and per-player private zones, plus the match’s random generator.
Redaction is mechanical: a player’s observation is the public zone plus
their own private entry, so there is no field to forget to strip. The
backing map is not exposed; games reach private data through the accessors.
Using this type is a convenience, not a requirement (Game::State is an
associated type and can be any shape) but it makes the common hidden-info
game turnkey.
The Prng lives here so it clones, serializes, and rewinds together with
everything else: snapshot and resume are O(1), and a Reversible undo
record can restore the stream position (see ARCHITECTURE.md). Games with
their own State type should embed a Prng the same way.
Implementations§
Source§impl<P, Q> State<P, Q>
impl<P, Q> State<P, Q>
Sourcepub const fn new(public: P, seed: u64) -> Self
pub const fn new(public: P, seed: u64) -> Self
Creates a state with the given public zone, no private entries, and a
generator seeded from seed.
Sourcepub const fn public_mut(&mut self) -> &mut P
pub const fn public_mut(&mut self) -> &mut P
Returns the public zone for mutation inside apply.
Sourcepub fn insert_private(&mut self, player: PlayerId, value: Q) -> Option<Q>
pub fn insert_private(&mut self, player: PlayerId, value: Q) -> Option<Q>
Sets player’s private zone, returning the previous value if any.
Sourcepub fn private(&self, player: PlayerId) -> Option<&Q>
pub fn private(&self, player: PlayerId) -> Option<&Q>
Returns player’s private zone, or None if they have none.
Sourcepub fn private_mut(&mut self, player: PlayerId) -> Option<&mut Q>
pub fn private_mut(&mut self, player: PlayerId) -> Option<&mut Q>
Returns a mutable reference to player’s private zone.
Sourcepub fn remove_private(&mut self, player: PlayerId) -> Option<Q>
pub fn remove_private(&mut self, player: PlayerId) -> Option<Q>
Removes and returns player’s private zone, if any.
Needed to reverse a deal exactly: undoing a chance move that dealt a
private card must leave no stale entry behind, so a Reversible::undo
calls this to drop the card it handed out.
§Example
use turnbase::{PlayerId, State};
let mut state: State<(), u8> = State::new((), 0);
state.insert_private(PlayerId::new(0), 7); // deal
assert_eq!(state.remove_private(PlayerId::new(0)), Some(7)); // undo the deal
assert_eq!(state.private(PlayerId::new(0)), None);Source§impl<P: Clone, Q: Clone> State<P, Q>
impl<P: Clone, Q: Clone> State<P, Q>
Sourcepub fn view_for(&self, viewer: Option<PlayerId>) -> PlayerView<P, Q>
pub fn view_for(&self, viewer: Option<PlayerId>) -> PlayerView<P, Q>
Produces the standard observation for viewer: the public zone plus
their own private zone (None viewer, a spectator, sees public only).
This is the default visibility rule. Games whose rule is inverted or
otherwise non-standard (e.g. Hanabi) build their PlayerView directly
instead of calling this. Clones the observed data, so games with a large
public zone may prefer a cheaper custom projection.