Struct magpie::othello::Board

source ·
pub struct Board { /* private fields */ }
Expand description

Represents an Othello board and provides convenient functions to manipulate it.

The board is represented by two bitboards, one for black and one for white. Each bitboard is a 64-bit unsigned integer, where each bit encodes if a stone for that player exists on that space. As can be seen in the graphic below, MSB denotes A1 while LSB denotes H8.

    A    B    C    D    E    F    G    H
  +----+----+----+----+----+----+----+----+
1 | 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 |
  +----+----+----+----+----+----+----+----+
2 | 08 | 09 | 10 | 11 | 12 | 13 | 14 | 15 |
  +----+----+----+----+----+----+----+----+
3 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
  +----+----+----+----+----+----+----+----+
4 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
  +----+----+----+----+----+----+----+----+
5 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
  +----+----+----+----+----+----+----+----+
6 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
  +----+----+----+----+----+----+----+----+
7 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 |
  +----+----+----+----+----+----+----+----+
8 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
  +----+----+----+----+----+----+----+----+

Implementations§

source§

impl Board

source

pub fn empty() -> Self

Returns a completely empty board.

This can be useful for setting up specific scenarios but for most users, the standard constructor will be more useful.

§Examples
use magpie::othello::Board;

let board = Board::empty();
assert_eq!(64, board.empty_squares().count_set());
source

pub fn standard() -> Self

Returns a board with the standard opening position configured.

If W denotes white and B denotes black, this is the opening position:

    ABCDEFGH
   +--------+
 1 |........|
 2 |........|
 3 |........|
 4 |...WB...|
 5 |...BW...|
 6 |........|
 7 |........|
 8 |........|
   +--------+
§Examples
use magpie::othello::Board;

let board = Board::standard();
assert_eq!(60, board.empty_squares().count_set());
source

pub fn is_valid(&self) -> bool

Evaluates if the board is in a consistent state

Consistency is defined as whether or not multiple stones occupy the same square.

§Examples
use magpie::othello::{Board, Stone};

let mut board = Board::empty();
// The board should be valid
assert!(board.is_valid());

// Here multiple stones are placed on the same
// squares, which is not valid
board.place_stone_unchecked(Stone::Black, u64::MAX.into());
board.place_stone_unchecked(Stone::White, u64::MAX.into());
assert!(!board.is_valid());
source

pub fn place_stone_unchecked(&mut self, stone: Stone, pos: Bitboard)

Places stones in the specified positions.

Multiple stones may be placed at once, but no other stones will be flipped as during normal play. No restrictions are placed on the number or position of the pieces which may result in an invalid board where multiple stones occupy the same square. It is the responsibility of the caller to ensure that the board remains consistent.

remove_stone_unchecked may be of use as well.

§Examples
use magpie::othello::{Board, Stone};

let mut board = Board::empty();
let pos = 1_u64.try_into().unwrap();
board.place_stone_unchecked(Stone::Black, pos);
assert_ne!(board, Board::empty());
source

pub fn remove_stone_unchecked(&mut self, stone: Stone, pos: Bitboard)

Removes stones in the specified positions.

Multiple stones may be removed at once.

place_stone_unchecked may be of use as well.

§Examples
use magpie::othello::{Board, Stone};

let mut board = Board::standard();
let black_stones = board.bits_for(Stone::Black);
let white_stones = board.bits_for(Stone::White);
board.remove_stone_unchecked(Stone::Black, black_stones);
board.remove_stone_unchecked(Stone::White, white_stones);
assert_eq!(board, Board::empty());
source

pub fn play(&mut self, stone: Stone, pos: Position)

Places a stone in the specified position and updates the board accordingly.

It is the responsibility of the caller to ensure that the move is legal. Playing an illegal move will result in undefined behavior.

§Examples
use magpie::othello::{Board, Stone};

let mut board = Board::standard();
let player = Stone::Black;
let pos = board
    .moves_for(player)
    .hot_bits()
    .next()
    .unwrap();
board.play(Stone::Black, pos);
assert_ne!(board, Board::standard());
source

pub fn bits_for(&self, stone: Stone) -> Bitboard

Returns the bitboard representation of the specified player.

§Examples
use magpie::othello::{Board, Stone};

let board = Board::standard();
let black = board.bits_for(Stone::Black);
let white = board.bits_for(Stone::White);
// The two bitboards do not intersect
assert_eq!(0, black & white);
// They do contain the same number of stones
assert_eq!(black.count_set(), white.count_set());

Checks whether or not a move is valid for the specified player.

§Examples
use magpie::othello::{Board, Stone};

let board = Board::standard();
let pos = 1_u64.try_into().unwrap();
assert!(!board.is_legal_move(Stone::Black, pos));
source

pub fn moves_for(&self, stone: Stone) -> Bitboard

Calculates and returns the set of all legal moves for the specified player.

§Examples
use magpie::othello::{Board, Stone};

let board = Board::standard();
let stone = Stone::Black;
assert_eq!(4, board.moves_for(stone).count_set());
source

pub fn empty_squares(&self) -> Bitboard

Returns the set of all empty squares on the board.

§Examples
use magpie::othello::Board;

let board = Board::standard();
assert_eq!(60, board.empty_squares().count_set());
source

pub fn stone_at(&self, pos: Position) -> Option<Stone>

Queries the board at the specified position for the presence of a stone.

§Examples
use magpie::othello::{Board, Stone};

let board = Board::standard();
let pos = 0x8000000.try_into().unwrap();
assert_eq!(Some(Stone::White), board.stone_at(pos));
source

pub fn display(&self) -> BoardDisplay<'_>

Returns a struct that implements Display for customizing the display of Othello boards.

§Examples
use magpie::othello::{Board, Stone};

let board = Board::standard();
println!("{}", board.display());

Trait Implementations§

source§

impl Clone for Board

source§

fn clone(&self) -> Board

Returns a copy 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 Debug for Board

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for Board

source§

fn default() -> Self

Returns a board with the standard opening position configured.

Simply delegates to the standard constructor.

§Examples
use magpie::othello::Board;

assert_eq!(Board::standard(), Board::default());
source§

impl Hash for Board

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Board

source§

fn eq(&self, other: &Board) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl TryFrom<(Bitboard, Bitboard)> for Board

source§

fn try_from(stones: (Bitboard, Bitboard)) -> Result<Self, Self::Error>

Returns a board built from the two specified bitboards.

Returns an error if the two bitboards intersect.

§Examples
use magpie::othello::{Board, Stone};

let board = Board::standard();
let black = board.bits_for(Stone::Black);
let white = board.bits_for(Stone::White);

// Quite a contrived example
let board = Board::try_from((black, white));
assert_eq!(Ok(Board::standard()), board);
§

type Error = OthelloError

The type returned in the event of a conversion error.
source§

impl TryFrom<(u64, u64)> for Board

source§

fn try_from(stones: (u64, u64)) -> Result<Self, Self::Error>

Returns a board built from the two specified bitboards.

Returns an error if the two bitboards intersect.

§Examples
use magpie::othello::{Board, Stone};

let board = Board::standard();
let black = board.bits_for(Stone::Black);
let white = board.bits_for(Stone::White);

// Quite a contrived example
let board = Board::try_from((black, white));
assert_eq!(Ok(Board::standard()), board);
§

type Error = OthelloError

The type returned in the event of a conversion error.
source§

impl Eq for Board

source§

impl StructuralPartialEq for Board

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> 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,

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.