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
sourceimpl Board
impl Board
sourcepub fn new(size: CoordValue) -> Self
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
.
sourcepub fn from_stone_matrix(stones: StoneMatrix) -> Result<Self, InvalidBoard>
pub fn from_stone_matrix(stones: StoneMatrix) -> Result<Self, InvalidBoard>
Load a board from a StoneMatrix
.
sourcepub fn to_stone_matrix(&self) -> StoneMatrix
pub fn to_stone_matrix(&self) -> StoneMatrix
Convert this board to a StoneMatrix
.
sourcepub fn size(&self) -> CoordValue
pub fn size(&self) -> CoordValue
Return the size of this board. Boards are always square.
sourcepub fn get_color<T: Into<CoordsOrEdge>>(
&self,
coords_or_edge: T
) -> Option<Color>
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.
pub fn is_in_same_set<S: Into<CoordsOrEdge>, T: Into<CoordsOrEdge>>(
&self,
s: S,
t: T
) -> bool
pub fn play(&mut self, coords: Coords, color: Color) -> Result<(), InvalidMove>
sourcepub fn get_empty_cells(&self) -> Vec<Coords>
pub fn get_empty_cells(&self) -> Vec<Coords>
Return all empty cells.
sourcepub fn get_neighbors(
&self,
coords: Coords
) -> impl Iterator<Item = CoordsOrEdge> + '_
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),
]);
sourcepub fn find_attacked_bridges(&self, coords: Coords) -> Vec<Coords>
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
Auto Trait Implementations
impl !RefUnwindSafe for Board
impl Send for Board
impl !Sync for Board
impl Unpin for Board
impl UnwindSafe for Board
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more