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
impl Game
sourcepub fn new() -> Self
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());
sourcepub fn from_state(
board: Board,
next_player: Stone,
passed_last_turn: bool
) -> Result<Self, OthelloError>
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());
sourcepub fn current_turn(&self) -> Stone
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);
sourcepub fn pass_turn(&mut self)
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);
sourcepub fn status(&self) -> Status
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);
sourcepub fn play(&mut self, pos: Position) -> Result<(), OthelloError>
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());
sourcepub fn board(&self) -> Board
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());
sourcepub fn passed_last_turn(&self) -> bool
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);
sourcepub fn is_legal_move(&self, pos: Position) -> bool
pub fn is_legal_move(&self, pos: Position) -> bool
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);
sourcepub fn moves(&self) -> Bitboard
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());
sourcepub fn bits_for(&self, stone: Stone) -> Bitboard
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());
sourcepub fn empty_squares(&self) -> Bitboard
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());
sourcepub fn stone_at(&self, pos: Position) -> Option<Stone>
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));