Struct 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 duplicate of the value. Read more
1.0.0 · Source§

const 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 for Game<T>
where T: Eq + Hash + PartialEq,

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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> StructuralPartialEq for Game<T>
where T: Eq + Hash,

Auto Trait Implementations§

§

impl<T> Freeze for Game<T>

§

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

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.