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:
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,
impl<T> Game<T>where T: Eq + Hash,
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)where
T: Copy + PartialOrd + Add<Output = T> + Sub<Output = T> + One + Bounded + ToPrimitive,
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> Display for Game<T>where
T: Eq + Hash + Copy + PartialOrd + Zero + One + ToPrimitive,
impl<T> Display for Game<T>where T: Eq + Hash + Copy + PartialOrd + Zero + One + ToPrimitive,
source§impl<T> PartialEq<Game<T>> for Game<T>where
T: Eq + Hash + PartialEq,
impl<T> PartialEq<Game<T>> for Game<T>where T: Eq + Hash + PartialEq,
impl<T> Eq for Game<T>where T: Eq + Hash + Eq,
impl<T> StructuralEq for Game<T>where T: Eq + Hash,
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> 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