Represent a square on the chess board
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) };
for sq in !EMPTY {
assert_ne!(bad_sq, sq);
}
Make a square given a rank and a file
use chess::{Square, Rank, File, BitBoard};
let sq = Square::make_square(Rank::First, File::A);
let bb = BitBoard::from_square(sq);
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);
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
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);
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Performs copy-assignment from source
. Read more
This method returns an Ordering
between self
and other
. Read more
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
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String
. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static