Square

Enum Square 

Source
#[repr(u8)]
pub enum Square {
Show 64 variants A1 = 0, B1 = 1, C1 = 2, D1 = 3, E1 = 4, F1 = 5, G1 = 6, H1 = 7, A2 = 8, B2 = 9, C2 = 10, D2 = 11, E2 = 12, F2 = 13, G2 = 14, H2 = 15, A3 = 16, B3 = 17, C3 = 18, D3 = 19, E3 = 20, F3 = 21, G3 = 22, H3 = 23, A4 = 24, B4 = 25, C4 = 26, D4 = 27, E4 = 28, F4 = 29, G4 = 30, H4 = 31, A5 = 32, B5 = 33, C5 = 34, D5 = 35, E5 = 36, F5 = 37, G5 = 38, H5 = 39, A6 = 40, B6 = 41, C6 = 42, D6 = 43, E6 = 44, F6 = 45, G6 = 46, H6 = 47, A7 = 48, B7 = 49, C7 = 50, D7 = 51, E7 = 52, F7 = 53, G7 = 54, H7 = 55, A8 = 56, B8 = 57, C8 = 58, D8 = 59, E8 = 60, F8 = 61, G8 = 62, H8 = 63,
}
Expand description

Represent a square on the chess board.

Variants§

§

A1 = 0

§

B1 = 1

§

C1 = 2

§

D1 = 3

§

E1 = 4

§

F1 = 5

§

G1 = 6

§

H1 = 7

§

A2 = 8

§

B2 = 9

§

C2 = 10

§

D2 = 11

§

E2 = 12

§

F2 = 13

§

G2 = 14

§

H2 = 15

§

A3 = 16

§

B3 = 17

§

C3 = 18

§

D3 = 19

§

E3 = 20

§

F3 = 21

§

G3 = 22

§

H3 = 23

§

A4 = 24

§

B4 = 25

§

C4 = 26

§

D4 = 27

§

E4 = 28

§

F4 = 29

§

G4 = 30

§

H4 = 31

§

A5 = 32

§

B5 = 33

§

C5 = 34

§

D5 = 35

§

E5 = 36

§

F5 = 37

§

G5 = 38

§

H5 = 39

§

A6 = 40

§

B6 = 41

§

C6 = 42

§

D6 = 43

§

E6 = 44

§

F6 = 45

§

G6 = 46

§

H6 = 47

§

A7 = 48

§

B7 = 49

§

C7 = 50

§

D7 = 51

§

E7 = 52

§

F7 = 53

§

G7 = 54

§

H7 = 55

§

A8 = 56

§

B8 = 57

§

C8 = 58

§

D8 = 59

§

E8 = 60

§

F8 = 61

§

G8 = 62

§

H8 = 63

Implementations§

Source§

impl Square

Source

pub fn new(index: usize) -> Self

Create a new Square, from an index.

§Panics

Panic if the index is not in the range 0..=63.

§Examples
use chess::Square;

assert_eq!(Square::new(0), Square::A1);
assert_eq!(Square::new(63), Square::H8);
Source

pub fn to_index(&self) -> usize

Convert this Square into a usize.

Source

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

Make a square from File and Rank.

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

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

pub fn from_screen(x: f32, y: f32) -> Square

Transform a screen coordinate into a Square.

Reciprocal: see Square::to_screen.

The result depend of:

Source

pub fn to_screen(&self) -> (f32, f32)

Transform a Square into a screen coordinate.

Reciprocal: see Square::from_screen.

The result depend of:

Source

pub fn file(&self) -> File

Return the File of this square.

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

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

assert_eq!(square.file(), File::D);
Source

pub fn file_for(&self, color: Color) -> File

Return the “relative” File of this square according the side.

§Examples
use chess::{Color, File, Square};

assert_eq!(Square::A1.file_for(Color::White), File::A);
assert_eq!(Square::A1.file_for(Color::Black), File::H);
Source

pub fn rank(&self) -> Rank

Return the Rank of this square.

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

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

assert_eq!(square.rank(), Rank::Seventh);
Source

pub fn rank_for(&self, color: Color) -> Rank

Return the “relative” Rank of this square according the side. (i.e. return ranks for black)

§Examples
use chess::{Color, Rank, Square};

assert_eq!(Square::E1.rank_for(Color::White), Rank::First);
assert_eq!(Square::E8.rank_for(Color::White), Rank::Eighth);
assert_eq!(Square::E2.rank_for(Color::Black), Rank::Seventh);
assert_eq!(Square::E7.rank_for(Color::Black), Rank::Second);
Source

pub fn up(&self) -> Self

Go one Rank up.

§Examples
use chess::Square;

assert_eq!(Square::B2.up(), Square::B3);
Source

pub fn n_up(&self, n: usize) -> Self

Go n Rank up.

§Examples
use chess::Square;

assert_eq!(Square::B2.n_up(3), Square::B5);
Source

pub fn forward(&self, color: Color) -> Self

Go one Rank forward according to the side.

§Examples
use chess::{Color, Square};

assert_eq!(Square::B2.forward(Color::White), Square::B3);
assert_eq!(Square::B2.forward(Color::Black), Square::B1);
Source

pub fn n_forward(&self, color: Color, n: usize) -> Self

Go n Rank forward according to the side.

§Examples
use chess::{Color, Square};

assert_eq!(Square::B2.n_forward(Color::White, 2), Square::B4);
assert_eq!(Square::B8.n_forward(Color::Black, 5), Square::B3);
Source

pub fn down(&self) -> Self

Go one Rank down.

§Examples
use chess::Square;

assert_eq!(Square::B2.down(), Square::B1);
Source

pub fn n_down(&self, n: usize) -> Self

Go n Rank down.

§Examples
use chess::{Color, Square};

assert_eq!(Square::B4.n_down(2), Square::B2);
Source

pub fn backward(&self, color: Color) -> Self

Go one Rank backward according to the side.

§Examples
use chess::{Color, Square};

assert_eq!(Square::B2.backward(Color::White), Square::B1);
assert_eq!(Square::B2.backward(Color::Black), Square::B3);
Source

pub fn n_backward(&self, color: Color, n: usize) -> Self

Go n Rank backward according to the side.

§Examples
use chess::{Color, Square};

assert_eq!(Square::B4.n_backward(Color::White, 2), Square::B2);
assert_eq!(Square::B3.n_backward(Color::Black, 5), Square::B8);
Source

pub fn right(&self) -> Self

Go one File to the right.

§Examples
use chess::Square;

assert_eq!(Square::B2.right(), Square::C2);
Source

pub fn n_right(&self, n: usize) -> Self

Go n File to the right.

§Examples
use chess::{Color, Square};

assert_eq!(Square::A4.n_right(3), Square::D4);
Source

pub fn right_for(&self, color: Color) -> Self

Go one File right according to the side.

§Examples
use chess::{Color, Square};

assert_eq!(Square::B2.right_for(Color::White), Square::C2);
assert_eq!(Square::B2.right_for(Color::Black), Square::A2);
Source

pub fn n_right_for(&self, color: Color, n: usize) -> Self

Go n File to the right according to the side.

§Examples
use chess::{Color, Square};

assert_eq!(Square::A4.n_right_for(Color::White, 3), Square::D4);
assert_eq!(Square::D4.n_right_for(Color::Black, 3), Square::A4);
Source

pub fn left(&self) -> Self

Go one File to the left.

§Examples
use chess::Square;

assert_eq!(Square::B2.left(), Square::A2);
Source

pub fn n_left(&self, n: usize) -> Self

Go n File to the left.

§Examples
use chess::{Color, Square};

assert_eq!(Square::D4.n_left(3), Square::A4);
Source

pub fn left_for(&self, color: Color) -> Self

Go one File left according to the side.

§Examples
use chess::{Color, Square};

assert_eq!(Square::B2.left_for(Color::White), Square::A2);
assert_eq!(Square::B2.left_for(Color::Black), Square::C2);
Source

pub fn n_left_for(&self, color: Color, n: usize) -> Self

Go n File to the left according to the side.

§Examples
use chess::{Color, Square};

assert_eq!(Square::D4.n_left_for(Color::White, 3), Square::A4);
assert_eq!(Square::A4.n_left_for(Color::Black, 3), Square::D4);
Source

pub fn follow_direction(&self, direction: Direction) -> Self

Go one Square in the given direction.

§Examples
use chess::{Direction, Square};

assert_eq!(Square::B2.follow_direction(Direction::Up), Square::B3);
assert_eq!(Square::B2.follow_direction(Direction::DownRight), Square::C1);
Source

pub fn distance(&self, other: Square) -> u32

The distance between the two squares, i.e. the number of king steps to get from one square to the other.

§Examples
use chess::Square;

assert_eq!(Square::A2.distance(Square::B5), 3);

Trait Implementations§

Source§

impl Clone for Square

Source§

fn clone(&self) -> Square

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Square

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Display for Square

Source§

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

Formats the value using the given formatter. Read more
Source§

impl FromStr for Square

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

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

impl Index<Square> for Board

Source§

type Output = Option<(Piece, Color)>

The returned type after indexing.
Source§

fn index(&self, index: Square) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<Square> for Board

Source§

fn index_mut(&mut self, index: Square) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Ord for Square

Source§

fn cmp(&self, other: &Square) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Square

Source§

fn eq(&self, other: &Square) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Square

Source§

fn partial_cmp(&self, other: &Square) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Square

Source§

impl Eq for Square

Source§

impl StructuralPartialEq for Square

Auto Trait Implementations§

§

impl Freeze for Square

§

impl RefUnwindSafe for Square

§

impl Send for Square

§

impl Sync for Square

§

impl Unpin for Square

§

impl UnwindSafe for Square

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V