pub struct Game<T>{ /* 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:
§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>
impl<T> Game<T>
Sourcepub fn new(rule: Rule, board: Board<T>) -> Self
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);
Sourcepub const fn rule(&self) -> &Rule
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);
Sourcepub const fn board(&self) -> &Board<T>
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);
Sourcepub fn advance(&mut self)
pub fn advance(&mut self)
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§
impl<T> Eq for Game<T>
impl<T> StructuralPartialEq for Game<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more