Struct minorhacks_chess::Game[][src]

pub struct Game { /* fields omitted */ }

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

impl Game[src]

pub fn new() -> Game[src]

Create a new Game with the initial position.

use minorhacks_chess::{Game, Board};

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

pub fn new_with_board(board: Board) -> Game[src]

Create a new Game with a specific starting position.

use minorhacks_chess::{Game, Board};

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

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

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

use minorhacks_chess::{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);

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

What is the status of this game?

use minorhacks_chess::Game;

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

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

👎 Deprecated since 3.1.0:

Please use Game::from_str(fen)? instead.

Create a new Game object from an FEN string.

use minorhacks_chess::{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());

pub fn current_position(&self) -> Board[src]

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

use minorhacks_chess::{Game, Board};

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

pub fn can_declare_draw(&self) -> bool[src]

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

use minorhacks_chess::{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

pub fn declare_draw(&mut self) -> bool[src]

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

use minorhacks_chess::{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();

pub fn make_move(&mut self, chess_move: ChessMove) -> bool[src]

Make a chess move on the board

use minorhacks_chess::{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"));

pub fn side_to_move(&self) -> Color[src]

Who’s turn is it to move?

use minorhacks_chess::{Game, Color};

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

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

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

use minorhacks_chess::{Game, Color};

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

pub fn accept_draw(&mut self) -> bool[src]

Accept a draw offer from my opponent.

use minorhacks_chess::{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);

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

color resigns the game

use minorhacks_chess::{Game, Color};

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

Trait Implementations

impl Clone for Game[src]

fn clone(&self) -> Game[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for Game[src]

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

Formats the value using the given formatter. Read more

impl Default for Game[src]

fn default() -> Self[src]

Returns the “default value” for a type. Read more

impl FromStr for Game[src]

type Err = Error

The associated error which can be returned from parsing.

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

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

Auto Trait Implementations

impl RefUnwindSafe for Game

impl Send for Game

impl Sync for Game

impl Unpin for Game

impl UnwindSafe for Game

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.