Skip to main content

TurnRules

Trait TurnRules 

Source
pub trait TurnRules {
    type Command;
    type Event;
    type Error;
    type Outcome;

    // Required methods
    fn check(&self, command: &Self::Command) -> Result<(), Self::Error>;
    fn apply(
        &mut self,
        command: &Self::Command,
    ) -> Result<Vec<Self::Event>, Self::Error>;
    fn legal_commands(&self) -> Vec<Self::Command>;
    fn outcome(&self) -> Option<Self::Outcome>;
}
Expand description

A turn-based game’s state, and the commands that change it.

§Contract

  • Deterministic. The same state and command always give the same events and the same new state. Luck comes from a seeded RNG kept in the state (SimRng), never from outside, so a game can be rebuilt by replaying its commands.
  • All or nothing. apply either changes the state and returns what happened, or refuses the command with an error and changes nothing.
  • check gives apply’s answer without changing anything, luck included.
  • legal_commands lists commands check accepts, always in the same order. A game may leave some out (when there are too many to list), and says which.
  • Once outcome is Some, the game is over and legal_commands is empty.

Required Associated Types§

Source

type Command

What a player (or an AI) asks for.

Source

type Event

What happened when a command was applied.

Source

type Error

Why a command was refused.

Source

type Outcome

How a game ended.

Required Methods§

Source

fn check(&self, command: &Self::Command) -> Result<(), Self::Error>

Whether apply would accept command, and if not why, without changing anything.

Source

fn apply( &mut self, command: &Self::Command, ) -> Result<Vec<Self::Event>, Self::Error>

Applies command and returns what happened, or refuses it and changes nothing.

Source

fn legal_commands(&self) -> Vec<Self::Command>

The commands check accepts now, in a fixed order.

Source

fn outcome(&self) -> Option<Self::Outcome>

How the game ended, once it has.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§