Struct Board

Source
pub struct Board { /* private fields */ }
Expand description

Board represents the Hex board and all placed stones.

The play method can be used to place stones on the board. Note that Board has no notion of a current player and will allow to place any amount of stones in any color.

A nice human-readable format can be obtained via the Display trait:

use hexgame::{Board, Color, Coords};
let mut board = Board::new(5);
board.play(Coords::new(1, 3), Color::Black);
board.play(Coords::new(0, 2), Color::White);
println!("{}", board);

will output

 a  b  c  d  e
1\.  .  ○  .  .\1
 2\.  .  .  ●  .\2
  3\.  .  .  .  .\3
   4\.  .  .  .  .\4
    5\.  .  .  .  .\5
       a  b  c  d  e

Implementations§

Source§

impl Board

Source

pub fn new(size: CoordValue) -> Self

Create a new board with the given size. Boards are always square.

This method will panic if the size is not bounded by MIN_BOARD_SIZE and MAX_BOARD_SIZE.

Source

pub fn from_stone_matrix(stones: StoneMatrix) -> Result<Self, InvalidBoard>

Load a board from a StoneMatrix.

Source

pub fn to_stone_matrix(&self) -> StoneMatrix

Convert this board to a StoneMatrix.

Source

pub fn size(&self) -> CoordValue

Return the size of this board. Boards are always square.

Source

pub fn get_color<T: Into<CoordsOrEdge>>( &self, coords_or_edge: T, ) -> Option<Color>

Return the color at the given coordinates or edge. If no stone has been placed in the given cell, this method will return None.

Source

pub fn is_in_same_set<S: Into<CoordsOrEdge>, T: Into<CoordsOrEdge>>( &self, s: S, t: T, ) -> bool

Source

pub fn play(&mut self, coords: Coords, color: Color) -> Result<(), InvalidMove>

Source

pub fn get_empty_cells(&self) -> Vec<Coords>

Return all empty cells.

Source

pub fn get_neighbors( &self, coords: Coords, ) -> impl Iterator<Item = CoordsOrEdge> + '_

Return all neighbors of the given cell, including edges that border the cell.

Example:

 a  b  c
1\.  .  .\1
 2\.  .  .\2
  3\●  .  .\3
     a  b  c

Let’s compute the neighbors of the stone in the bottom left corner.

let board = Board::new(3);
let neighbors: Vec<CoordsOrEdge> = board.get_neighbors(Coords::new(2, 0)).collect();
assert_eq!(neighbors, vec![
    CoordsOrEdge::Edge(Edge::Left),
    CoordsOrEdge::Coords("a2".parse().unwrap()),
    CoordsOrEdge::Coords("b2".parse().unwrap()),
    CoordsOrEdge::Coords("b3".parse().unwrap()),
    CoordsOrEdge::Edge(Edge::Bottom),
]);
Source

pub fn find_attacked_bridges(&self, coords: Coords) -> Vec<Coords>

Return all bridges that are attacked by a given stone.

A bridge is the most common virtual connection pattern in Hex. In the following example, White cannot prevent Black from connecting their stones: If White places a stone between the black stones, Black may just choose the other cell between the black stones.

 a  b  c
1\.  .  .\1
 2\.  .  ●\2
  3\●  .  .\3
     a  b  c

This method assumes that one player has just played on coords. It computes all bridges of the other player that are being attacked by this move. For each attacked bridge, the free cell in the middle is returned. If the attacked player places a stone on this cell, the bridge will be fully connected despite the attack.

This method will also return bridges that connect a stone to one of the player’s own edges (see the example below).

The color at coords determines the attacking player (if the cell at coords is empty, this method returns an empty list.)

Example:

 a  b  c
1\.  .  .\1
 2\.  .  ●\2
  3\●  ○  .\3
     a  b  c

The attacked bridges on this board and coords=b3 are:

  • a3 - b2 - c2
  • c2 - c3 - bottom edge

Thus, this method would return the middle cells [b2, c3].

Trait Implementations§

Source§

impl Clone for Board

Source§

fn clone(&self) -> Board

Returns a duplicate 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 Display for Board

Source§

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

Pretty human-readable format for boards.

Example:

 a  b  c  d  e
1\.  .  .  .  .\1
 2\.  ●  .  ○  .\2
  3\.  .  ●  .  .\3
   4\.  .  .  ○  .\4
    5\.  .  .  .  .\5
       a  b  c  d  e

Auto Trait Implementations§

§

impl Freeze for Board

§

impl !RefUnwindSafe for Board

§

impl Send for Board

§

impl !Sync for Board

§

impl Unpin for Board

§

impl UnwindSafe for Board

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.