[][src]Struct conway::universe::Universe

pub struct Universe { /* fields omitted */ }

Represents a wrapping universe in Conway's game of life.

Methods

impl Universe[src]

pub fn get_cell_state(
    &mut self,
    col: usize,
    row: usize,
    opt_player_id: Option<usize>
) -> CellState
[src]

Gets a CellState enum for cell at (col, row).

Panics

Panics if row or col are out of range.

pub fn set_unchecked(&mut self, col: usize, row: usize, new_state: CellState)[src]

Sets the state of a cell in the latest generation, with minimal checking. It doesn't support setting CellState::Fog.

Panics

Panics if an attempt is made to set an unknown cell.

pub fn set(
    &mut self,
    col: usize,
    row: usize,
    new_state: CellState,
    player_id: usize
)
[src]

Checked set - check for:

  • current cell state (can't change wall)
  • player writable region
  • fog
  • if current cell is alive, player_id matches player_id argument

If any of the above checks fail, do nothing.

Panics

Panic if player_id inside CellState does not match player_id argument.

pub fn toggle_unchecked(
    &mut self,
    col: usize,
    row: usize,
    opt_player_id: Option<usize>
) -> CellState
[src]

Switches any non-dead state to CellState::Dead. Switches CellState::Dead to CellState::Alive(opt_player_id) and clears fog for that player, if any.

This operation works in three steps:

  1. Toggle alive/dead cell in the current generation state cell grid
  2. Clear all players' cell
  3. If general cell transitioned Dead->Alive, then set requested player's cell

The new value of the cell is returned.

pub fn toggle(
    &mut self,
    col: usize,
    row: usize,
    player_id: usize
) -> ConwayResult<CellState>
[src]

Checked toggle - switch between CellState::Alive and CellState::Dead.

Errors

It is an error to toggle outside player's writable area, or to toggle a wall or an unknown cell.

pub fn new(
    width: usize,
    height: usize,
    is_server: bool,
    history: usize,
    num_players: usize,
    player_writable: Vec<Region>,
    fog_radius: usize
) -> ConwayResult<Universe>
[src]

Instantiate a new blank universe with the given width and height, in cells. The universe is at generation 1.

Note: it is easier to use BigBang to build a Universe, as that has default values that can be overridden as needed.

pub fn latest_gen(&self) -> usize[src]

Get the latest generation number (1-based).

pub fn next(&mut self) -> usize[src]

Compute the next generation. Returns the new latest generation number.

pub fn each_non_dead(
    &self,
    region: Region,
    visibility: Option<usize>,
    callback: &mut dyn FnMut(usize, usize, CellState)
)
[src]

Iterate over every non-dead cell in the universe for the current generation. region is the rectangular area used for restricting results. visibility is an optional player_id; if specified, causes cells not visible to the player to be passed as CellState::Fog to the callback.

Callback receives (col, row, cell_state).

Panics

Does numerous consistency checks on the bitmaps, and panics if inconsistencies are found.

pub fn each_non_dead_full(
    &self,
    visibility: Option<usize>,
    callback: &mut dyn FnMut(usize, usize, CellState)
)
[src]

Iterate over every non-dead cell in the universe for the current generation. visibility is an optional player_id, allowing filtering based on fog. Callback receives (col, row, cell_state).

pub fn region(&self) -> Region[src]

Get a Region of the same size as the universe.

pub fn copy_from_bit_grid(
    &mut self,
    src: &BitGrid,
    dst_region: Region,
    opt_player_id: Option<usize>
)
[src]

Copies from src BitGrid to this GenState as the player specified by opt_player_id, unless opt_player_id is None.

This function is similar to GenState::copy_from_bit_grid except that 1) when a player_id is specified, the specified player's writable region is used, and 2) the latest generation is written to.

Panics if opt_player_id is Some(player_id) and player_id is out of range.

pub fn apply(
    &mut self,
    diff: &GenStateDiff,
    visibility: Option<usize>
) -> ConwayResult<Option<usize>>
[src]

Apply diff to our Universe. Generally this is executed by the client in response to an update from the server.

Arguments

  • diff: reference to a GenStateDiff containing a base generation number, a new generation number, and a pattern showing how to make the new generation out of the base.
  • visibility: an optional player_id indicating the player to apply this for. If None, the pattern must not have any fog.

Return values

  • Ok(Some(new_gen)) if the update was applied.
  • Ok(None) if the update is valid but was not applied because either:
    • the generation to be applied is already present,
    • there is already a greater generation present, or
    • the base generation of this diff (that is, diff.gen0) could not be found. A base generation of 0 is a special case -- it is always found.
  • Err(InvalidData{reason: msg}) if the update is invalid, either because:
    • the difference between diff.gen0 and diff.gen1 is too large. Since the server knows the client's buffer size, this should not happen. In this case, no updates are made to the Universe. A base generation of 0 is a special case -- the difference is never too large.
    • the RLE pattern is invalid. NOTE: in this case, the pattern is only partially written and all other updates (e.g., increasing the generation count) are made as if it were valid.

Panics

This will panic if:

  • gen0 is not less than gen1.
  • visibility is out of range.

pub fn diff(
    &self,
    gen0: usize,
    gen1: usize,
    visibility: Option<usize>
) -> Option<GenStateDiff>
[src]

If it's possible to generate a diff between the GenStates specified by gen0 and gen1, do so. Otherwise, return None.

The optional visibility specifies the player this is viewed as.

Panics

  • Panics if gen0 >= gen1.
  • Panics if visibility is out of range.

Trait Implementations

impl CharGrid for Universe[src]

fn width(&self) -> usize[src]

Return width in cells.

fn height(&self) -> usize[src]

Return height in cells.

fn to_pattern(&self, visibility: Option<usize>) -> Pattern[src]

Returns a Pattern that describes this CharGrid as viewed by specified player if visibility.is_some(), or a fog-less view if visibility.is_none(). Read more

impl Display for Universe[src]

Auto Trait Implementations

impl Send for Universe

impl Sync for Universe

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]