Skip to main content

Game

Struct Game 

Source
pub struct Game { /* private fields */ }
Expand description

A game of Yahtzee for one or more players.

Players act in seat order, thirteen rounds each. The primitive input is deterministic dice injectionroll takes the complete five-die result, which makes replays and front ends trivial and keeps the rules free of randomness. Game::roll_with rolls for you behind the rand feature.

Illegal inputs are rejected with a GameError and leave the game unchanged, so interactive callers can simply retry.

Implementations§

Source§

impl Game

Source

pub fn new(players: usize) -> Self

Sets up a game.

§Panics

Panics if players is zero.

Source

pub const fn phase(&self) -> Phase

The current phase.

Source

pub const fn is_over(&self) -> bool

Whether all thirteen rounds are complete.

Source

pub const fn seat(&self) -> usize

The seat of the player to act, 0..players.

Source

pub const fn round(&self) -> u8

The current round, 0..13.

Source

pub const fn rolls_left(&self) -> u8

How many rolls remain this turn: 3 before the first roll, 0 after the third (when only scoring is left) and once the game is over.

Source

pub const fn kept(&self) -> Keep

The dice held for the pending roll; empty at the start of a turn.

Source

pub const fn dice(&self) -> Option<Dice>

The dice on the table, or None while a roll is due.

Source

pub fn scorecard(&self, seat: usize) -> &Scorecard

One player’s card.

§Panics

Panics if seat is out of range.

Source

pub fn scorecards(&self) -> &[Scorecard]

All cards in seat order.

Source

pub fn totals(&self) -> Vec<u16>

Every player’s grand total, in seat order.

Source

pub const fn view(&self) -> View<'_>

The decision context for the acting player.

Source

pub fn roll(&mut self, dice: Dice) -> Result<(), GameError>

Resolves the pending roll with a complete five-die result, which must contain the held dice.

§Errors

GameError::Finished, GameError::ActionPending, or GameError::KeptNotHeld if dice dropped a held die.

Source

pub fn roll_with<R: Rng + ?Sized>( &mut self, rng: &mut R, ) -> Result<Dice, GameError>

Rolls the unheld dice with rng and resolves the pending roll.

§Errors

GameError::Finished or GameError::ActionPending.

Source

pub fn act( &mut self, action: TurnAction, ) -> Result<Option<ScoreDelta>, GameError>

Applies the acting player’s decision: hold and reroll, or score.

Scoring returns the ScoreDelta and advances to the next seat; a reroll returns None and awaits the next roll.

§Errors

GameError::Finished, GameError::RollPending, GameError::NoRerollsLeft, GameError::KeepNotInDice, or GameError::IllegalCategory; the game is left unchanged.

Trait Implementations§

Source§

impl Clone for Game

Source§

fn clone(&self) -> Game

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Game

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Game

Source§

impl Hash for Game

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Game

Source§

fn eq(&self, other: &Game) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Game

Auto Trait Implementations§

§

impl Freeze for Game

§

impl RefUnwindSafe for Game

§

impl Send for Game

§

impl Sync for Game

§

impl Unpin for Game

§

impl UnsafeUnpin for Game

§

impl UnwindSafe for Game

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.