[board] Clean up interfaces of pieces and square structs
This commit is contained in:
parent
301fe1a4f4
commit
41421dddbb
5 changed files with 57 additions and 31 deletions
|
@ -21,28 +21,12 @@ pub struct SquareOutOfBoundsError;
|
|||
|
||||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
||||
pub struct Square {
|
||||
pub rank: u8,
|
||||
pub file: u8,
|
||||
pub index: u8,
|
||||
rank: u8,
|
||||
file: u8,
|
||||
index: u8,
|
||||
}
|
||||
|
||||
impl Square {
|
||||
pub fn from_index(index: u8) -> Result<Square, SquareOutOfBoundsError> {
|
||||
if index >= 64 {
|
||||
return Err(SquareOutOfBoundsError);
|
||||
}
|
||||
|
||||
Ok(Square::from_index_unsafe(index))
|
||||
}
|
||||
|
||||
pub(crate) fn from_index_unsafe(index: u8) -> Square {
|
||||
Square {
|
||||
rank: index / 8,
|
||||
file: index % 8,
|
||||
index: index,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_rank_file(rank: u8, file: u8) -> Result<Square, SquareOutOfBoundsError> {
|
||||
if rank >= 8 || file >= 8 {
|
||||
return Err(SquareOutOfBoundsError);
|
||||
|
@ -59,6 +43,10 @@ impl Square {
|
|||
s.parse()
|
||||
}
|
||||
|
||||
pub fn rank_file(&self) -> (u8, u8) {
|
||||
(self.rank, self.file)
|
||||
}
|
||||
|
||||
pub fn neighbor(&self, direction: Direction) -> Option<Square> {
|
||||
match direction {
|
||||
Direction::North => Square::from_index(self.index + 8),
|
||||
|
@ -110,6 +98,28 @@ impl Square {
|
|||
}
|
||||
}
|
||||
|
||||
impl Square {
|
||||
pub(crate) fn from_index(index: u8) -> Result<Square, SquareOutOfBoundsError> {
|
||||
if index >= 64 {
|
||||
return Err(SquareOutOfBoundsError);
|
||||
}
|
||||
|
||||
Ok(Square::from_index_unsafe(index))
|
||||
}
|
||||
|
||||
pub(crate) fn from_index_unsafe(index: u8) -> Square {
|
||||
Square {
|
||||
rank: index / 8,
|
||||
file: index % 8,
|
||||
index: index,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn index(&self) -> u8 {
|
||||
self.index
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Square {
|
||||
type Err = ParseSquareError;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue