Merge branch 'many-crates'

This commit is contained in:
Eryn Wells 2024-01-24 17:14:30 -08:00
commit d27c455ce4
25 changed files with 235 additions and 227 deletions

View file

@ -4,13 +4,12 @@ pub mod fen;
mod display; mod display;
mod r#move; mod r#move;
#[macro_use]
mod macros;
mod move_generator; mod move_generator;
pub mod piece;
mod position; mod position;
mod sight; mod sight;
pub use piece::{Color, Piece}; #[macro_use]
mod macros;
pub use position::{MoveBuilder as MakeMoveBuilder, Position, PositionBuilder}; pub use position::{MoveBuilder as MakeMoveBuilder, Position, PositionBuilder};
pub use r#move::{Castle, MakeMoveError, Move, MoveBuilder}; pub use r#move::{Castle, MakeMoveError, Move, MoveBuilder};

View file

@ -1,15 +1,5 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
#[macro_export]
macro_rules! piece {
($color:ident $shape:ident) => {
$crate::piece::Piece::new($crate::piece::Color::$color, $crate::piece::Shape::$shape)
};
($color:ident $shape:ident on $square:ident) => {
$crate::piece::PlacedPiece::new(piece!($color $shape), chessfriend_core::Square::$square)
}
}
#[macro_export] #[macro_export]
macro_rules! position { macro_rules! position {
[$($color:ident $shape:ident on $square:ident),* $(,)?] => { [$($color:ident $shape:ident on $square:ident),* $(,)?] => {

View file

@ -1,7 +1,6 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::piece::{Piece, PlacedPiece, Shape}; use chessfriend_core::{Piece, PlacedPiece, Rank, Shape, Square};
use chessfriend_core::{Rank, Square};
use std::fmt; use std::fmt;
pub use castle::Castle; pub use castle::Castle;
@ -17,8 +16,7 @@ pub enum MakeMoveError {
} }
mod castle { mod castle {
use crate::Color; use chessfriend_core::{Color, Square};
use chessfriend_core::Square;
#[repr(u16)] #[repr(u16)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
@ -305,7 +303,8 @@ impl MoveBuilder {
mod move_formatter { mod move_formatter {
use super::{Castle, Move}; use super::{Castle, Move};
use crate::{piece::Shape, Position}; use crate::Position;
use chessfriend_core::Shape;
use std::fmt; use std::fmt;
enum Style { enum Style {
@ -410,14 +409,15 @@ mod move_formatter {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{AlgebraicMoveFormatter, Style}; use super::{AlgebraicMoveFormatter, Style};
use crate::{piece, position}; use crate::position;
use chessfriend_core::piece;
macro_rules! chess_move { macro_rules! chess_move {
($color:ident $shape:ident $from_square:ident - $to_square:ident) => { ($color:ident $shape:ident $from_square:ident - $to_square:ident) => {
$crate::MoveBuilder::new( $crate::MoveBuilder::new(
$crate::piece::Piece::new( chessfriend_core::Piece::new(
$crate::piece::Color::$color, chessfriend_core::Color::$color,
$crate::piece::Shape::$shape, chessfriend_core::Shape::$shape,
), ),
chessfriend_core::Square::$from_square, chessfriend_core::Square::$from_square,
chessfriend_core::Square::$to_square, chessfriend_core::Square::$to_square,
@ -426,17 +426,17 @@ mod move_formatter {
}; };
($color:ident $shape:ident $from_square:ident x $to_square:ident, $captured_color:ident $captured_shape:ident) => { ($color:ident $shape:ident $from_square:ident x $to_square:ident, $captured_color:ident $captured_shape:ident) => {
$crate::MoveBuilder::new( $crate::MoveBuilder::new(
$crate::piece::Piece::new( chessfriend_core::Piece::new(
$crate::piece::Color::$color, chessfriend_core::Color::$color,
$crate::piece::Shape::$shape, chessfriend_core::Shape::$shape,
), ),
chessfriend_core::Square::$from_square, chessfriend_core::Square::$from_square,
chessfriend_core::Square::$to_square, chessfriend_core::Square::$to_square,
) )
.capturing($crate::piece::PlacedPiece::new( .capturing(chessfriend_core::PlacedPiece::new(
$crate::piece::Piece::new( chessfriend_core::Piece::new(
$crate::piece::Color::$captured_color, chessfriend_core::Color::$captured_color,
$crate::piece::Shape::$captured_shape, chessfriend_core::Shape::$captured_shape,
), ),
chessfriend_core::Square::$to_square, chessfriend_core::Square::$to_square,
)) ))
@ -488,7 +488,7 @@ mod move_formatter {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::piece; use chessfriend_core::piece;
macro_rules! assert_flag { macro_rules! assert_flag {
($move:expr, $left:expr, $right:expr) => { ($move:expr, $left:expr, $right:expr) => {

View file

@ -1,12 +1,9 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{ use crate::{MoveBuilder, Position};
piece::{Color, Piece, PlacedPiece},
MoveBuilder, Position,
};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::Direction; use chessfriend_core::{Color, Direction, Piece, PlacedPiece};
move_generator_declaration!(ClassicalMoveGenerator); move_generator_declaration!(ClassicalMoveGenerator);
@ -61,8 +58,9 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{piece, piece::Color, position, position::DiagramFormatter}; use crate::{position, position::DiagramFormatter};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::{piece, Color};
#[test] #[test]
fn classical_single_bishop_bitboard() { fn classical_single_bishop_bitboard() {

View file

@ -4,12 +4,9 @@
//! generating the possible moves for the king in the given position. //! generating the possible moves for the king in the given position.
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{ use crate::{ r#move::Castle, Move, MoveBuilder, Position};
piece::{Color, Piece, PlacedPiece},
r#move::Castle,
Move, MoveBuilder, Position,
};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece, PlacedPiece};
move_generator_declaration!(KingMoveGenerator, struct); move_generator_declaration!(KingMoveGenerator, struct);
move_generator_declaration!(KingMoveGenerator, new); move_generator_declaration!(KingMoveGenerator, new);
@ -77,8 +74,9 @@ impl<'pos> MoveGeneratorInternal for KingMoveGenerator<'pos> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::position;
use chessfriend_bitboard::bitboard; use chessfriend_bitboard::bitboard;
use chessfriend_core::Square; use chessfriend_core::{piece, Square};
use std::collections::HashSet; use std::collections::HashSet;
#[test] #[test]

View file

@ -1,11 +1,9 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{ use crate::{MoveBuilder, Position};
piece::{Color, Piece, PlacedPiece},
MoveBuilder, Position,
};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece, PlacedPiece};
move_generator_declaration!(KnightMoveGenerator); move_generator_declaration!(KnightMoveGenerator);
@ -41,8 +39,8 @@ impl<'pos> MoveGeneratorInternal for KnightMoveGenerator<'pos> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{piece, position, Move}; use crate::{position, Move};
use chessfriend_core::Square; use chessfriend_core::{piece, Square};
use std::collections::HashSet; use std::collections::HashSet;
#[test] #[test]

View file

@ -12,11 +12,8 @@ mod rook;
pub use move_generator::Moves; pub use move_generator::Moves;
pub(crate) use move_set::MoveSet; pub(crate) use move_set::MoveSet;
use crate::{ use crate::{ Move, Position};
piece::{Color, Piece, PlacedPiece}, use chessfriend_core::{Color, Piece, PlacedPiece, Square};
Move, Position,
};
use chessfriend_core::Square;
use std::collections::BTreeMap; use std::collections::BTreeMap;
trait MoveGenerator { trait MoveGenerator {
@ -32,9 +29,10 @@ macro_rules! move_generator_declaration {
move_generator_declaration!($name, getters); move_generator_declaration!($name, getters);
}; };
($name:ident, struct) => { ($name:ident, struct) => {
#[derive(Clone, Debug)]
pub(super) struct $name<'pos> { pub(super) struct $name<'pos> {
position: &'pos $crate::Position, position: &'pos $crate::Position,
color: $crate::piece::Color, color: chessfriend_core::Color,
move_sets: std::collections::BTreeMap< move_sets: std::collections::BTreeMap<
chessfriend_core::Square, chessfriend_core::Square,
$crate::move_generator::MoveSet, $crate::move_generator::MoveSet,
@ -43,7 +41,10 @@ macro_rules! move_generator_declaration {
}; };
($name:ident, new) => { ($name:ident, new) => {
impl<'pos> $name<'pos> { impl<'pos> $name<'pos> {
pub(super) fn new(position: &$crate::Position, color: $crate::piece::Color) -> $name { pub(super) fn new(
position: &$crate::Position,
color: chessfriend_core::Color,
) -> $name {
$name { $name {
position, position,
color, color,
@ -58,6 +59,13 @@ macro_rules! move_generator_declaration {
self.move_sets.values().map(|set| set.moves()).flatten() self.move_sets.values().map(|set| set.moves()).flatten()
} }
pub(crate) fn moves_for_piece(
&self,
piece: &chessfriend_core::PlacedPiece,
) -> Option<&$crate::move_generator::move_set::MoveSet> {
self.move_sets.get(&piece.square())
}
fn bitboard(&self) -> chessfriend_bitboard::BitBoard { fn bitboard(&self) -> chessfriend_bitboard::BitBoard {
self.move_sets.values().fold( self.move_sets.values().fold(
chessfriend_bitboard::BitBoard::empty(), chessfriend_bitboard::BitBoard::empty(),

View file

@ -2,12 +2,14 @@
use super::{ use super::{
bishop::ClassicalMoveGenerator as BishopMoveGenerator, king::KingMoveGenerator, bishop::ClassicalMoveGenerator as BishopMoveGenerator, king::KingMoveGenerator,
knight::KnightMoveGenerator, pawn::PawnMoveGenerator, knight::KnightMoveGenerator, move_set::MoveSet, pawn::PawnMoveGenerator,
queen::ClassicalMoveGenerator as QueenMoveGenerator, queen::ClassicalMoveGenerator as QueenMoveGenerator,
rook::ClassicalMoveGenerator as RookMoveGenerator, rook::ClassicalMoveGenerator as RookMoveGenerator,
}; };
use crate::{piece::Color, Move, Position}; use crate::{Move, Position};
use chessfriend_core::{Color, PlacedPiece, Shape};
#[derive(Clone, Debug)]
pub struct Moves<'pos> { pub struct Moves<'pos> {
pawn_moves: PawnMoveGenerator<'pos>, pawn_moves: PawnMoveGenerator<'pos>,
knight_moves: KnightMoveGenerator<'pos>, knight_moves: KnightMoveGenerator<'pos>,
@ -29,6 +31,17 @@ impl<'a> Moves<'a> {
} }
} }
pub(crate) fn moves_for_piece(&self, piece: &PlacedPiece) -> Option<&MoveSet> {
match piece.shape() {
Shape::Pawn => self.pawn_moves.moves_for_piece(piece),
Shape::Knight => self.knight_moves.moves_for_piece(piece),
Shape::Bishop => self.bishop_moves.moves_for_piece(piece),
Shape::Rook => self.rook_moves.moves_for_piece(piece),
Shape::Queen => self.queen_moves.moves_for_piece(piece),
Shape::King => self.king_moves.moves_for_piece(piece),
}
}
fn iter(&self) -> impl Iterator<Item = Move> + '_ { fn iter(&self) -> impl Iterator<Item = Move> + '_ {
self.pawn_moves self.pawn_moves
.iter() .iter()
@ -43,8 +56,8 @@ impl<'a> Moves<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{piece, position, r#move::AlgebraicMoveFormatter, Move, MoveBuilder}; use crate::{position, r#move::AlgebraicMoveFormatter, Move, MoveBuilder};
use chessfriend_core::Square; use chessfriend_core::{piece, Square};
use std::collections::HashSet; use std::collections::HashSet;
#[test] #[test]

View file

@ -1,5 +1,8 @@
use crate::{piece::PlacedPiece, Move}; // Eryn Wells <eryn@erynwells.me>
use crate::Move;
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::PlacedPiece;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
struct BitBoardSet { struct BitBoardSet {

View file

@ -1,10 +1,9 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::{ use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
piece::{Color, Piece, Shape}, use crate::{Move, MoveBuilder, Position};
Move, MoveBuilder, Position,
};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Color, Piece, PlacedPiece, Shape};
enum MoveList { enum MoveList {
Quiet = 0, Quiet = 0,

View file

@ -1,12 +1,9 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{ use crate::{MoveBuilder, Position};
piece::{Color, Piece, PlacedPiece},
MoveBuilder, Position,
};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::Direction; use chessfriend_core::{Color, Direction, Piece, PlacedPiece};
move_generator_declaration!(ClassicalMoveGenerator); move_generator_declaration!(ClassicalMoveGenerator);
@ -67,8 +64,9 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{piece, position, position::DiagramFormatter, Color}; use crate::{position, position::DiagramFormatter};
use chessfriend_bitboard::{bitboard, BitBoard}; use chessfriend_bitboard::{bitboard, BitBoard};
use chessfriend_core::{piece, Color};
#[test] #[test]
fn classical_single_queen_bitboard() { fn classical_single_queen_bitboard() {

View file

@ -1,12 +1,9 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet};
use crate::{ use crate::{ MoveBuilder, Position};
piece::{Color, Piece, PlacedPiece},
MoveBuilder, Position,
};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::Direction; use chessfriend_core::{Color, Direction, Piece, PlacedPiece};
move_generator_declaration!(ClassicalMoveGenerator); move_generator_declaration!(ClassicalMoveGenerator);
@ -63,9 +60,9 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{piece::Piece, position, position::DiagramFormatter, Color, Position}; use crate::{position, position::DiagramFormatter};
use chessfriend_bitboard::{bitboard, BitBoard}; use chessfriend_bitboard::{bitboard, BitBoard};
use chessfriend_core::Square; use chessfriend_core::Color;
#[test] #[test]
fn classical_single_rook_bitboard() { fn classical_single_rook_bitboard() {

View file

@ -1,13 +1,10 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::{ use crate::{
piece::{PlacedPiece, Shape}, position::flags::Flags, r#move::Castle, sight::SightExt, MakeMoveError, Move, Position,
position::flags::Flags,
r#move::Castle,
Color, MakeMoveError, Move, Piece, Position,
}; };
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::{Direction, Square}; use chessfriend_core::{Color, Direction, Piece, PlacedPiece, Shape, Square};
/// A position builder that builds a new position by making a move. /// A position builder that builds a new position by making a move.
#[derive(Clone)] #[derive(Clone)]
@ -249,7 +246,8 @@ impl<'p> From<&'p Position> for Builder<'p, NoMove> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{r#move::Castle, MoveBuilder, PositionBuilder}; use crate::{position, r#move::Castle, MoveBuilder, PositionBuilder};
use chessfriend_core::piece;
#[test] #[test]
fn move_white_pawn_one_square() -> Result<(), MakeMoveError> { fn move_white_pawn_one_square() -> Result<(), MakeMoveError> {

View file

@ -1,12 +1,11 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::{ use crate::{
piece::{PlacedPiece, Shape},
position::{flags::Flags, piece_sets::PieceBitBoards}, position::{flags::Flags, piece_sets::PieceBitBoards},
r#move::Castle, r#move::Castle,
Color, MakeMoveError, Move, Piece, Position, MakeMoveError, Move, Position,
}; };
use chessfriend_core::{Rank, Square}; use chessfriend_core::{piece, Color, Piece, PlacedPiece, Rank, Shape, Square};
use std::collections::BTreeMap; use std::collections::BTreeMap;
#[derive(Clone)] #[derive(Clone)]
@ -130,6 +129,7 @@ impl Default for Builder {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::PositionBuilder; use crate::PositionBuilder;
use chessfriend_core::piece;
#[test] #[test]
fn place_piece() { fn place_piece() {

View file

@ -40,7 +40,8 @@ impl<'a> fmt::Display for DiagramFormatter<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::Position; use crate::{position, Position};
use chessfriend_core::piece;
#[test] #[test]
#[ignore] #[ignore]

View file

@ -1,6 +1,7 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::{r#move::Castle, Color}; use crate::r#move::Castle;
use chessfriend_core::Color;
use std::fmt; use std::fmt;
#[derive(Clone, Copy, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Eq, Hash, PartialEq)]
@ -40,7 +41,8 @@ impl Default for Flags {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::{r#move::Castle, Color}; use crate::r#move::Castle;
use chessfriend_core::Color;
#[test] #[test]
fn castle_flags() { fn castle_flags() {

View file

@ -1,11 +1,7 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::{
piece::{Piece, PlacedPiece},
Color,
};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::Square; use chessfriend_core::{Color, Piece, PlacedPiece, Square};
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum PlacePieceStrategy { pub enum PlacePieceStrategy {

View file

@ -1,9 +1,8 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use super::Position; use super::Position;
use crate::piece::{Color, Piece, PlacedPiece, Shape};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::Square; use chessfriend_core::{Color, Piece, PlacedPiece, Shape, Square};
pub struct Pieces<'a> { pub struct Pieces<'a> {
color: Color, color: Color,
@ -74,9 +73,8 @@ impl<'a> Iterator for Pieces<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
use crate::piece::{Color, Piece, Shape};
use crate::{Position, PositionBuilder}; use crate::{Position, PositionBuilder};
use chessfriend_core::{piece, Color};
use std::collections::HashSet; use std::collections::HashSet;
#[test] #[test]

View file

@ -2,15 +2,14 @@
use super::{flags::Flags, piece_sets::PieceBitBoards, Pieces}; use super::{flags::Flags, piece_sets::PieceBitBoards, Pieces};
use crate::{ use crate::{
move_generator::Moves, move_generator::{MoveSet, Moves},
piece::{Color, Piece, PlacedPiece, Shape},
position::DiagramFormatter, position::DiagramFormatter,
r#move::Castle, r#move::Castle,
sight::Sight, sight::SightExt,
Move, Move,
}; };
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::Square; use chessfriend_core::{Color, Piece, PlacedPiece, Shape, Square};
use std::{cell::OnceCell, fmt}; use std::{cell::OnceCell, fmt};
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
@ -268,8 +267,8 @@ impl fmt::Display for Position {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{position, Castle, Color, Position}; use crate::{position, test_position, Castle, Position};
use chessfriend_core::Square; use chessfriend_core::{piece, Color, Square};
#[test] #[test]
fn piece_on_square() { fn piece_on_square() {

View file

@ -1,17 +1,22 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::{ use crate::Position;
piece::{Color, PlacedPiece, Shape},
Position,
};
use chessfriend_bitboard::BitBoard; use chessfriend_bitboard::BitBoard;
use chessfriend_core::Direction; use chessfriend_core::{Color, Direction, PlacedPiece, Shape};
pub(crate) trait Sight { pub(crate) trait SightExt {
fn sight_in_position(&self, position: &Position) -> BitBoard; fn sight_in_position(&self, position: &Position) -> BitBoard;
fn white_pawn_sight_in_position(&self, position: &Position) -> BitBoard;
fn black_pawn_sight_in_position(&self, position: &Position) -> BitBoard;
fn knight_sight_in_position(&self, position: &Position) -> BitBoard;
fn bishop_sight_in_position(&self, position: &Position) -> BitBoard;
fn rook_sight_in_position(&self, position: &Position) -> BitBoard;
fn queen_sight_in_position(&self, position: &Position) -> BitBoard;
fn king_sight_in_position(&self, position: &Position) -> BitBoard;
} }
impl Sight for PlacedPiece { impl SightExt for PlacedPiece {
fn sight_in_position(&self, position: &Position) -> BitBoard { fn sight_in_position(&self, position: &Position) -> BitBoard {
match self.shape() { match self.shape() {
Shape::Pawn => match self.color() { Shape::Pawn => match self.color() {
@ -25,9 +30,7 @@ impl Sight for PlacedPiece {
Shape::King => self.king_sight_in_position(position), Shape::King => self.king_sight_in_position(position),
} }
} }
}
impl PlacedPiece {
fn white_pawn_sight_in_position(&self, position: &Position) -> BitBoard { fn white_pawn_sight_in_position(&self, position: &Position) -> BitBoard {
let pawn: BitBoard = self.square().into(); let pawn: BitBoard = self.square().into();
let pawn = pawn.shift_north_west_one() | pawn.shift_north_east_one(); let pawn = pawn.shift_north_west_one() | pawn.shift_north_east_one();
@ -175,9 +178,9 @@ mod tests {
} }
mod pawn { mod pawn {
use crate::sight::Sight; use crate::{sight::SightExt, test_position};
use chessfriend_bitboard::{bitboard, BitBoard}; use chessfriend_bitboard::{bitboard, BitBoard};
use chessfriend_core::Square; use chessfriend_core::{piece, Square};
sight_test!(e4_pawn, piece!(White Pawn on E4), bitboard!(D5, F5)); sight_test!(e4_pawn, piece!(White Pawn on E4), bitboard!(D5, F5));
@ -235,8 +238,9 @@ mod tests {
#[macro_use] #[macro_use]
mod knight { mod knight {
use crate::sight::Sight; use crate::sight::SightExt;
use chessfriend_bitboard::bitboard; use chessfriend_bitboard::bitboard;
use chessfriend_core::piece;
sight_test!( sight_test!(
f6_knight, f6_knight,
@ -246,8 +250,9 @@ mod tests {
} }
mod bishop { mod bishop {
use crate::sight::Sight; use crate::sight::SightExt;
use chessfriend_bitboard::bitboard; use chessfriend_bitboard::bitboard;
use chessfriend_core::piece;
sight_test!( sight_test!(
c2_bishop, c2_bishop,
@ -257,8 +262,9 @@ mod tests {
} }
mod rook { mod rook {
use crate::sight::Sight; use crate::sight::SightExt;
use chessfriend_bitboard::bitboard; use chessfriend_bitboard::bitboard;
use chessfriend_core::piece;
sight_test!( sight_test!(
g3_rook, g3_rook,

62
core/src/colors.rs Normal file
View file

@ -0,0 +1,62 @@
// Eryn Wells <eryn@erynwells.me>
use crate::try_from_string;
use std::fmt;
#[derive(Debug, Eq, PartialEq)]
pub enum TryFromError {
InvalidCharacter,
ZeroLengthString,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Color {
White = 0,
Black = 1,
}
impl Color {
pub fn iter() -> impl Iterator<Item = Color> {
[Color::White, Color::Black].into_iter()
}
pub fn other(&self) -> Color {
match self {
Color::White => Color::Black,
Color::Black => Color::White,
}
}
}
impl Default for Color {
fn default() -> Self {
Color::White
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Color::White => "White",
Color::Black => "Black",
},
)
}
}
impl TryFrom<char> for Color {
type Error = TryFromError;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'w' | 'W' => Ok(Color::White),
'b' | 'B' => Ok(Color::Black),
_ => Err(TryFromError::InvalidCharacter),
}
}
}
try_from_string!(Color);

7
core/src/errors.rs Normal file
View file

@ -0,0 +1,7 @@
// Eryn Wells <eryn@erynwells.me>
#[derive(Debug, Eq, PartialEq)]
pub struct TryFromCharError;
#[derive(Debug, Eq, PartialEq)]
pub struct TryFromStrError;

View file

@ -1,3 +1,12 @@
mod coordinates; // Eryn Wells <eryn@erynwells.me>
pub mod colors;
pub mod coordinates;
pub mod errors;
pub mod pieces;
mod macros;
pub use colors::Color;
pub use coordinates::{Direction, File, Rank, Square}; pub use coordinates::{Direction, File, Rank, Square};
pub use pieces::{Piece, PlacedPiece, Shape};

32
core/src/macros.rs Normal file
View file

@ -0,0 +1,32 @@
// Eryn Wells <eryn@erynwells.me>
#[macro_export]
macro_rules! try_from_string {
($type:ty) => {
try_from_string!($type, &str);
try_from_string!($type, &String);
};
($type:ty, $from_type:ty) => {
impl TryFrom<$from_type> for $type {
type Error = $crate::errors::TryFromStrError;
fn try_from(value: $from_type) -> Result<Self, Self::Error> {
let first_char = value
.chars()
.nth(0)
.ok_or($crate::errors::TryFromStrError)?;
Self::try_from(first_char).map_err(|_| $crate::errors::TryFromStrError)
}
}
};
}
#[macro_export]
macro_rules! piece {
($color:ident $shape:ident) => {
$crate::Piece::new($crate::Color::$color, $crate::Shape::$shape)
};
($color:ident $shape:ident on $square:ident) => {
$crate::PlacedPiece::new(piece!($color $shape), $crate::Square::$square)
}
}

View file

@ -1,84 +1,7 @@
// Eryn Wells <eryn@erynwells.me> // Eryn Wells <eryn@erynwells.me>
use crate::display::{ASCIIDisplay, FENDisplay, UnicodeDisplay}; use crate::{errors::TryFromCharError, try_from_string, Color, Square};
use chessfriend_core::Square; use std::{fmt, slice::Iter};
use std::fmt;
use std::slice::Iter;
#[derive(Debug, Eq, PartialEq)]
pub enum TryFromError {
InvalidCharacter,
ZeroLengthString,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Color {
White = 0,
Black = 1,
}
impl Color {
pub fn iter() -> impl Iterator<Item = Color> {
[Color::White, Color::Black].into_iter()
}
pub fn other(&self) -> Color {
match self {
Color::White => Color::Black,
Color::Black => Color::White,
}
}
}
impl Default for Color {
fn default() -> Self {
Color::White
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Color::White => "White",
Color::Black => "Black",
},
)
}
}
impl TryFrom<char> for Color {
type Error = TryFromError;
fn try_from(value: char) -> Result<Self, Self::Error> {
match value {
'w' | 'W' => Ok(Color::White),
'b' | 'B' => Ok(Color::Black),
_ => Err(TryFromError::InvalidCharacter),
}
}
}
macro_rules! try_from_string {
($type:ty) => {
try_from_string!($type, &str);
try_from_string!($type, &String);
};
($type:ty, $from_type:ty) => {
impl TryFrom<$from_type> for $type {
type Error = TryFromError;
fn try_from(value: $from_type) -> Result<Self, Self::Error> {
let first_char = value.chars().nth(0).ok_or(TryFromError::ZeroLengthString)?;
Self::try_from(first_char)
}
}
};
}
try_from_string!(Color);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Shape { pub enum Shape {
@ -124,7 +47,7 @@ impl Shape {
} }
impl TryFrom<char> for Shape { impl TryFrom<char> for Shape {
type Error = TryFromError; type Error = TryFromCharError;
fn try_from(value: char) -> Result<Self, Self::Error> { fn try_from(value: char) -> Result<Self, Self::Error> {
match value { match value {
@ -134,7 +57,7 @@ impl TryFrom<char> for Shape {
'R' | 'r' => Ok(Shape::Rook), 'R' | 'r' => Ok(Shape::Rook),
'Q' | 'q' => Ok(Shape::Queen), 'Q' | 'q' => Ok(Shape::Queen),
'K' | 'k' => Ok(Shape::King), 'K' | 'k' => Ok(Shape::King),
_ => Err(TryFromError::InvalidCharacter), _ => Err(TryFromCharError),
} }
} }
} }
@ -219,18 +142,6 @@ impl Piece {
impl fmt::Display for Piece { impl fmt::Display for Piece {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
UnicodeDisplay::fmt(self, f)
}
}
impl ASCIIDisplay for Piece {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", Into::<char>::into(self.shape()))
}
}
impl UnicodeDisplay for Piece {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!( write!(
f, f,
"{}", "{}",
@ -252,20 +163,6 @@ impl UnicodeDisplay for Piece {
} }
} }
impl FENDisplay for Piece {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let ascii = self.shape().to_ascii();
write!(
f,
"{}",
match self.color {
Color::White => ascii.to_ascii_uppercase(),
Color::Black => ascii.to_ascii_lowercase(),
}
)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct PlacedPiece { pub struct PlacedPiece {
piece: Piece, piece: Piece,