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