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::core::Position;
let pos = Position::new(0, 0).unwrap();
assert_eq!(pos.to_string(), "a1");
let pos = Position::from_string("a1").unwrap();
assert_eq!(pos.col, 0);
assert_eq!(pos.row, 0);Fields§
§col: u8The column of the position (between 0 and 7)
row: u8The row of the position (between 0 and 7)
Implementations§
Source§impl Position
impl Position
Sourcepub fn new(col: u8, row: u8) -> Result<Position, PositionOutOfRangeError>
pub fn new(col: u8, row: u8) -> Result<Position, PositionOutOfRangeError>
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
Ok(Position): The new positionErr(PositionOutOfRangeError): If the position is out of range
§Examples
use chess_lab::core::Position;
let pos = Position::new(0, 0).unwrap();
assert_eq!(pos.col, 0);
assert_eq!(pos.row, 0);Sourcepub fn from_string(s: &str) -> Result<Position, PositionInvalidError>
pub fn from_string(s: &str) -> Result<Position, PositionInvalidError>
Creates a new position from a string
§Arguments
s: The string representation of the position
§Returns
Ok(Position): The new positionErr(PositionInvalidError): If the string is invalid
§Examples
use chess_lab::core::Position;
let pos = Position::from_string("a1").unwrap();
assert_eq!(pos.col, 0);
assert_eq!(pos.row, 0);Sourcepub fn to_bitboard(&self) -> u64
pub fn to_bitboard(&self) -> u64
Sourcepub fn from_bitboard(bitboard: u64) -> Vec<Position>
pub fn from_bitboard(bitboard: u64) -> Vec<Position>
Sourcepub fn direction(&self, other: &Position) -> (i8, i8)
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::core::Position;
let pos1 = Position::new(0, 0).unwrap();
let pos2 = Position::new(1, 1).unwrap();
let direction = pos1.direction(&pos2);
assert_eq!(direction, (1, 1));Trait Implementations§
Source§impl Sub<&Position> for &Position
impl Sub<&Position> for &Position
Source§fn sub(self, other: &Position) -> (i8, i8)
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::core::Position;
let pos1 = Position::new(0, 0).unwrap();
let pos2 = Position::new(1, 1).unwrap();
let offset = &pos1 - &pos2;
assert_eq!(offset, (-1, -1));impl Copy for Position
impl Eq for Position
impl StructuralPartialEq for Position
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin for Position
impl UnsafeUnpin for Position
impl UnwindSafe for Position
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more