Struct magpie::othello::Game

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

Represents an Othello game.

To interact with the game it is useful to understand the bitboards that the board uses. The Board-struct documents these.

Implementations§

source§

impl Game

source

pub fn new() -> Self

Returns a game 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::Game;

let game = Game::new();
assert_eq!(60, game.empty_squares().count_set());
source

pub fn from_state( board: Board, next_player: Stone, passed_last_turn: bool ) -> Result<Self, OthelloError>

Returns a game with the specified parameters set.

If the supplied board is invalid an error will be returned instead.

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

let board = Board::empty();

let game = Game::from_state(board, Stone::Black, false)
               .expect("The supplied board is invalid");
assert_eq!(game.board(), Board::empty());
source

pub fn current_turn(&self) -> Stone

Returns the stone of the current player.

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

let game = Game::new();
assert!(game.current_turn() == Stone::Black);
source

pub fn pass_turn(&mut self)

The current player will pass their turn.

If two players pass in a row the game is considered concluded. Do note that it is up to the caller to determine this by calling .status().

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

let mut game = Game::new();
game.pass_turn();
assert!(game.current_turn() == Stone::White);
source

pub fn status(&self) -> Status

Reports the status of the game.

§Examples
use magpie::othello::{Game, Status, Stone};

let mut game = Game::new();
assert!(game.status() == Status::Progressing);
source

pub fn play(&mut self, pos: Position) -> Result<(), OthelloError>

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

If the move is illegal an error will be returned leaving the game untouched.

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

let mut game = Game::new();
let pos = game
    .moves()
    .hot_bits()
    .next()
    .unwrap();
assert!(game.play(pos).is_ok());
source

pub fn board(&self) -> Board

Returns a copy of the internal board used in this game.

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

let mut game = Game::new();
let board = game.board();
assert_eq!(board, Board::standard());
source

pub fn passed_last_turn(&self) -> bool

Returns whether or not the previous player passed their turn.

§Examples
use magpie::othello::Game;

let mut game = Game::new();
assert_eq!(game.passed_last_turn(), false);

Checks if the supplied position is a legal move for the current player.

§Examples
use magpie::othello::Game;

let mut game = Game::new();
let pos = game
    .moves()
    .hot_bits()
    .next()
    .unwrap();
assert_eq!(game.is_legal_move(pos), true);
source

pub fn moves(&self) -> Bitboard

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

§Examples
use magpie::othello::Game;

let game = Game::new();
assert_eq!(4, game.moves().count_set());
source

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

Returns a bitboard representing all stones for the specified player.

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

let mut game = Game::new();
let black = game.bits_for(Stone::Black);
let white = game.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());
source

pub fn empty_squares(&self) -> Bitboard

Returns a bitboard representing the set of all empty squares on the board.

§Examples
use magpie::othello::Game;

let game = Game::new();
assert_eq!(60, game.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::{Game, Stone};

let game = Game::new();
let pos = 0x8000000.try_into().unwrap();
assert_eq!(Some(Stone::White), game.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::{Game, Stone};

let game = Game::new();
println!("{}", game.display());

Trait Implementations§

source§

impl Clone for Game

source§

fn clone(&self) -> Game

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 Game

source§

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

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

impl Default for Game

source§

fn default() -> Self

Returns a game with the standard opening position configured.

Simply delegates to the new constructor.

§Examples
use magpie::othello::Game;

assert_eq!(Game::new(), Game::default());
source§

impl Hash for Game

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 Game

source§

fn eq(&self, other: &Game) -> 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 Eq for Game

source§

impl StructuralPartialEq for Game

Auto Trait Implementations§

§

impl Freeze for Game

§

impl RefUnwindSafe for Game

§

impl Send for Game

§

impl Sync for Game

§

impl Unpin for Game

§

impl UnwindSafe for Game

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.