Struct Game

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

For UI/UCI Servers, store a game object which allows you to determine draw by 3 fold repitition, draw offers, resignations, and moves.

This structure is slow compared to using Board directly, so it is not recommended for engines.

Implementations§

Source§

impl Game

Source

pub fn new() -> Game

Create a new Game with the initial position.

use candidate::{Game, Board};

let game = Game::new();
assert_eq!(game.current_position(), Board::default());
Source

pub fn get_boards(&self) -> &Vec<Board>

Source

pub fn new_with_board(board: Board) -> Game

Create a new Game with a specific starting position.

use candidate::{Game, Board};

let game = Game::new_with_board(Board::default());
assert_eq!(game.current_position(), Board::default());
Source

pub fn actions(&self) -> &Vec<Action>

Get all actions made in this game (moves, draw offers, resignations, etc.)

use candidate::{Game, MoveGen, Color};

let mut game = Game::new();
let mut movegen = MoveGen::new_legal(&game.current_position());

game.make_move(movegen.next().expect("At least one valid move"));
game.resign(Color::Black);
assert_eq!(game.actions().len(), 2);
Source

pub fn result(&self) -> Option<GameResult>

What is the status of this game?

use candidate::Game;

let game = Game::new();
assert!(game.result().is_none());
Source

pub fn new_from_fen(fen: &str) -> Option<Game>

👎Deprecated since 3.1.0: Please use Game::from_str(fen)? instead.

Create a new Game object from an FEN string.

use candidate::{Game, Board};

// This is the better way:
use std::str::FromStr;
let game: Game = Game::from_str("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1").expect("Valid FEN");
let game2: Result<Game, _> = Game::from_str("Invalid FEN");
assert!(game2.is_err());

// This still works
let game = Game::new_from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1").expect("Valid FEN");
let game2 = Game::new_from_fen("Invalid FEN");
assert!(game2.is_none());
Source

pub fn current_position(&self) -> Board

Get the current position on the board from the Game object.

use candidate::{Game, Board};

let game = Game::new();
assert_eq!(game.current_position(), Board::default());
Source

pub fn can_declare_draw(&self) -> bool

Determine if a player can legally declare a draw by 3-fold repetition or 50-move rule.

use candidate::{Game, Square, ChessMove};

let b1c3 = ChessMove::new(Square::B1, Square::C3, None);
let c3b1 = ChessMove::new(Square::C3, Square::B1, None);

let b8c6 = ChessMove::new(Square::B8, Square::C6, None);
let c6b8 = ChessMove::new(Square::C6, Square::B8, None);

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

game.make_move(b1c3);
game.make_move(b8c6);
game.make_move(c3b1);
game.make_move(c6b8);

assert_eq!(game.can_declare_draw(), false); // position has shown up twice

game.make_move(b1c3);
game.make_move(b8c6);
game.make_move(c3b1);
game.make_move(c6b8);
assert_eq!(game.can_declare_draw(), true); // position has shown up three times
Source

pub fn declare_draw(&mut self) -> bool

Declare a draw by 3-fold repitition or 50-move rule.

use candidate::{Game, Square, ChessMove};

let b1c3 = ChessMove::new(Square::B1, Square::C3, None);
let c3b1 = ChessMove::new(Square::C3, Square::B1, None);

let b8c6 = ChessMove::new(Square::B8, Square::C6, None);
let c6b8 = ChessMove::new(Square::C6, Square::B8, None);

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

game.make_move(b1c3);
game.make_move(b8c6);
game.make_move(c3b1);
game.make_move(c6b8);

assert_eq!(game.can_declare_draw(), false); // position has shown up twice

game.make_move(b1c3);
game.make_move(b8c6);
game.make_move(c3b1);
game.make_move(c6b8);
assert_eq!(game.can_declare_draw(), true); // position has shown up three times
game.declare_draw();
Source

pub fn make_move(&mut self, chess_move: ChessMove) -> Option<String>

Make a chess move on the board

use candidate::{Game, MoveGen};

let mut game = Game::new();

let mut movegen = MoveGen::new_legal(&game.current_position());

game.make_move(movegen.next().expect("At least one legal move"));
Source

pub fn side_to_move(&self) -> Color

Who’s turn is it to move?

use candidate::{Game, Color};

let game = Game::new();
assert_eq!(game.side_to_move(), Color::White);
Source

pub fn offer_draw(&mut self, color: Color) -> bool

Offer a draw to my opponent. color is the player who offered the draw. The draw must be accepted before my opponent moves.

use candidate::{Game, Color};

let mut game = Game::new();
game.offer_draw(Color::White);
Source

pub fn accept_draw(&mut self) -> bool

Accept a draw offer from my opponent.

use candidate::{Game, MoveGen, Color};

let mut game = Game::new();
game.offer_draw(Color::Black);
assert_eq!(game.accept_draw(), true);

let mut game2 = Game::new();
let mut movegen = MoveGen::new_legal(&game2.current_position());
game2.offer_draw(Color::Black);
game2.make_move(movegen.next().expect("At least one legal move"));
assert_eq!(game2.accept_draw(), false);
Source

pub fn resign(&mut self, color: Color) -> bool

color resigns the game

use candidate::{Game, Color};

let mut game = Game::new();
game.resign(Color::White);

Trait Implementations§

Source§

impl Clone for Game

Source§

fn clone(&self) -> Game

Returns a duplicate of the value. Read more
1.0.0 · Source§

const 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 FromStr for Game

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(fen: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more

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