Enum chess::Square

source ·
#[repr(u8)]
pub enum Square {
Show 64 variants A1, B1, C1, D1, E1, F1, G1, H1, A2, B2, C2, D2, E2, F2, G2, H2, A3, B3, C3, D3, E3, F3, G3, H3, A4, B4, C4, D4, E4, F4, G4, H4, A5, B5, C5, D5, E5, F5, G5, H5, A6, B6, C6, D6, E6, F6, G6, H6, A7, B7, C7, D7, E7, F7, G7, H7, A8, B8, C8, D8, E8, F8, G8, H8,
}
Expand description

Represent a square on the chess board.

Variants

A1

B1

C1

D1

E1

F1

G1

H1

A2

B2

C2

D2

E2

F2

G2

H2

A3

B3

C3

D3

E3

F3

G3

H3

A4

B4

C4

D4

E4

F4

G4

H4

A5

B5

C5

D5

E5

F5

G5

H5

A6

B6

C6

D6

E6

F6

G6

H6

A7

B7

C7

D7

E7

F7

G7

H7

A8

B8

C8

D8

E8

F8

G8

H8

Implementations

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

Convert this Square into a usize.

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

Transform a screen coordinate into a Square.

Reciprocal: see Square::to_screen.

The result depend of:

Transform a Square into a screen coordinate.

Reciprocal: see Square::from_screen.

The result depend of:

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

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

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

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

Go one Rank up.

Examples
use chess::Square;

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

Go n Rank up.

Examples
use chess::Square;

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

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

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

Go one Rank down.

Examples
use chess::Square;

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

Go n Rank down.

Examples
use chess::{Color, Square};

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

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

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

Go one File to the right.

Examples
use chess::Square;

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

Go n File to the right.

Examples
use chess::{Color, Square};

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

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

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

Go one File to the left.

Examples
use chess::Square;

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

Go n File to the left.

Examples
use chess::{Color, Square};

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

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

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

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

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
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
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.