Struct Position

Source
pub struct Position {
    pub col: u8,
    pub row: u8,
}
Expand description

Represents a position on the board. The position is represented by a column and a row.

§Examples

use chess_lab::constants::Position;

let pos = Position::new(0, 0);

assert_eq!(pos.to_string(), "a1");

let pos = Position::from_string("a1");

assert_eq!(pos.col, 0);
assert_eq!(pos.row, 0);

Fields§

§col: u8§row: u8

Implementations§

Source§

impl Position

Source

pub fn new(col: u8, row: u8) -> Position

Creates a new position

§Arguments
  • col: The column of the position (between 0 and 7)
  • row: The row of the position (between 0 and 7)
§Returns

A new position

§Panics

Panics if the column or row is out of bounds

§Examples
use chess_lab::constants::Position;

let pos = Position::new(0, 0);

assert_eq!(pos.col, 0);
assert_eq!(pos.row, 0);
Source

pub fn from_string(s: &str) -> Position

Creates a new position from a string

§Arguments
  • s: The string representation of the position
§Returns

A new position

§Panics

Panics if the string is not a valid position

§Examples
use chess_lab::constants::Position;

let pos = Position::from_string("a1");

assert_eq!(pos.col, 0);
assert_eq!(pos.row, 0);
Source

pub fn to_bitboard(&self) -> u64

Converts the position to a string

§Returns

The string representation of the position

§Examples
use chess_lab::constants::Position;

let pos = Position::new(0, 0);

assert_eq!(pos.to_bitboard(), 0x0000000000000001);
Source

pub fn from_bitboard(bitboard: u64) -> Vec<Position>

Converts a bitboard to a list of positions

§Arguments
  • bitboard: The bitboard to convert
§Returns

A list of positions

§Examples
use chess_lab::constants::Position;

let positions = Position::from_bitboard(0x0000000000000001);

assert_eq!(positions.len(), 1);
assert_eq!(positions[0].to_string(), "a1");
Source

pub fn direction(&self, other: &Position) -> (i8, i8)

Gets the direction between two positions

§Arguments
  • other: The other position
§Returns

The direction between the two positions

§Examples
use chess_lab::constants::Position;

let pos1 = Position::new(0, 0);
let pos2 = Position::new(1, 1);

let direction = pos1.direction(&pos2);

assert_eq!(direction, (1, 1));

Trait Implementations§

Source§

impl Add<(i8, i8)> for &Position

Source§

fn add(self, other: (i8, i8)) -> Position

Adds a certain offset to the position

§Arguments
  • other: The offset to add
§Returns

The new position

§Examples
use chess_lab::constants::Position;

let pos = Position::new(0, 0);
let new_pos = &pos + (1, 1);

assert_eq!(new_pos.col, 1);
assert_eq!(new_pos.row, 1);
Source§

type Output = Position

The resulting type after applying the + operator.
Source§

impl Clone for Position

Source§

fn clone(&self) -> Position

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 Position

Source§

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

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

impl PartialEq for Position

Source§

fn eq(&self, other: &Position) -> 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 Sub<&Position> for &Position

Source§

fn sub(self, other: &Position) -> (i8, i8)

Gets the offset between two positions

§Arguments
  • other: The other position
§Returns

The offset between the two positions

§Examples
use chess_lab::constants::Position;

let pos1 = Position::new(0, 0);
let pos2 = Position::new(1, 1);

let offset = &pos1 - &pos2;

assert_eq!(offset, (-1, -1));
Source§

type Output = (i8, i8)

The resulting type after applying the - operator.
Source§

impl Sub<(i8, i8)> for &Position

Source§

fn sub(self, other: (i8, i8)) -> Position

Subtracts a certain offset from the position

§Arguments
  • other: The offset to subtract
§Returns

The new position

§Examples
use chess_lab::constants::Position;

let pos = Position::new(1, 1);
let new_pos = &pos - (1, 1);

assert_eq!(new_pos.col, 0);
assert_eq!(new_pos.row, 0);
Source§

type Output = Position

The resulting type after applying the - operator.
Source§

impl ToString for Position

Source§

fn to_string(&self) -> String

Converts the position to a string

§Returns

The string representation of the position

§Examples
use chess_lab::constants::Position;

let pos = Position::new(0, 0);

assert_eq!(pos.to_string(), "a1");
Source§

impl Copy for Position

Source§

impl StructuralPartialEq for Position

Auto Trait Implementations§

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> 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, 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.