[][src]Struct chess::Square

pub struct Square(_);

Represent a square on the chess board

Methods

impl Square
[src]

pub unsafe fn new(sq: u8) -> 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);
}

pub fn make_square(rank: Rank, file: File) -> Square
[src]

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);
}

pub fn get_rank(&self) -> Rank
[src]

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);

pub fn get_file(&self) -> File
[src]

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);

pub fn up(&self) -> Option<Square>
[src]

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);

pub fn down(&self) -> Option<Square>
[src]

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);

pub fn left(&self) -> Option<Square>
[src]

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);

pub fn right(&self) -> Option<Square>
[src]

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);

pub fn forward(&self, color: Color) -> Option<Square>
[src]

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);

pub fn backward(&self, color: Color) -> Option<Square>
[src]

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);

pub fn uup(&self) -> Square
[src]

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));

pub fn udown(&self) -> Square
[src]

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));

pub fn uleft(&self) -> Square
[src]

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));

pub fn uright(&self) -> Square
[src]

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));

pub fn uforward(&self, color: Color) -> Square
[src]

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));

pub fn ubackward(&self, color: Color) -> Square
[src]

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));

pub fn to_int(&self) -> u8
[src]

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);

pub fn to_index(&self) -> usize
[src]

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);

pub fn from_string(s: String) -> Option<Square>
[src]

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 Eq for Square
[src]

impl PartialOrd<Square> for Square
[src]

impl Copy for Square
[src]

impl Default for Square
[src]

fn default() -> 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 PartialEq<Square> for Square
[src]

impl Clone for Square
[src]

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

Performs copy-assignment from source. Read more

impl Ord for Square
[src]

fn max(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self
1.21.0
[src]

Compares and returns the minimum of two values. Read more

impl Debug for Square
[src]

impl Display for Square
[src]

impl Hash for Square
[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

Auto Trait Implementations

impl Send for Square

impl Sync for Square

Blanket Implementations

impl<T> From for T
[src]

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

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

type Owned = T

impl<T> ToString for T where
    T: Display + ?Sized
[src]

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

type Error = !

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

The type returned in the event of a conversion error.

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

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

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

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

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

The type returned in the event of a conversion error.

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