From 7e08a9adc44eba7ffc85e893b23cff66c81dba2f Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Tue, 23 Jan 2024 17:18:48 -0800 Subject: [PATCH 01/12] [core] Create a core crate --- Cargo.toml | 1 + core/Cargo.toml | 8 ++++++++ core/src/lib.rs | 0 3 files changed, 9 insertions(+) create mode 100644 core/Cargo.toml create mode 100644 core/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 655f6ac..d595fa5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,5 +2,6 @@ members = [ "board", + "core", "explorer", ] diff --git a/core/Cargo.toml b/core/Cargo.toml new file mode 100644 index 0000000..900733d --- /dev/null +++ b/core/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "core" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/core/src/lib.rs b/core/src/lib.rs new file mode 100644 index 0000000..e69de29 From 406631b617646a43967668ae94125d94e62fcf2b Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 08:25:56 -0800 Subject: [PATCH 02/12] [core] Move the contents of board::square to core::coordinates Export Square, Rank, and File from the core crate. --- board/src/lib.rs | 1 - .../src/square.rs => core/src/coordinates.rs | 361 ++++++++++-------- core/src/lib.rs | 3 + 3 files changed, 212 insertions(+), 153 deletions(-) rename board/src/square.rs => core/src/coordinates.rs (55%) diff --git a/board/src/lib.rs b/board/src/lib.rs index 3db0753..8c68fad 100644 --- a/board/src/lib.rs +++ b/board/src/lib.rs @@ -12,7 +12,6 @@ mod move_generator; pub mod piece; mod position; mod sight; -mod square; pub use piece::{Color, Piece}; pub use position::{MoveBuilder as MakeMoveBuilder, Position, PositionBuilder}; diff --git a/board/src/square.rs b/core/src/coordinates.rs similarity index 55% rename from board/src/square.rs rename to core/src/coordinates.rs index 23565cf..fd0d25e 100644 --- a/board/src/square.rs +++ b/core/src/coordinates.rs @@ -1,8 +1,10 @@ // Eryn Wells -use crate::Color; -use std::{fmt, str::FromStr}; +use std::fmt; +use std::str::FromStr; +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[repr(u8)] pub enum Direction { North, NorthWest, @@ -14,14 +16,24 @@ pub enum Direction { NorthEast, } -#[derive(Debug)] -pub struct ParseFileError; +impl Direction { + pub fn to_offset(&self) -> i8 { + const OFFSETS: [i8; 8] = [8, 7, -1, -9, -8, -7, 1, 9]; + OFFSETS[*self as usize] + } +} -#[derive(Debug)] -pub struct ParseSquareError; +macro_rules! try_from_integer { + ($type:ident, $int_type:ident) => { + impl TryFrom<$int_type> for $type { + type Error = (); -#[derive(Debug)] -pub struct SquareOutOfBoundsError; + fn try_from(value: $int_type) -> Result { + Square::try_from(value as u8) + } + } + }; +} macro_rules! coordinate_enum { ($name: ident, $($variant:ident),*) => { @@ -34,43 +46,116 @@ macro_rules! coordinate_enum { impl $name { pub const NUM: usize = [$(Self::$variant), *].len(); pub const ALL: [Self; Self::NUM] = [$(Self::$variant), *]; + } - #[inline] - pub(crate) fn from_index(index: usize) -> Self { - assert!( - index < Self::NUM, - "Index {} out of bounds for {}.", - index, - stringify!($name) - ); - Self::try_index(index).unwrap() + impl TryFrom for $name { + type Error = (); + + fn try_from(value: u8) -> Result { + let value_usize = value as usize; + if value_usize < Self::NUM { + Ok($name::ALL[value_usize]) + } else { + Err(()) + } + } + } + + try_from_integer!($name, u16); + try_from_integer!($name, u32); + try_from_integer!($name, u64); + } +} + +macro_rules! range_bound_struct { + ($vis:vis, $type:ident, $repr:ty, $max:expr) => { + #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] + $vis struct $type($repr); + + #[allow(dead_code)] + impl $type { + $vis const FIRST: $type = $type(0); + $vis const LAST: $type = $type($max - 1); + } + + impl $type { + $vis fn new(x: $repr) -> Option { + if x < $max { + Some(Self(x)) + } else { + None + } } - pub fn try_index(index: usize) -> Option { - $( - #[allow(non_upper_case_globals)] - const $variant: usize = $name::$variant as usize; - )* + $vis unsafe fn new_unchecked(x: $repr) -> Self { + Self(x) + } + } - #[allow(non_upper_case_globals)] - match index { - $($variant => Some($name::$variant),)* - _ => None, - } + impl Into<$repr> for $type { + fn into(self) -> $repr { + self.0 + } + } + + impl TryFrom<$repr> for $type { + type Error = (); + + fn try_from(value: $repr) -> Result { + Self::new(value).ok_or(()) } } } } -#[rustfmt::skip] -coordinate_enum!(Rank, - One, Two, Three, Four, Five, Six, Seven, Eight -); +range_bound_struct!(pub, File, u8, 8); -#[rustfmt::skip] -coordinate_enum!(File, - A, B, C, D, E, F, G, H -); +impl File { + pub const A: File = File(0); + pub const B: File = File(1); + pub const C: File = File(2); + pub const D: File = File(3); + pub const E: File = File(4); + pub const F: File = File(5); + pub const G: File = File(6); + pub const H: File = File(7); + + pub const ALL: [File; 8] = [ + File::A, + File::B, + File::C, + File::D, + File::E, + File::F, + File::G, + File::H, + ]; +} + +range_bound_struct!(pub, Rank, u8, 8); + +#[allow(dead_code)] +impl Rank { + pub const ONE: Rank = Rank(0); + pub const TWO: Rank = Rank(1); + pub const THREE: Rank = Rank(2); + pub const FOUR: Rank = Rank(3); + pub const FIVE: Rank = Rank(4); + pub const SIX: Rank = Rank(5); + pub const SEVEN: Rank = Rank(6); + pub const EIGHT: Rank = Rank(7); + + pub const ALL: [Rank; 8] = [ + Rank::ONE, + Rank::TWO, + Rank::THREE, + Rank::FOUR, + Rank::FIVE, + Rank::SIX, + Rank::SEVEN, + Rank::EIGHT, + ]; +} #[rustfmt::skip] coordinate_enum!(Square, @@ -84,138 +169,87 @@ coordinate_enum!(Square, A8, B8, C8, D8, E8, F8, G8, H8 ); -impl Into for File { - fn into(self) -> char { - ('a' as u8 + self as u8) as char - } -} - -impl TryFrom for File { - type Error = ParseFileError; - - fn try_from(value: char) -> Result { - let lowercase_value = value.to_ascii_lowercase(); - for file in File::ALL.iter() { - if lowercase_value == (*file).into() { - return Ok(*file); - } - } - - Err(ParseFileError) - } -} - -impl fmt::Display for File { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", Into::::into(*self).to_uppercase()) - } -} - -impl Into for Rank { - fn into(self) -> char { - ('1' as u8 + self as u8) as char - } -} - -impl TryFrom for Rank { - type Error = ParseFileError; - - fn try_from(value: char) -> Result { - let lowercase_value = value.to_ascii_lowercase(); - for rank in Self::ALL.iter().cloned() { - if lowercase_value == rank.into() { - return Ok(rank); - } - } - - Err(ParseFileError) - } -} - -impl fmt::Display for Rank { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}", Into::::into(*self)) - } -} - impl Square { + pub unsafe fn from_index(x: u8) -> Square { + Self::try_from(x).unwrap_unchecked() + } + #[inline] pub fn from_file_rank(file: File, rank: Rank) -> Square { - Self::from_index((rank as usize) << 3 | file as usize) - } - - #[inline] - pub fn file(self) -> File { - File::from_index(self as usize & 0b000111) - } - - #[inline] - pub fn rank(self) -> Rank { - Rank::from_index(self as usize >> 3) - } -} - -impl Square { - const KING_STARTING_SQUARES: [Square; 2] = [Square::E1, Square::E8]; - - pub fn king_starting_square(color: Color) -> Square { - Square::KING_STARTING_SQUARES[color as usize] + let file_int: u8 = file.into(); + let rank_int: u8 = rank.into(); + unsafe { Self::from_index(rank_int << 3 | file_int) } } pub fn from_algebraic_str(s: &str) -> Result { s.parse() } + #[inline] + pub fn file(self) -> File { + unsafe { File::new_unchecked((self as u8) & 0b000111) } + } + + #[inline] + pub fn rank(self) -> Rank { + unsafe { Rank::new_unchecked((self as u8) >> 3) } + } + pub fn neighbor(self, direction: Direction) -> Option { + let index: u8 = self as u8; + let dir: i8 = direction.to_offset(); match direction { - Direction::North => Square::try_index(self as usize + 8), + Direction::North => Square::try_from(index.wrapping_add_signed(dir)).ok(), Direction::NorthWest => { - if self.rank() != Rank::Eight { - Square::try_index(self as usize + 7) + if self.rank() != Rank::EIGHT { + Square::try_from(index.wrapping_add_signed(dir)).ok() } else { None } } Direction::West => { if self.file() != File::A { - Square::try_index(self as usize - 1) + Square::try_from(index.wrapping_add_signed(dir)).ok() } else { None } } Direction::SouthWest => { - if self.rank() != Rank::One { - Square::try_index(self as usize - 9) + if self.rank() != Rank::ONE { + Square::try_from(index.wrapping_add_signed(dir)).ok() } else { None } } Direction::South => { - if self.rank() != Rank::One { - Square::try_index(self as usize - 8) + if self.rank() != Rank::ONE { + Square::try_from(index.wrapping_add_signed(dir)).ok() } else { None } } Direction::SouthEast => { - if self.rank() != Rank::One { - Square::try_index(self as usize - 7) + if self.rank() != Rank::ONE { + Square::try_from(index.wrapping_add_signed(dir)).ok() } else { None } } Direction::East => { if self.file() != File::H { - Square::try_index(self as usize + 1) + Square::try_from(index.wrapping_add_signed(dir)).ok() } else { None } } - Direction::NorthEast => Square::try_index(self as usize + 9), + Direction::NorthEast => Square::try_from(index.wrapping_add_signed(dir)).ok(), } } } +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct ParseSquareError; + impl FromStr for Square { type Err = ParseSquareError; @@ -240,31 +274,54 @@ impl FromStr for Square { } } -macro_rules! try_from_integer { - ($int_type:ident) => { - impl TryFrom<$int_type> for Square { - type Error = SquareOutOfBoundsError; - - fn try_from(value: $int_type) -> Result { - Square::try_index(value as usize).ok_or(SquareOutOfBoundsError) - } - } - }; +impl fmt::Display for File { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", Into::::into(*self)) + } } -try_from_integer!(u8); -try_from_integer!(u16); -try_from_integer!(u32); -try_from_integer!(u64); +impl fmt::Display for Rank { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", Into::::into(*self)) + } +} impl fmt::Display for Square { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{}{}", - ('a' as u8 + self.file() as u8) as char, - self.rank() as usize + 1 - ) + self.file().fmt(f)?; + self.rank().fmt(f)?; + Ok(()) + } +} + +impl Into for File { + fn into(self) -> char { + let value: u8 = self.into(); + (value + 'a' as u8) as char + } +} + +impl Into for Rank { + fn into(self) -> char { + let value: u8 = self.into(); + (value + '1' as u8) as char + } +} + +impl TryFrom for File { + type Error = (); + + fn try_from(value: char) -> Result { + File::try_from(value.to_ascii_lowercase() as u8 - 'a' as u8) + } +} + +impl TryFrom for Rank { + type Error = (); + + fn try_from(value: char) -> Result { + let result = (value as u8).checked_sub('1' as u8).ok_or(())?; + Self::try_from(result) } } @@ -275,16 +332,16 @@ mod tests { #[test] fn good_algebraic_input() { let sq = Square::from_algebraic_str("a4").expect("Failed to parse 'a4' square"); - assert_eq!(sq.file(), File::A); - assert_eq!(sq.rank(), Rank::Four); + assert_eq!(sq.file(), File(0)); + assert_eq!(sq.rank(), Rank(3)); let sq = Square::from_algebraic_str("B8").expect("Failed to parse 'B8' square"); - assert_eq!(sq.file(), File::B); - assert_eq!(sq.rank(), Rank::Eight); + assert_eq!(sq.file(), File(1)); + assert_eq!(sq.rank(), Rank(7)); let sq = Square::from_algebraic_str("e4").expect("Failed to parse 'B8' square"); - assert_eq!(sq.file(), File::E); - assert_eq!(sq.rank(), Rank::Four); + assert_eq!(sq.file(), File(4)); + assert_eq!(sq.rank(), Rank(3)); } #[test] @@ -299,13 +356,13 @@ mod tests { #[test] fn from_index() { - let sq = Square::try_index(4).expect("Unable to get Square from index"); - assert_eq!(sq.file(), File::E); - assert_eq!(sq.rank(), Rank::One); + let sq = Square::try_from(4u32).expect("Unable to get Square from index"); + assert_eq!(sq.file(), File(4)); + assert_eq!(sq.rank(), Rank(0)); - let sq = Square::try_index(28).expect("Unable to get Square from index"); - assert_eq!(sq.file(), File::E); - assert_eq!(sq.rank(), Rank::Four); + let sq = Square::try_from(28u32).expect("Unable to get Square from index"); + assert_eq!(sq.file(), File(4)); + assert_eq!(sq.rank(), Rank(3)); } #[test] diff --git a/core/src/lib.rs b/core/src/lib.rs index e69de29..2fe191d 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -0,0 +1,3 @@ +mod coordinates; + +pub use coordinates::{Direction, File, Rank, Square}; From 3c3a62345d2ec67983a01bdff6867fc702d9b37b Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 08:29:16 -0800 Subject: [PATCH 03/12] =?UTF-8?q?[core]=20Rename=20core=20=E2=86=92=20ches?= =?UTF-8?q?s=5Fcore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 2 +- {core => chess_core}/Cargo.toml | 2 +- {core => chess_core}/src/coordinates.rs | 0 {core => chess_core}/src/lib.rs | 0 4 files changed, 2 insertions(+), 2 deletions(-) rename {core => chess_core}/Cargo.toml (88%) rename {core => chess_core}/src/coordinates.rs (100%) rename {core => chess_core}/src/lib.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index d595fa5..0ecbba5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,6 @@ members = [ "board", - "core", + "chess_core", "explorer", ] diff --git a/core/Cargo.toml b/chess_core/Cargo.toml similarity index 88% rename from core/Cargo.toml rename to chess_core/Cargo.toml index 900733d..9b8c272 100644 --- a/core/Cargo.toml +++ b/chess_core/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "core" +name = "chess_core" version = "0.1.0" edition = "2021" diff --git a/core/src/coordinates.rs b/chess_core/src/coordinates.rs similarity index 100% rename from core/src/coordinates.rs rename to chess_core/src/coordinates.rs diff --git a/core/src/lib.rs b/chess_core/src/lib.rs similarity index 100% rename from core/src/lib.rs rename to chess_core/src/lib.rs From 106800bcb353f57d6ceaf9f816d7ce96026dbd55 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 08:32:09 -0800 Subject: [PATCH 04/12] [core,board] Update all use imports referring to Square, Rank, and File --- board/Cargo.toml | 1 + board/src/fen.rs | 5 ++-- board/src/lib.rs | 1 - board/src/macros.rs | 2 +- board/src/move.rs | 24 +++++++++---------- board/src/move_generator/bishop.rs | 2 +- board/src/move_generator/queen.rs | 2 +- board/src/move_generator/rook.rs | 7 +++--- board/src/piece.rs | 6 ++--- board/src/position/builders/move_builder.rs | 4 ++-- .../src/position/builders/position_builder.rs | 6 ++--- board/src/position/diagram_formatter.rs | 3 ++- board/src/position/piece_sets.rs | 3 ++- board/src/position/pieces.rs | 3 +-- board/src/position/position.rs | 6 +++-- board/src/sight.rs | 5 ++-- 16 files changed, 40 insertions(+), 40 deletions(-) diff --git a/board/Cargo.toml b/board/Cargo.toml index 6b600a9..da7df3c 100644 --- a/board/Cargo.toml +++ b/board/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chess_core = { path = "../chess_core" } diff --git a/board/src/fen.rs b/board/src/fen.rs index b212c94..be63610 100644 --- a/board/src/fen.rs +++ b/board/src/fen.rs @@ -3,8 +3,9 @@ use crate::{ piece::{Piece, PlacedPiece}, r#move::Castle, - Color, File, Position, Rank, Square, + Color, Position, }; +use chess_core::{File, Rank, Square}; use std::fmt::Write; #[derive(Clone, Copy, Debug, Eq, PartialEq)] @@ -42,7 +43,7 @@ impl ToFen for Position { write!(fen_string, "{}", empty_squares).map_err(|err| FenError::FmtError(err))?; empty_squares = 0; } - if rank != &Rank::One { + if rank != &Rank::ONE { write!(fen_string, "/").map_err(|err| FenError::FmtError(err))?; } } diff --git a/board/src/lib.rs b/board/src/lib.rs index 8c68fad..670368f 100644 --- a/board/src/lib.rs +++ b/board/src/lib.rs @@ -16,6 +16,5 @@ mod sight; pub use piece::{Color, Piece}; pub use position::{MoveBuilder as MakeMoveBuilder, Position, PositionBuilder}; pub use r#move::{Castle, MakeMoveError, Move, MoveBuilder}; -pub use square::{File, Rank, Square}; pub(crate) use bitboard::BitBoard; diff --git a/board/src/macros.rs b/board/src/macros.rs index 9dd3323..ad69496 100644 --- a/board/src/macros.rs +++ b/board/src/macros.rs @@ -6,7 +6,7 @@ macro_rules! piece { $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), $crate::square::Square::$square) + $crate::piece::PlacedPiece::new(piece!($color $shape), chess_core::Square::$square) } } diff --git a/board/src/move.rs b/board/src/move.rs index 08cf988..c593b56 100644 --- a/board/src/move.rs +++ b/board/src/move.rs @@ -1,10 +1,7 @@ // Eryn Wells -use crate::{ - piece::{Piece, PlacedPiece, Shape}, - square::Rank, - Square, -}; +use crate::piece::{Piece, PlacedPiece, Shape}; +use chess_core::{Rank, Square}; use std::fmt; pub use castle::Castle; @@ -20,7 +17,8 @@ pub enum MakeMoveError { } mod castle { - use crate::{Color, Square}; + use crate::Color; + use chess_core::Square; #[repr(u16)] #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -248,8 +246,8 @@ impl MoveBuilder { Shape::Pawn => { let from_rank = from.rank(); let to_rank = to.rank(); - let is_white_double_push = from_rank == Rank::Two && to_rank == Rank::Four; - let is_black_double_push = from_rank == Rank::Seven && to_rank == Rank::Five; + let is_white_double_push = from_rank == Rank::TWO && to_rank == Rank::FOUR; + let is_black_double_push = from_rank == Rank::SEVEN && to_rank == Rank::FIVE; if is_white_double_push || is_black_double_push { Kind::DoublePush } else { @@ -421,8 +419,8 @@ mod move_formatter { $crate::piece::Color::$color, $crate::piece::Shape::$shape, ), - $crate::Square::$from_square, - $crate::Square::$to_square, + chess_core::Square::$from_square, + chess_core::Square::$to_square, ) .build() }; @@ -432,15 +430,15 @@ mod move_formatter { $crate::piece::Color::$color, $crate::piece::Shape::$shape, ), - $crate::Square::$from_square, - $crate::Square::$to_square, + chess_core::Square::$from_square, + chess_core::Square::$to_square, ) .capturing($crate::piece::PlacedPiece::new( $crate::piece::Piece::new( $crate::piece::Color::$captured_color, $crate::piece::Shape::$captured_shape, ), - $crate::Square::$to_square, + chess_core::Square::$to_square, )) .build() }; diff --git a/board/src/move_generator/bishop.rs b/board/src/move_generator/bishop.rs index 7b07d54..7bad274 100644 --- a/board/src/move_generator/bishop.rs +++ b/board/src/move_generator/bishop.rs @@ -3,9 +3,9 @@ use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use crate::{ piece::{Color, Piece, PlacedPiece}, - square::Direction, BitBoard, MoveBuilder, Position, }; +use chess_core::Direction; move_generator_declaration!(ClassicalMoveGenerator); diff --git a/board/src/move_generator/queen.rs b/board/src/move_generator/queen.rs index 6694bf6..10ad498 100644 --- a/board/src/move_generator/queen.rs +++ b/board/src/move_generator/queen.rs @@ -3,9 +3,9 @@ use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use crate::{ piece::{Color, Piece, PlacedPiece}, - square::Direction, BitBoard, MoveBuilder, Position, }; +use chess_core::Direction; move_generator_declaration!(ClassicalMoveGenerator); diff --git a/board/src/move_generator/rook.rs b/board/src/move_generator/rook.rs index bd48711..8ee6acd 100644 --- a/board/src/move_generator/rook.rs +++ b/board/src/move_generator/rook.rs @@ -3,9 +3,9 @@ use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use crate::{ piece::{Color, Piece, PlacedPiece}, - square::Direction, BitBoard, MoveBuilder, Position, }; +use chess_core::Direction; move_generator_declaration!(ClassicalMoveGenerator); @@ -62,9 +62,8 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> { #[cfg(test)] mod tests { use super::*; - use crate::{ - piece::Piece, position, position::DiagramFormatter, BitBoard, Color, Position, Square, - }; + use crate::{piece::Piece, position, position::DiagramFormatter, BitBoard, Color, Position}; + use chess_core::Square; #[test] fn classical_single_rook_bitboard() { diff --git a/board/src/piece.rs b/board/src/piece.rs index f65a7b8..cc91765 100644 --- a/board/src/piece.rs +++ b/board/src/piece.rs @@ -1,9 +1,7 @@ // Eryn Wells -use crate::{ - display::{ASCIIDisplay, FENDisplay, UnicodeDisplay}, - Square, -}; +use crate::display::{ASCIIDisplay, FENDisplay, UnicodeDisplay}; +use chess_core::Square; use std::fmt; use std::slice::Iter; diff --git a/board/src/position/builders/move_builder.rs b/board/src/position/builders/move_builder.rs index 5d12b19..2146e9d 100644 --- a/board/src/position/builders/move_builder.rs +++ b/board/src/position/builders/move_builder.rs @@ -4,9 +4,9 @@ use crate::{ piece::{PlacedPiece, Shape}, position::flags::Flags, r#move::Castle, - square::Direction, - BitBoard, Color, MakeMoveError, Move, Piece, Position, Square, + BitBoard, Color, MakeMoveError, Move, Piece, Position, }; +use chess_core::{Direction, Square}; /// A position builder that builds a new position by making a move. #[derive(Clone)] diff --git a/board/src/position/builders/position_builder.rs b/board/src/position/builders/position_builder.rs index 7c68413..0410ca3 100644 --- a/board/src/position/builders/position_builder.rs +++ b/board/src/position/builders/position_builder.rs @@ -5,9 +5,9 @@ use crate::{ piece::{PlacedPiece, Shape}, position::{flags::Flags, piece_sets::PieceBitBoards}, r#move::Castle, - square::{Direction, Rank}, - BitBoard, Color, MakeMoveError, Move, Piece, Position, Square, + BitBoard, Color, MakeMoveError, Move, Piece, Position, }; +use chess_core::{Rank, Square}; use std::collections::BTreeMap; #[derive(Clone)] @@ -100,7 +100,7 @@ impl Builder { // Pawns cannot be placed on the first (back) rank of their side, // and cannot be placed on the final rank without a promotion. let rank = piece.square().rank(); - return rank != Rank::One && rank != Rank::Eight; + return rank != Rank::ONE && rank != Rank::EIGHT; } true diff --git a/board/src/position/diagram_formatter.rs b/board/src/position/diagram_formatter.rs index b007d20..ff4c3cf 100644 --- a/board/src/position/diagram_formatter.rs +++ b/board/src/position/diagram_formatter.rs @@ -1,6 +1,7 @@ // Eryn Wells -use crate::{File, Position, Rank, Square}; +use crate::Position; +use chess_core::{File, Rank, Square}; use std::fmt; pub struct DiagramFormatter<'a>(&'a Position); diff --git a/board/src/position/piece_sets.rs b/board/src/position/piece_sets.rs index 58921b9..fa08c84 100644 --- a/board/src/position/piece_sets.rs +++ b/board/src/position/piece_sets.rs @@ -2,8 +2,9 @@ use crate::{ piece::{Piece, PlacedPiece}, - BitBoard, Color, Square, + BitBoard, Color, }; +use chess_core::Square; #[derive(Debug, Eq, PartialEq)] pub enum PlacePieceStrategy { diff --git a/board/src/position/pieces.rs b/board/src/position/pieces.rs index d1cb6c7..4e4495d 100644 --- a/board/src/position/pieces.rs +++ b/board/src/position/pieces.rs @@ -3,7 +3,7 @@ use super::Position; use crate::piece::{Color, Piece, PlacedPiece, Shape}; use crate::BitBoard; -use crate::Square; +use chess_core::Square; pub struct Pieces<'a> { color: Color, @@ -76,7 +76,6 @@ impl<'a> Iterator for Pieces<'a> { mod tests { use super::*; use crate::piece::{Color, Piece, Shape}; - use crate::Square; use crate::{Position, PositionBuilder}; use std::collections::HashSet; diff --git a/board/src/position/position.rs b/board/src/position/position.rs index 484293e..ebc2948 100644 --- a/board/src/position/position.rs +++ b/board/src/position/position.rs @@ -7,8 +7,9 @@ use crate::{ position::DiagramFormatter, r#move::Castle, sight::Sight, - BitBoard, Move, Square, + BitBoard, Move, }; +use chess_core::Square; use std::{cell::OnceCell, fmt}; #[derive(Clone, Debug, Eq, PartialEq)] @@ -266,7 +267,8 @@ impl fmt::Display for Position { #[cfg(test)] mod tests { - use crate::{position, Castle, Color, Position, Square}; + use crate::{position, Castle, Color, Position}; + use chess_core::Square; #[test] fn piece_on_square() { diff --git a/board/src/sight.rs b/board/src/sight.rs index 33bd2d8..7763bac 100644 --- a/board/src/sight.rs +++ b/board/src/sight.rs @@ -2,9 +2,9 @@ use crate::{ piece::{Color, PlacedPiece, Shape}, - square::Direction, BitBoard, Position, }; +use chess_core::Direction; pub(crate) trait Sight { fn sight_in_position(&self, position: &Position) -> BitBoard; @@ -174,7 +174,8 @@ mod tests { } mod pawn { - use crate::{sight::Sight, BitBoard, Square}; + use crate::{sight::Sight, BitBoard}; + use chess_core::Square; sight_test!(e4_pawn, piece!(White Pawn on E4), bitboard!(D5, F5)); From eab30cc33baea919991450a5a3d16c65b6731091 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 08:33:07 -0800 Subject: [PATCH 05/12] =?UTF-8?q?[core]=20Rename=20the=20directory=20chess?= =?UTF-8?q?=5Fcore=20=E2=86=92=20core?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I think this is sufficient. --- {chess_core => core}/Cargo.toml | 0 {chess_core => core}/src/coordinates.rs | 0 {chess_core => core}/src/lib.rs | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename {chess_core => core}/Cargo.toml (100%) rename {chess_core => core}/src/coordinates.rs (100%) rename {chess_core => core}/src/lib.rs (100%) diff --git a/chess_core/Cargo.toml b/core/Cargo.toml similarity index 100% rename from chess_core/Cargo.toml rename to core/Cargo.toml diff --git a/chess_core/src/coordinates.rs b/core/src/coordinates.rs similarity index 100% rename from chess_core/src/coordinates.rs rename to core/src/coordinates.rs diff --git a/chess_core/src/lib.rs b/core/src/lib.rs similarity index 100% rename from chess_core/src/lib.rs rename to core/src/lib.rs From 32100b9553169c8553bfa360221f7fe4845fcb0b Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 08:34:23 -0800 Subject: [PATCH 06/12] [bitboard] Make an empty chess_bitboard crate This crate lives in bitboard/ --- Cargo.toml | 1 + bitboard/Cargo.toml | 9 +++++++++ bitboard/src/lib.rs | 1 + 3 files changed, 11 insertions(+) create mode 100644 bitboard/Cargo.toml create mode 100644 bitboard/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 0ecbba5..a5661d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "board", + "chess_bitboard", "chess_core", "explorer", ] diff --git a/bitboard/Cargo.toml b/bitboard/Cargo.toml new file mode 100644 index 0000000..0670e94 --- /dev/null +++ b/bitboard/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "chess_bitboard" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +core = { path = "../core" } diff --git a/bitboard/src/lib.rs b/bitboard/src/lib.rs new file mode 100644 index 0000000..8aa971d --- /dev/null +++ b/bitboard/src/lib.rs @@ -0,0 +1 @@ +// Eryn Wells From 625bfb2446de4e3a7502714523e82fe6b224fd85 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 08:35:22 -0800 Subject: [PATCH 07/12] [bitboard] Move everything in board::bitboard to the bitboard crate --- .../bitboard => bitboard/src}/bit_scanner.rs | 0 .../src/bitboard => bitboard/src}/bitboard.rs | 13 +++++++------ bitboard/src/lib.rs | 17 +++++++++++++++++ {board/src/bitboard => bitboard/src}/library.rs | 2 +- {board/src/bitboard => bitboard/src}/shifts.rs | 0 board/src/bitboard/mod.rs | 16 ---------------- board/src/lib.rs | 2 -- 7 files changed, 25 insertions(+), 25 deletions(-) rename {board/src/bitboard => bitboard/src}/bit_scanner.rs (100%) rename {board/src/bitboard => bitboard/src}/bitboard.rs (96%) rename {board/src/bitboard => bitboard/src}/library.rs (99%) rename {board/src/bitboard => bitboard/src}/shifts.rs (100%) delete mode 100644 board/src/bitboard/mod.rs diff --git a/board/src/bitboard/bit_scanner.rs b/bitboard/src/bit_scanner.rs similarity index 100% rename from board/src/bitboard/bit_scanner.rs rename to bitboard/src/bit_scanner.rs diff --git a/board/src/bitboard/bitboard.rs b/bitboard/src/bitboard.rs similarity index 96% rename from board/src/bitboard/bitboard.rs rename to bitboard/src/bitboard.rs index 5c35ca8..730c7c2 100644 --- a/board/src/bitboard/bitboard.rs +++ b/bitboard/src/bitboard.rs @@ -1,8 +1,8 @@ // Eryn Wells -use super::library::{library, FILES, RANKS}; -use super::LeadingBitScanner; -use crate::{square::Direction, Square}; +use crate::library::{library, FILES, RANKS}; +use crate::LeadingBitScanner; +use chess_core::{Direction, Square}; use std::fmt; use std::ops::Not; @@ -72,13 +72,13 @@ impl BitBoard { /// Return an Iterator over the occupied squares, starting from the leading /// (most-significant bit) end of the field. pub(crate) fn occupied_squares(&self) -> impl Iterator { - LeadingBitScanner::new(self.0).map(Square::from_index) + LeadingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) }) } /// Return an Iterator over the occupied squares, starting from the trailing /// (least-significant bit) end of the field. pub(crate) fn occupied_squares_trailing(&self) -> impl Iterator { - LeadingBitScanner::new(self.0).map(Square::from_index) + LeadingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) }) } } @@ -243,7 +243,8 @@ impl BitBoardBuilder { #[cfg(test)] mod tests { use super::*; - use crate::{bitboard, Square}; + use crate::bitboard; + use chess_core::Square; #[test] fn display_and_debug() { diff --git a/bitboard/src/lib.rs b/bitboard/src/lib.rs index 8aa971d..d3c0c4b 100644 --- a/bitboard/src/lib.rs +++ b/bitboard/src/lib.rs @@ -1 +1,18 @@ // Eryn Wells + +mod bit_scanner; +mod bitboard; +mod library; +mod shifts; + +pub(crate) use bit_scanner::{LeadingBitScanner, TrailingBitScanner}; +pub(crate) use bitboard::{BitBoard, BitBoardBuilder}; + +#[macro_export] +macro_rules! bitboard { + ($($sq:ident),* $(,)?) => { + $crate::bitboard::BitBoardBuilder::empty() + $(.square(chess_core::Square::$sq))* + .build() + }; +} diff --git a/board/src/bitboard/library.rs b/bitboard/src/library.rs similarity index 99% rename from board/src/bitboard/library.rs rename to bitboard/src/library.rs index fe2daa0..1e6a84d 100644 --- a/board/src/bitboard/library.rs +++ b/bitboard/src/library.rs @@ -1,7 +1,7 @@ // Eryn Wells use super::BitBoard; -use crate::{square::Direction, Square}; +use chess_core::{Direction, Square}; use std::sync::Once; pub(super) const RANKS: [BitBoard; 8] = [ diff --git a/board/src/bitboard/shifts.rs b/bitboard/src/shifts.rs similarity index 100% rename from board/src/bitboard/shifts.rs rename to bitboard/src/shifts.rs diff --git a/board/src/bitboard/mod.rs b/board/src/bitboard/mod.rs deleted file mode 100644 index c417abd..0000000 --- a/board/src/bitboard/mod.rs +++ /dev/null @@ -1,16 +0,0 @@ -mod bit_scanner; -mod bitboard; -mod library; -mod shifts; - -pub(crate) use bit_scanner::{LeadingBitScanner, TrailingBitScanner}; -pub(crate) use bitboard::{BitBoard, BitBoardBuilder}; - -#[macro_export] -macro_rules! bitboard { - ($($sq:ident),* $(,)?) => { - $crate::bitboard::BitBoardBuilder::empty() - $(.square($crate::Square::$sq))* - .build() - }; -} diff --git a/board/src/lib.rs b/board/src/lib.rs index 670368f..2ed3495 100644 --- a/board/src/lib.rs +++ b/board/src/lib.rs @@ -2,8 +2,6 @@ pub mod fen; -#[macro_use] -mod bitboard; mod display; mod r#move; #[macro_use] From b0b22048a8efdd120faaf7d350318162cccc19d7 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 08:48:19 -0800 Subject: [PATCH 08/12] =?UTF-8?q?[core]=20Rename=20(once=20again)=20chess?= =?UTF-8?q?=5Fcore=20=E2=86=92=20chessfriend=5Fcore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 2 +- bitboard/Cargo.toml | 2 +- bitboard/src/bitboard.rs | 2 +- bitboard/src/lib.rs | 2 +- bitboard/src/library.rs | 2 +- board/Cargo.toml | 2 +- board/src/fen.rs | 2 +- board/src/macros.rs | 2 +- board/src/move.rs | 14 +++++++------- board/src/move_generator/bishop.rs | 2 +- board/src/move_generator/queen.rs | 2 +- board/src/move_generator/rook.rs | 4 ++-- board/src/piece.rs | 2 +- board/src/position/builders/move_builder.rs | 2 +- board/src/position/builders/position_builder.rs | 2 +- board/src/position/diagram_formatter.rs | 2 +- board/src/position/piece_sets.rs | 2 +- board/src/position/pieces.rs | 2 +- board/src/position/position.rs | 4 ++-- board/src/sight.rs | 4 ++-- core/Cargo.toml | 2 +- 21 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a5661d8..7542226 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,6 @@ members = [ "board", "chess_bitboard", - "chess_core", + "chessfriend_core", "explorer", ] diff --git a/bitboard/Cargo.toml b/bitboard/Cargo.toml index 0670e94..e565de3 100644 --- a/bitboard/Cargo.toml +++ b/bitboard/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -core = { path = "../core" } +chessfriend_core = { path = "../core" } diff --git a/bitboard/src/bitboard.rs b/bitboard/src/bitboard.rs index 730c7c2..5be905a 100644 --- a/bitboard/src/bitboard.rs +++ b/bitboard/src/bitboard.rs @@ -2,7 +2,7 @@ use crate::library::{library, FILES, RANKS}; use crate::LeadingBitScanner; -use chess_core::{Direction, Square}; +use chessfriend_core::{Direction, Square}; use std::fmt; use std::ops::Not; diff --git a/bitboard/src/lib.rs b/bitboard/src/lib.rs index d3c0c4b..6b0703e 100644 --- a/bitboard/src/lib.rs +++ b/bitboard/src/lib.rs @@ -12,7 +12,7 @@ pub(crate) use bitboard::{BitBoard, BitBoardBuilder}; macro_rules! bitboard { ($($sq:ident),* $(,)?) => { $crate::bitboard::BitBoardBuilder::empty() - $(.square(chess_core::Square::$sq))* + $(.square(chessfriend_core::Square::$sq))* .build() }; } diff --git a/bitboard/src/library.rs b/bitboard/src/library.rs index 1e6a84d..506d6cd 100644 --- a/bitboard/src/library.rs +++ b/bitboard/src/library.rs @@ -1,7 +1,7 @@ // Eryn Wells use super::BitBoard; -use chess_core::{Direction, Square}; +use chessfriend_core::{Direction, Square}; use std::sync::Once; pub(super) const RANKS: [BitBoard; 8] = [ diff --git a/board/Cargo.toml b/board/Cargo.toml index da7df3c..2f8727d 100644 --- a/board/Cargo.toml +++ b/board/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -chess_core = { path = "../chess_core" } +chessfriend_core = { path = "../core" } diff --git a/board/src/fen.rs b/board/src/fen.rs index be63610..8610f00 100644 --- a/board/src/fen.rs +++ b/board/src/fen.rs @@ -5,7 +5,7 @@ use crate::{ r#move::Castle, Color, Position, }; -use chess_core::{File, Rank, Square}; +use chessfriend_core::{File, Rank, Square}; use std::fmt::Write; #[derive(Clone, Copy, Debug, Eq, PartialEq)] diff --git a/board/src/macros.rs b/board/src/macros.rs index ad69496..c6c374c 100644 --- a/board/src/macros.rs +++ b/board/src/macros.rs @@ -6,7 +6,7 @@ macro_rules! piece { $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), chess_core::Square::$square) + $crate::piece::PlacedPiece::new(piece!($color $shape), chessfriend_core::Square::$square) } } diff --git a/board/src/move.rs b/board/src/move.rs index c593b56..7b3e55c 100644 --- a/board/src/move.rs +++ b/board/src/move.rs @@ -1,7 +1,7 @@ // Eryn Wells use crate::piece::{Piece, PlacedPiece, Shape}; -use chess_core::{Rank, Square}; +use chessfriend_core::{Rank, Square}; use std::fmt; pub use castle::Castle; @@ -18,7 +18,7 @@ pub enum MakeMoveError { mod castle { use crate::Color; - use chess_core::Square; + use chessfriend_core::Square; #[repr(u16)] #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -419,8 +419,8 @@ mod move_formatter { $crate::piece::Color::$color, $crate::piece::Shape::$shape, ), - chess_core::Square::$from_square, - chess_core::Square::$to_square, + chessfriend_core::Square::$from_square, + chessfriend_core::Square::$to_square, ) .build() }; @@ -430,15 +430,15 @@ mod move_formatter { $crate::piece::Color::$color, $crate::piece::Shape::$shape, ), - chess_core::Square::$from_square, - chess_core::Square::$to_square, + chessfriend_core::Square::$from_square, + chessfriend_core::Square::$to_square, ) .capturing($crate::piece::PlacedPiece::new( $crate::piece::Piece::new( $crate::piece::Color::$captured_color, $crate::piece::Shape::$captured_shape, ), - chess_core::Square::$to_square, + chessfriend_core::Square::$to_square, )) .build() }; diff --git a/board/src/move_generator/bishop.rs b/board/src/move_generator/bishop.rs index 7bad274..cd259f7 100644 --- a/board/src/move_generator/bishop.rs +++ b/board/src/move_generator/bishop.rs @@ -5,7 +5,7 @@ use crate::{ piece::{Color, Piece, PlacedPiece}, BitBoard, MoveBuilder, Position, }; -use chess_core::Direction; +use chessfriend_core::Direction; move_generator_declaration!(ClassicalMoveGenerator); diff --git a/board/src/move_generator/queen.rs b/board/src/move_generator/queen.rs index 10ad498..f012d8c 100644 --- a/board/src/move_generator/queen.rs +++ b/board/src/move_generator/queen.rs @@ -5,7 +5,7 @@ use crate::{ piece::{Color, Piece, PlacedPiece}, BitBoard, MoveBuilder, Position, }; -use chess_core::Direction; +use chessfriend_core::Direction; move_generator_declaration!(ClassicalMoveGenerator); diff --git a/board/src/move_generator/rook.rs b/board/src/move_generator/rook.rs index 8ee6acd..c6dbb7b 100644 --- a/board/src/move_generator/rook.rs +++ b/board/src/move_generator/rook.rs @@ -5,7 +5,7 @@ use crate::{ piece::{Color, Piece, PlacedPiece}, BitBoard, MoveBuilder, Position, }; -use chess_core::Direction; +use chessfriend_core::Direction; move_generator_declaration!(ClassicalMoveGenerator); @@ -63,7 +63,7 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> { mod tests { use super::*; use crate::{piece::Piece, position, position::DiagramFormatter, BitBoard, Color, Position}; - use chess_core::Square; + use chessfriend_core::Square; #[test] fn classical_single_rook_bitboard() { diff --git a/board/src/piece.rs b/board/src/piece.rs index cc91765..b80d1da 100644 --- a/board/src/piece.rs +++ b/board/src/piece.rs @@ -1,7 +1,7 @@ // Eryn Wells use crate::display::{ASCIIDisplay, FENDisplay, UnicodeDisplay}; -use chess_core::Square; +use chessfriend_core::Square; use std::fmt; use std::slice::Iter; diff --git a/board/src/position/builders/move_builder.rs b/board/src/position/builders/move_builder.rs index 2146e9d..17eb36d 100644 --- a/board/src/position/builders/move_builder.rs +++ b/board/src/position/builders/move_builder.rs @@ -6,7 +6,7 @@ use crate::{ r#move::Castle, BitBoard, Color, MakeMoveError, Move, Piece, Position, }; -use chess_core::{Direction, Square}; +use chessfriend_core::{Direction, Square}; /// A position builder that builds a new position by making a move. #[derive(Clone)] diff --git a/board/src/position/builders/position_builder.rs b/board/src/position/builders/position_builder.rs index 0410ca3..0866094 100644 --- a/board/src/position/builders/position_builder.rs +++ b/board/src/position/builders/position_builder.rs @@ -7,8 +7,8 @@ use crate::{ r#move::Castle, BitBoard, Color, MakeMoveError, Move, Piece, Position, }; -use chess_core::{Rank, Square}; use std::collections::BTreeMap; +use chessfriend_core::Square; #[derive(Clone)] pub struct Builder { diff --git a/board/src/position/diagram_formatter.rs b/board/src/position/diagram_formatter.rs index ff4c3cf..0c46de4 100644 --- a/board/src/position/diagram_formatter.rs +++ b/board/src/position/diagram_formatter.rs @@ -1,7 +1,7 @@ // Eryn Wells use crate::Position; -use chess_core::{File, Rank, Square}; +use chessfriend_core::{File, Rank, Square}; use std::fmt; pub struct DiagramFormatter<'a>(&'a Position); diff --git a/board/src/position/piece_sets.rs b/board/src/position/piece_sets.rs index fa08c84..856a3e8 100644 --- a/board/src/position/piece_sets.rs +++ b/board/src/position/piece_sets.rs @@ -4,7 +4,7 @@ use crate::{ piece::{Piece, PlacedPiece}, BitBoard, Color, }; -use chess_core::Square; +use chessfriend_core::Square; #[derive(Debug, Eq, PartialEq)] pub enum PlacePieceStrategy { diff --git a/board/src/position/pieces.rs b/board/src/position/pieces.rs index 4e4495d..959a9da 100644 --- a/board/src/position/pieces.rs +++ b/board/src/position/pieces.rs @@ -3,7 +3,7 @@ use super::Position; use crate::piece::{Color, Piece, PlacedPiece, Shape}; use crate::BitBoard; -use chess_core::Square; +use chessfriend_core::Square; pub struct Pieces<'a> { color: Color, diff --git a/board/src/position/position.rs b/board/src/position/position.rs index ebc2948..e1f2f92 100644 --- a/board/src/position/position.rs +++ b/board/src/position/position.rs @@ -9,7 +9,7 @@ use crate::{ sight::Sight, BitBoard, Move, }; -use chess_core::Square; +use chessfriend_core::Square; use std::{cell::OnceCell, fmt}; #[derive(Clone, Debug, Eq, PartialEq)] @@ -268,7 +268,7 @@ impl fmt::Display for Position { #[cfg(test)] mod tests { use crate::{position, Castle, Color, Position}; - use chess_core::Square; + use chessfriend_core::Square; #[test] fn piece_on_square() { diff --git a/board/src/sight.rs b/board/src/sight.rs index 7763bac..92b295f 100644 --- a/board/src/sight.rs +++ b/board/src/sight.rs @@ -4,7 +4,7 @@ use crate::{ piece::{Color, PlacedPiece, Shape}, BitBoard, Position, }; -use chess_core::Direction; +use chessfriend_core::Direction; pub(crate) trait Sight { fn sight_in_position(&self, position: &Position) -> BitBoard; @@ -175,7 +175,7 @@ mod tests { mod pawn { use crate::{sight::Sight, BitBoard}; - use chess_core::Square; + use chessfriend_core::Square; sight_test!(e4_pawn, piece!(White Pawn on E4), bitboard!(D5, F5)); diff --git a/core/Cargo.toml b/core/Cargo.toml index 9b8c272..9b33128 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "chess_core" +name = "chessfriend_core" version = "0.1.0" edition = "2021" From 7738f30e5e51c487d6e1fca0d829e14684d9748c Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 09:14:59 -0800 Subject: [PATCH 09/12] Fix the workspace members The items in the members array are paths, not crate names. --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7542226..cfa9c81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ members = [ "board", - "chess_bitboard", - "chessfriend_core", + "bitboard", + "core", "explorer", ] From d901be53d2ff45173e8be11319fa69740eb1c724 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 09:15:18 -0800 Subject: [PATCH 10/12] [explorer] Depends on core --- explorer/Cargo.toml | 1 + explorer/src/main.rs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/explorer/Cargo.toml b/explorer/Cargo.toml index 5d49281..3f2513b 100644 --- a/explorer/Cargo.toml +++ b/explorer/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chessfriend_core = { path = "../core" } board = { path = "../board" } clap = { version = "4.4.12", features = ["derive"] } rustyline = "13.0.0" diff --git a/explorer/src/main.rs b/explorer/src/main.rs index 9da6a64..e8fca2f 100644 --- a/explorer/src/main.rs +++ b/explorer/src/main.rs @@ -1,5 +1,6 @@ use board::piece::{Color, Piece, Shape}; -use board::{Position, Square}; +use board::Position; +use chessfriend_core::Square; use clap::{Arg, Command}; use rustyline::error::ReadlineError; use rustyline::DefaultEditor; From 3cec64d6861d0b36fc88182e8ab1aa5d0c28a58d Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 09:16:21 -0800 Subject: [PATCH 11/12] [bitboard] Make the bitboard crate more crate-like Export symbols needed to use BitBoard and BitBoardBuilder. Fix build errors. --- bitboard/Cargo.toml | 2 +- bitboard/src/bitboard.rs | 8 ++++---- bitboard/src/lib.rs | 5 +++-- bitboard/src/library.rs | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/bitboard/Cargo.toml b/bitboard/Cargo.toml index e565de3..df608b5 100644 --- a/bitboard/Cargo.toml +++ b/bitboard/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "chess_bitboard" +name = "chessfriend_bitboard" version = "0.1.0" edition = "2021" diff --git a/bitboard/src/bitboard.rs b/bitboard/src/bitboard.rs index 5be905a..6aa0df2 100644 --- a/bitboard/src/bitboard.rs +++ b/bitboard/src/bitboard.rs @@ -7,7 +7,7 @@ use std::fmt; use std::ops::Not; #[derive(Clone, Copy, Eq, Hash, PartialEq)] -pub(crate) struct BitBoard(pub(super) u64); +pub struct BitBoard(pub(crate) u64); macro_rules! moves_getter { ($getter_name:ident) => { @@ -71,13 +71,13 @@ impl BitBoard { impl BitBoard { /// Return an Iterator over the occupied squares, starting from the leading /// (most-significant bit) end of the field. - pub(crate) fn occupied_squares(&self) -> impl Iterator { + pub fn occupied_squares(&self) -> impl Iterator { LeadingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) }) } /// Return an Iterator over the occupied squares, starting from the trailing /// (least-significant bit) end of the field. - pub(crate) fn occupied_squares_trailing(&self) -> impl Iterator { + pub fn occupied_squares_trailing(&self) -> impl Iterator { LeadingBitScanner::new(self.0).map(|idx| unsafe { Square::from_index(idx as u8) }) } } @@ -244,7 +244,7 @@ impl BitBoardBuilder { mod tests { use super::*; use crate::bitboard; - use chess_core::Square; + use chessfriend_core::Square; #[test] fn display_and_debug() { diff --git a/bitboard/src/lib.rs b/bitboard/src/lib.rs index 6b0703e..30a35f2 100644 --- a/bitboard/src/lib.rs +++ b/bitboard/src/lib.rs @@ -5,13 +5,14 @@ mod bitboard; mod library; mod shifts; +pub use bitboard::{BitBoard, BitBoardBuilder}; + pub(crate) use bit_scanner::{LeadingBitScanner, TrailingBitScanner}; -pub(crate) use bitboard::{BitBoard, BitBoardBuilder}; #[macro_export] macro_rules! bitboard { ($($sq:ident),* $(,)?) => { - $crate::bitboard::BitBoardBuilder::empty() + $crate::BitBoardBuilder::empty() $(.square(chessfriend_core::Square::$sq))* .build() }; diff --git a/bitboard/src/library.rs b/bitboard/src/library.rs index 506d6cd..6c59291 100644 --- a/bitboard/src/library.rs +++ b/bitboard/src/library.rs @@ -1,6 +1,6 @@ // Eryn Wells -use super::BitBoard; +use crate::BitBoard; use chessfriend_core::{Direction, Square}; use std::sync::Once; From 6f85305912d6b8ca4cc290f6f6e7e2290ba9e145 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 24 Jan 2024 09:18:12 -0800 Subject: [PATCH 12/12] [board] Clean up a bunch of build errors Fix imports to refer to core and bitboard crates. Fix some API use errors. --- board/Cargo.toml | 1 + board/src/lib.rs | 2 -- board/src/move_generator/bishop.rs | 6 ++++-- board/src/move_generator/king.rs | 6 ++++-- board/src/move_generator/knight.rs | 6 ++++-- board/src/move_generator/mod.rs | 19 +++++++++++-------- board/src/move_generator/move_generator.rs | 3 ++- board/src/move_generator/move_set.rs | 3 ++- board/src/move_generator/pawn.rs | 6 ++++-- board/src/move_generator/queen.rs | 6 ++++-- board/src/move_generator/rook.rs | 6 ++++-- board/src/position/builders/move_builder.rs | 3 ++- .../src/position/builders/position_builder.rs | 9 ++++----- board/src/position/piece_sets.rs | 5 +++-- board/src/position/pieces.rs | 2 +- board/src/position/position.rs | 3 ++- board/src/sight.rs | 9 +++++++-- 17 files changed, 59 insertions(+), 36 deletions(-) diff --git a/board/Cargo.toml b/board/Cargo.toml index 2f8727d..70584c2 100644 --- a/board/Cargo.toml +++ b/board/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" [dependencies] chessfriend_core = { path = "../core" } +chessfriend_bitboard = { path = "../bitboard" } diff --git a/board/src/lib.rs b/board/src/lib.rs index 2ed3495..243686e 100644 --- a/board/src/lib.rs +++ b/board/src/lib.rs @@ -14,5 +14,3 @@ mod sight; pub use piece::{Color, Piece}; pub use position::{MoveBuilder as MakeMoveBuilder, Position, PositionBuilder}; pub use r#move::{Castle, MakeMoveError, Move, MoveBuilder}; - -pub(crate) use bitboard::BitBoard; diff --git a/board/src/move_generator/bishop.rs b/board/src/move_generator/bishop.rs index cd259f7..404d68b 100644 --- a/board/src/move_generator/bishop.rs +++ b/board/src/move_generator/bishop.rs @@ -3,8 +3,9 @@ use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use crate::{ piece::{Color, Piece, PlacedPiece}, - BitBoard, MoveBuilder, Position, + MoveBuilder, Position, }; +use chessfriend_bitboard::BitBoard; use chessfriend_core::Direction; move_generator_declaration!(ClassicalMoveGenerator); @@ -60,7 +61,8 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> { #[cfg(test)] mod tests { use super::*; - use crate::{piece, piece::Color, position, position::DiagramFormatter, BitBoard}; + use crate::{piece, piece::Color, position, position::DiagramFormatter}; + use chessfriend_bitboard::BitBoard; #[test] fn classical_single_bishop_bitboard() { diff --git a/board/src/move_generator/king.rs b/board/src/move_generator/king.rs index 06ae76e..643a987 100644 --- a/board/src/move_generator/king.rs +++ b/board/src/move_generator/king.rs @@ -7,8 +7,9 @@ use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use crate::{ piece::{Color, Piece, PlacedPiece}, r#move::Castle, - BitBoard, Move, MoveBuilder, Position, + Move, MoveBuilder, Position, }; +use chessfriend_bitboard::BitBoard; move_generator_declaration!(KingMoveGenerator, struct); move_generator_declaration!(KingMoveGenerator, new); @@ -76,7 +77,8 @@ impl<'pos> MoveGeneratorInternal for KingMoveGenerator<'pos> { #[cfg(test)] mod tests { use super::*; - use crate::Square; + use chessfriend_bitboard::bitboard; + use chessfriend_core::Square; use std::collections::HashSet; #[test] diff --git a/board/src/move_generator/knight.rs b/board/src/move_generator/knight.rs index 50a7ad0..64b360f 100644 --- a/board/src/move_generator/knight.rs +++ b/board/src/move_generator/knight.rs @@ -3,8 +3,9 @@ use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use crate::{ piece::{Color, Piece, PlacedPiece}, - BitBoard, MoveBuilder, Position, + MoveBuilder, Position, }; +use chessfriend_bitboard::BitBoard; move_generator_declaration!(KnightMoveGenerator); @@ -40,7 +41,8 @@ impl<'pos> MoveGeneratorInternal for KnightMoveGenerator<'pos> { #[cfg(test)] mod tests { use super::*; - use crate::{piece, position, Move, Square}; + use crate::{piece, position, Move}; + use chessfriend_core::Square; use std::collections::HashSet; #[test] diff --git a/board/src/move_generator/mod.rs b/board/src/move_generator/mod.rs index eb080c3..ed5ac74 100644 --- a/board/src/move_generator/mod.rs +++ b/board/src/move_generator/mod.rs @@ -14,8 +14,9 @@ pub(crate) use move_set::MoveSet; use crate::{ piece::{Color, Piece, PlacedPiece}, - Move, Position, Square, + Move, Position, }; +use chessfriend_core::Square; use std::collections::BTreeMap; trait MoveGenerator { @@ -34,7 +35,10 @@ macro_rules! move_generator_declaration { pub(super) struct $name<'pos> { position: &'pos $crate::Position, color: $crate::piece::Color, - move_sets: std::collections::BTreeMap<$crate::Square, $crate::move_generator::MoveSet>, + move_sets: std::collections::BTreeMap< + chessfriend_core::Square, + $crate::move_generator::MoveSet, + >, } }; ($name:ident, new) => { @@ -54,12 +58,11 @@ macro_rules! move_generator_declaration { self.move_sets.values().map(|set| set.moves()).flatten() } - fn bitboard(&self) -> $crate::BitBoard { - self.move_sets - .values() - .fold($crate::BitBoard::empty(), |partial, mv_set| { - partial | mv_set.bitboard() - }) + fn bitboard(&self) -> chessfriend_bitboard::BitBoard { + self.move_sets.values().fold( + chessfriend_bitboard::BitBoard::empty(), + |partial, mv_set| partial | mv_set.bitboard(), + ) } } }; diff --git a/board/src/move_generator/move_generator.rs b/board/src/move_generator/move_generator.rs index 689e47d..2ef4922 100644 --- a/board/src/move_generator/move_generator.rs +++ b/board/src/move_generator/move_generator.rs @@ -43,7 +43,8 @@ impl<'a> Moves<'a> { #[cfg(test)] mod tests { - use crate::{piece, position, r#move::AlgebraicMoveFormatter, Move, MoveBuilder, Square}; + use crate::{piece, position, r#move::AlgebraicMoveFormatter, Move, MoveBuilder}; + use chessfriend_core::Square; use std::collections::HashSet; #[test] diff --git a/board/src/move_generator/move_set.rs b/board/src/move_generator/move_set.rs index 1c44766..1c22eac 100644 --- a/board/src/move_generator/move_set.rs +++ b/board/src/move_generator/move_set.rs @@ -1,4 +1,5 @@ -use crate::{piece::PlacedPiece, BitBoard, Move}; +use crate::{piece::PlacedPiece, Move}; +use chessfriend_bitboard::BitBoard; #[derive(Clone, Debug, Eq, PartialEq)] struct BitBoardSet { diff --git a/board/src/move_generator/pawn.rs b/board/src/move_generator/pawn.rs index 7f77e15..d366405 100644 --- a/board/src/move_generator/pawn.rs +++ b/board/src/move_generator/pawn.rs @@ -2,8 +2,9 @@ use crate::{ piece::{Color, Piece, Shape}, - BitBoard, Move, MoveBuilder, Position, + Move, MoveBuilder, Position, }; +use chessfriend_bitboard::BitBoard; enum MoveList { Quiet = 0, @@ -246,7 +247,8 @@ impl<'pos> Iterator for PawnMoveGenerator<'pos> { mod tests { use super::*; use crate::position::DiagramFormatter; - use crate::{piece, Position, Square}; + use crate::{piece, Position}; + use chessfriend_core::Square; use std::collections::HashSet; #[test] diff --git a/board/src/move_generator/queen.rs b/board/src/move_generator/queen.rs index f012d8c..fb96dd7 100644 --- a/board/src/move_generator/queen.rs +++ b/board/src/move_generator/queen.rs @@ -3,8 +3,9 @@ use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use crate::{ piece::{Color, Piece, PlacedPiece}, - BitBoard, MoveBuilder, Position, + MoveBuilder, Position, }; +use chessfriend_bitboard::BitBoard; use chessfriend_core::Direction; move_generator_declaration!(ClassicalMoveGenerator); @@ -66,7 +67,8 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> { #[cfg(test)] mod tests { use super::*; - use crate::{piece, position, position::DiagramFormatter, BitBoard, Color}; + use crate::{piece, position, position::DiagramFormatter, Color}; + use chessfriend_bitboard::{bitboard, BitBoard}; #[test] fn classical_single_queen_bitboard() { diff --git a/board/src/move_generator/rook.rs b/board/src/move_generator/rook.rs index c6dbb7b..570255a 100644 --- a/board/src/move_generator/rook.rs +++ b/board/src/move_generator/rook.rs @@ -3,8 +3,9 @@ use super::{move_generator_declaration, MoveGeneratorInternal, MoveSet}; use crate::{ piece::{Color, Piece, PlacedPiece}, - BitBoard, MoveBuilder, Position, + MoveBuilder, Position, }; +use chessfriend_bitboard::BitBoard; use chessfriend_core::Direction; move_generator_declaration!(ClassicalMoveGenerator); @@ -62,7 +63,8 @@ impl<'pos> MoveGeneratorInternal for ClassicalMoveGenerator<'pos> { #[cfg(test)] mod tests { use super::*; - use crate::{piece::Piece, position, position::DiagramFormatter, BitBoard, Color, Position}; + use crate::{piece::Piece, position, position::DiagramFormatter, Color, Position}; + use chessfriend_bitboard::{bitboard, BitBoard}; use chessfriend_core::Square; #[test] diff --git a/board/src/position/builders/move_builder.rs b/board/src/position/builders/move_builder.rs index 17eb36d..4530543 100644 --- a/board/src/position/builders/move_builder.rs +++ b/board/src/position/builders/move_builder.rs @@ -4,8 +4,9 @@ use crate::{ piece::{PlacedPiece, Shape}, position::flags::Flags, r#move::Castle, - BitBoard, Color, MakeMoveError, Move, Piece, Position, + Color, MakeMoveError, Move, Piece, Position, }; +use chessfriend_bitboard::BitBoard; use chessfriend_core::{Direction, Square}; /// A position builder that builds a new position by making a move. diff --git a/board/src/position/builders/position_builder.rs b/board/src/position/builders/position_builder.rs index 0866094..88838b7 100644 --- a/board/src/position/builders/position_builder.rs +++ b/board/src/position/builders/position_builder.rs @@ -1,14 +1,13 @@ // Eryn Wells use crate::{ - bitboard::BitBoardBuilder, piece::{PlacedPiece, Shape}, position::{flags::Flags, piece_sets::PieceBitBoards}, r#move::Castle, - BitBoard, Color, MakeMoveError, Move, Piece, Position, + Color, MakeMoveError, Move, Piece, Position, }; +use chessfriend_core::{Rank, Square}; use std::collections::BTreeMap; -use chessfriend_core::Square; #[derive(Clone)] pub struct Builder { @@ -109,8 +108,8 @@ impl Builder { impl Default for Builder { fn default() -> Self { - let white_king_square = Square::king_starting_square(Color::White); - let black_king_square = Square::king_starting_square(Color::Black); + let white_king_square = Square::E1; + let black_king_square = Square::E8; let pieces = BTreeMap::from_iter([ (white_king_square, piece!(White King)), diff --git a/board/src/position/piece_sets.rs b/board/src/position/piece_sets.rs index 856a3e8..d07eb2a 100644 --- a/board/src/position/piece_sets.rs +++ b/board/src/position/piece_sets.rs @@ -2,8 +2,9 @@ use crate::{ piece::{Piece, PlacedPiece}, - BitBoard, Color, + Color, }; +use chessfriend_bitboard::BitBoard; use chessfriend_core::Square; #[derive(Debug, Eq, PartialEq)] @@ -125,7 +126,7 @@ impl PieceBitBoards { impl FromIterator for PieceBitBoards { fn from_iter>(iter: T) -> Self { - let mut pieces = Self::default(); + let mut pieces: Self = Default::default(); for piece in iter { let _ = pieces.place_piece(&piece); diff --git a/board/src/position/pieces.rs b/board/src/position/pieces.rs index 959a9da..8530869 100644 --- a/board/src/position/pieces.rs +++ b/board/src/position/pieces.rs @@ -2,7 +2,7 @@ use super::Position; use crate::piece::{Color, Piece, PlacedPiece, Shape}; -use crate::BitBoard; +use chessfriend_bitboard::BitBoard; use chessfriend_core::Square; pub struct Pieces<'a> { diff --git a/board/src/position/position.rs b/board/src/position/position.rs index e1f2f92..82ef1bd 100644 --- a/board/src/position/position.rs +++ b/board/src/position/position.rs @@ -7,8 +7,9 @@ use crate::{ position::DiagramFormatter, r#move::Castle, sight::Sight, - BitBoard, Move, + Move, }; +use chessfriend_bitboard::BitBoard; use chessfriend_core::Square; use std::{cell::OnceCell, fmt}; diff --git a/board/src/sight.rs b/board/src/sight.rs index 92b295f..ffed3f7 100644 --- a/board/src/sight.rs +++ b/board/src/sight.rs @@ -2,8 +2,9 @@ use crate::{ piece::{Color, PlacedPiece, Shape}, - BitBoard, Position, + Position, }; +use chessfriend_bitboard::BitBoard; use chessfriend_core::Direction; pub(crate) trait Sight { @@ -174,7 +175,8 @@ mod tests { } mod pawn { - use crate::{sight::Sight, BitBoard}; + use crate::sight::Sight; + use chessfriend_bitboard::{bitboard, BitBoard}; use chessfriend_core::Square; sight_test!(e4_pawn, piece!(White Pawn on E4), bitboard!(D5, F5)); @@ -234,6 +236,7 @@ mod tests { #[macro_use] mod knight { use crate::sight::Sight; + use chessfriend_bitboard::bitboard; sight_test!( f6_knight, @@ -244,6 +247,7 @@ mod tests { mod bishop { use crate::sight::Sight; + use chessfriend_bitboard::bitboard; sight_test!( c2_bishop, @@ -254,6 +258,7 @@ mod tests { mod rook { use crate::sight::Sight; + use chessfriend_bitboard::bitboard; sight_test!( g3_rook,