Struct chess::Square[][src]

pub struct Square(_);

Represent a square on the chess board

Methods

impl Square
[src]

Create a new square, given an index. Note: It is invalid, but allowed, to pass in a number >= 64. Doing so will crash stuff.


use chess::{Square, Rank, File, EMPTY};

assert_eq!(unsafe { Square::new(0) }, Square::default());

let bad_sq = unsafe { Square::new(64) };

// Iterate over all possible squares and ensure that *none* of them are equal to `bad_sq`.
for sq in !EMPTY {
    assert_ne!(bad_sq, sq);
}

Make a square given a rank and a file

use chess::{Square, Rank, File, BitBoard};

// Make the A1 square
let sq = Square::make_square(Rank::First, File::A);

// Convert it to a bitboard
let bb = BitBoard::from_square(sq);

// loop over all squares in the bitboard (should be only one), and ensure that the square
// is what we created
for x in bb {
    assert_eq!(sq, x);
}

Return the rank given this square.

use chess::{Square, Rank, File};

let sq = Square::make_square(Rank::Seventh, File::D);

assert_eq!(sq.get_rank(), Rank::Seventh);

Return the file given this square.

use chess::{Square, Rank, File};

let sq = Square::make_square(Rank::Seventh, File::D);

assert_eq!(sq.get_file(), File::D);

If there is a square above me, return that. Otherwise, None.

use chess::{Square, Rank, File};

let sq = Square::make_square(Rank::Seventh, File::D);

assert_eq!(sq.up().expect("Valid Square"), Square::make_square(Rank::Eighth, File::D));

assert_eq!(sq.up().expect("Valid Square").up(), None);

If there is a square below me, return that. Otherwise, None.

use chess::{Square, Rank, File};

let sq = Square::make_square(Rank::Second, File::D);

assert_eq!(sq.down().expect("Valid Square"), Square::make_square(Rank::First, File::D));

assert_eq!(sq.down().expect("Valid Square").down(), None);

If there is a square to the left of me, return that. Otherwise, None.

use chess::{Square, Rank, File};

let sq = Square::make_square(Rank::Seventh, File::B);

assert_eq!(sq.left().expect("Valid Square"), Square::make_square(Rank::Seventh, File::A));

assert_eq!(sq.left().expect("Valid Square").left(), None);

If there is a square to the right of me, return that. Otherwise, None.

use chess::{Square, Rank, File};

let sq = Square::make_square(Rank::Seventh, File::G);

assert_eq!(sq.right().expect("Valid Square"), Square::make_square(Rank::Seventh, File::H));

assert_eq!(sq.right().expect("Valid Square").right(), None);

If there is a square "forward", given my Color, go in that direction. Otherwise, None.

use chess::{Square, Rank, File, Color};

let mut sq = Square::make_square(Rank::Seventh, File::D);

assert_eq!(sq.forward(Color::White).expect("Valid Square"), Square::make_square(Rank::Eighth, File::D));
assert_eq!(sq.forward(Color::White).expect("Valid Square").forward(Color::White), None);

sq = Square::make_square(Rank::Second, File::D);

assert_eq!(sq.forward(Color::Black).expect("Valid Square"), Square::make_square(Rank::First, File::D));
assert_eq!(sq.forward(Color::Black).expect("Valid Square").forward(Color::Black), None);

If there is a square "backward" given my Color, go in that direction. Otherwise, None.

use chess::{Square, Rank, File, Color};

let mut sq = Square::make_square(Rank::Seventh, File::D);

assert_eq!(sq.backward(Color::Black).expect("Valid Square"), Square::make_square(Rank::Eighth, File::D));
assert_eq!(sq.backward(Color::Black).expect("Valid Square").backward(Color::Black), None);

sq = Square::make_square(Rank::Second, File::D);

assert_eq!(sq.backward(Color::White).expect("Valid Square"), Square::make_square(Rank::First, File::D));
assert_eq!(sq.backward(Color::White).expect("Valid Square").backward(Color::White), None);

If there is a square above me, return that. If not, wrap around to the other side.

use chess::{Square, Rank, File};

let sq = Square::make_square(Rank::Seventh, File::D);

assert_eq!(sq.uup(), Square::make_square(Rank::Eighth, File::D));

assert_eq!(sq.uup().uup(), Square::make_square(Rank::First, File::D));

If there is a square below me, return that. If not, wrap around to the other side.

use chess::{Square, Rank, File};

let sq = Square::make_square(Rank::Second, File::D);

assert_eq!(sq.udown(), Square::make_square(Rank::First, File::D));

assert_eq!(sq.udown().udown(), Square::make_square(Rank::Eighth, File::D));

If there is a square to the left of me, return that. If not, wrap around to the other side.

use chess::{Square, Rank, File};

let sq = Square::make_square(Rank::Seventh, File::B);

assert_eq!(sq.uleft(), Square::make_square(Rank::Seventh, File::A));

assert_eq!(sq.uleft().uleft(), Square::make_square(Rank::Seventh, File::H));

If there is a square to the right of me, return that. If not, wrap around to the other side.

use chess::{Square, Rank, File};

let sq = Square::make_square(Rank::Seventh, File::G);

assert_eq!(sq.uright(), Square::make_square(Rank::Seventh, File::H));

assert_eq!(sq.uright().uright(), Square::make_square(Rank::Seventh, File::A));

If there is a square "forward", given my color, return that. If not, wrap around to the other side.

use chess::{Square, Rank, File, Color};

let mut sq = Square::make_square(Rank::Seventh, File::D);

assert_eq!(sq.uforward(Color::White), Square::make_square(Rank::Eighth, File::D));
assert_eq!(sq.uforward(Color::White).uforward(Color::White), Square::make_square(Rank::First, File::D));

sq = Square::make_square(Rank::Second, File::D);

assert_eq!(sq.uforward(Color::Black), Square::make_square(Rank::First, File::D));
assert_eq!(sq.uforward(Color::Black).uforward(Color::Black), Square::make_square(Rank::Eighth, File::D));

If there is a square "backward", given my color, return that. If not, wrap around to the other side.

use chess::{Square, Rank, File, Color};

let mut sq = Square::make_square(Rank::Seventh, File::D);

assert_eq!(sq.ubackward(Color::Black), Square::make_square(Rank::Eighth, File::D));
assert_eq!(sq.ubackward(Color::Black).ubackward(Color::Black), Square::make_square(Rank::First, File::D));

sq = Square::make_square(Rank::Second, File::D);

assert_eq!(sq.ubackward(Color::White), Square::make_square(Rank::First, File::D));
assert_eq!(sq.ubackward(Color::White).ubackward(Color::White), Square::make_square(Rank::Eighth, File::D));

Convert this square to an integer.

use chess::{Square, Rank, File};

assert_eq!(Square::make_square(Rank::First, File::A).to_int(), 0);
assert_eq!(Square::make_square(Rank::Second, File::A).to_int(), 8);
assert_eq!(Square::make_square(Rank::First, File::B).to_int(), 1);
assert_eq!(Square::make_square(Rank::Eighth, File::H).to_int(), 63);

Convert this Square to a usize for table lookup purposes

use chess::{Square, Rank, File};

assert_eq!(Square::make_square(Rank::First, File::A).to_index(), 0);
assert_eq!(Square::make_square(Rank::Second, File::A).to_index(), 8);
assert_eq!(Square::make_square(Rank::First, File::B).to_index(), 1);
assert_eq!(Square::make_square(Rank::Eighth, File::H).to_index(), 63);

Convert a UCI String to a square. If invalid, return None

use chess::Square;

let sq = Square::default();

assert_eq!(Square::from_string("a1".to_owned()).expect("Valid Square"), sq);

Trait Implementations

impl PartialEq for Square
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Ord for Square
[src]

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

impl Eq for Square
[src]

impl PartialOrd for Square
[src]

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl Copy for Square
[src]

impl Clone for Square
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for Square
[src]

Formats the value using the given formatter. Read more

impl Default for Square
[src]

Create a square on A1.

use chess::{Square, Rank, File};

let explicit_sq = Square::make_square(Rank::First, File::A);
let implicit_sq = Square::default();

assert_eq!(explicit_sq, implicit_sq);

impl Display for Square
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Square

impl Sync for Square