Struct life_backend::Game

source ·
pub struct Game<T>where
    T: Eq + Hash,{ /* private fields */ }
Expand description

A representation of a game.

The type parameter T is used as the type of the x- and y-coordinate values for each cell.

The following operations are supported:

  • Constructing from Rule and Board
  • Advancing a generation
  • Returning the current state information

Examples

use life_backend::{Board, Game, Position, Rule};
let rule = Rule::conways_life();
let board: Board<_> = [(1, 0), (2, 1), (0, 2), (1, 2), (2, 2)] // Glider pattern
    .iter()
    .copied()
    .map(|(x, y)| Position(x, y))
    .collect();
let mut game = Game::new(rule, board);
game.advance();
let bbox = game.board().bounding_box();
assert_eq!(bbox.x(), &(0..=2));
assert_eq!(bbox.y(), &(1..=3));

Implementations§

source§

impl<T> Game<T>where T: Eq + Hash,

source

pub fn new(rule: Rule, board: Board<T>) -> Self

Creates from the specified rule and the board.

Examples
use life_backend::{Board, Game, Position, Rule};
let rule = Rule::conways_life();
let board: Board<_> = [Position(1, 0), Position(0, 1)].iter().collect();
let game = Game::new(rule, board);
source

pub const fn rule(&self) -> &Rule

Returns the rule.

Examples
use life_backend::{Board, Game, Position, Rule};
let rule = Rule::conways_life();
let board: Board<_> = [Position(1, 0), Position(0, 1)].iter().collect();
let game = Game::new(rule.clone(), board);
assert_eq!(game.rule(), &rule);
source

pub const fn board(&self) -> &Board<T>

Returns the board.

Examples
use life_backend::{Board, Game, Position, Rule};
let rule = Rule::conways_life();
let board: Board<_> = [Position(1, 0), Position(0, 1)].iter().collect();
let game = Game::new(rule, board);
let board = game.board();
let bbox = board.bounding_box();
assert_eq!(bbox.x(), &(0..=1));
assert_eq!(bbox.y(), &(0..=1));
assert_eq!(board.contains(&Position(0, 0)), false);
assert_eq!(board.contains(&Position(1, 0)), true);
assert_eq!(board.contains(&Position(0, 1)), true);
assert_eq!(board.contains(&Position(1, 1)), false);
source

pub fn advance(&mut self)where T: Copy + PartialOrd + Add<Output = T> + Sub<Output = T> + One + Bounded + ToPrimitive,

Advance the game by one generation.

Examples
use life_backend::{Board, Game, Position, Rule};
let rule = Rule::conways_life();
let board: Board<_> = [Position(0, 1), Position(1, 1), Position(2, 1)].iter().collect(); // Blinker pattern
let mut game = Game::new(rule, board);
game.advance();
let board = game.board();
let bbox = board.bounding_box();
assert_eq!(bbox.x(), &(1..=1));
assert_eq!(bbox.y(), &(0..=2));
assert_eq!(board.contains(&Position(1, 0)), true);
assert_eq!(board.contains(&Position(1, 1)), true);
assert_eq!(board.contains(&Position(1, 2)), true);

Trait Implementations§

source§

impl<T> Clone for Game<T>where T: Eq + Hash + Clone,

source§

fn clone(&self) -> Game<T>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T> Debug for Game<T>where T: Eq + Hash + Debug,

source§

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

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

impl<T> Display for Game<T>where T: Eq + Hash + Copy + PartialOrd + Zero + One + ToPrimitive,

source§

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

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

impl<T> PartialEq<Game<T>> for Game<T>where T: Eq + Hash + PartialEq,

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> Eq for Game<T>where T: Eq + Hash + Eq,

source§

impl<T> StructuralEq for Game<T>where T: Eq + Hash,

source§

impl<T> StructuralPartialEq for Game<T>where T: Eq + Hash,

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Game<T>where T: RefUnwindSafe,

§

impl<T> Send for Game<T>where T: Send,

§

impl<T> Sync for Game<T>where T: Sync,

§

impl<T> Unpin for Game<T>where T: Unpin,

§

impl<T> UnwindSafe for Game<T>where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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> ToOwned for Twhere T: Clone,

§

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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.