Trait ezra::dice::Die [] [src]

pub trait Die {
    type ValueType;
    fn roll(&mut self) -> &mut Self;
    fn get_value(&self) -> Self::ValueType;
    fn values(&self) -> Vec<Self::ValueType>;
}

An abstraction representing a die. All dice implement this trait. Any object implementing Die may be rolled to randomly choose a new value for the object from a predetermined set of values. The value of the Die may be read at any time, including before being rolled for the first time.

Associated Types

The type of value the die may have.

Required Methods

Randomize the value of the die, selecting from the values specified at die construction.

Return value

A mutable reference to the Die

Examples

use ezra::dice::{Die,NumericDie};

// Construct a standard six sided die
let mut die = NumericDie::new(1, 1, 6, None);

// Randomize the value of the die
die.roll();

let value = die.get_value();
assert!(1 <= value && value <= 6); 

Since the roll method returns a reference to itself, you may chain die methods together.

let mut die = NumericDie::new(1, 1, 6, None);

// Roll the die three times and print its value
println!("After three rolls the die has value {}.", die.roll().roll().roll().get_value());

Obtain a copy of the current value of the die.

Return value

A copy of the current value of the die

Examples

use ezra::dice::{Die,NumericDie};

// Construct a standard six sided die
let mut die = NumericDie::new(1, 1, 6, None);

die.roll();

println!("The value of the die is {}.", die.get_value());

Obtain a vector of all possile values the die may take.

Return value

A vector of all possible values the die may have

Examples

use ezra::dice::{Die,NumericDie};

// Construct a standard six sided die
let die = NumericDie::new(1, 1, 6, None);

assert_eq!(die.values(), vec![1, 2, 3, 4, 5, 6])

Implementors