Commit graph

473 commits

Author SHA1 Message Date
997621eea7 [bitboard] Make a few tweaks to some definitions in BitBoard
Use u64::MIN and u64::MAX to define the empty and full bitboards
Write From<Square> as `1u64 << sq as u32`
Write a doc test for BitBoard::is_single_square()
Make library::RANKS and library::FILES pub(crate) instead of pub(super)
2024-02-11 09:58:06 -07:00
891b3ddbb9 [position] Remove the sight data from Position 2024-02-05 14:00:23 -08:00
a5e8f33afe [core] Implement Square::file_rank()
Returns a tuple of the square's file and rank.
2024-02-05 13:59:58 -08:00
a8034c79aa [bitboard] Use a OnceLock to hold the global BitBoard library instance
Thread safe and a single object instead of two!
2024-02-05 13:59:26 -08:00
4b35051deb [position] Derive several traits for CheckingPieces
Clone, Debug, Eq, and PartialEq.
2024-02-04 19:26:41 -08:00
17410936ab [position] Remove the rook_push_mask test from check.rs
This test now lives in move_generator::tests::peterellisjones
2024-02-04 19:26:11 -08:00
f4e57d7d6c [position] Implement all the example positions from Peter Ellis Jones' blog post
https://peterellisjones.com/posts/generating-legal-chess-moves-efficiently/
2024-02-03 15:16:00 -08:00
6c14851806 [position] Move whole-module move_generator tests to a tests/ module 2024-02-03 10:05:11 -08:00
4a601c2b81 [position] Fix all the unit tests
The pawn move generator only generated pushes for white pawns.
The is_king_in_check returned an inverted flag.
MoveSet needed a couple more validation methods: can_move_to_square and can_castle.
The MakeMoveBuilder also needed a little more move validation using the above methods.
2024-02-03 10:04:41 -08:00
63758a2edd Remove the move_generator crate from the Cargo workspace 2024-02-02 08:07:13 -08:00
efcc65d8d6 Merge branch 'checking-pieces-struct' 2024-02-02 08:06:17 -08:00
942c758e15 [position] Calculate legal moves based on whether the king is in check
Use CheckingPieces to determine which and how many pieces check the king.

- If there are no checks, proceed with move generation as normal.
- If there is one checking piece, calculate push and capture masks and use those
   to generate legal moves.
- If there are more than one checking pieces, the only legal moves are king
  moves. Indicate this by setting the push and capture masks to EMPTY.
2024-02-02 08:05:37 -08:00
986758d011 [position] For non-King move generators, only generate moves if the push and captures masks aren't empty
Always generate King moves.
2024-02-02 08:03:51 -08:00
31903cb514 [position] Calculate push and capture masks on CheckingPieces 2024-02-02 07:53:50 -08:00
ac07a8d6cf [position] Implement SliderRayToSquareExt on Shape instead of PlacedPiece
Add an origin square argument to its one method ::ray_to_square(). Pushing this
implementation down a layer means I don't have to care about the color of the
piece.
2024-02-02 07:29:43 -08:00
c8faad799e [position] Clean up Position's construction scheme
Position::default specifies the defaults for all field. Then, ::new() and
::starting() can use ..Default::default() in their implementations to avoid
having to specify empty values for all the internal structures.
2024-02-02 07:28:12 -08:00
f09376f5dc [position] Make the checking_piece! macro handle specifying the path to the piece constructor 2024-02-02 07:25:59 -08:00
032fabe072 [bitboard] Return BitBoard::EMPTY from BitBoard's Default impl 2024-02-01 08:45:10 -08:00
ca861df9c4 [bitboard] Use TrailingBitScanner in occupied_squares_trailing
This was an oversight.
2024-02-01 08:44:53 -08:00
deea23352b [bitboard] Implement BitBoard::population_count()
Uses u64::count_ones() to return the population count of the BitBoard.
2024-02-01 08:44:25 -08:00
7b97060ba2 [position] The KingMoveGenerator doesn't use push or capture masks
Mark these arguments with an _ so the linter stops complaining about it.
2024-02-01 08:43:49 -08:00
f1a05f33c9 [position] Factor out the update_moves_with_ray! as ray_in_direction!
This macro is implemented in every sight method. Factor it out, rename it, and
use it in all these macros.
2024-02-01 08:43:03 -08:00
722a90b860 [position] Implement a SliderRayToSquare trait
This trait declares ray_to_square() which should return a BitBoard representing
a ray from a Square to another Square. The ray should include the target Square.

PlacedPiece implements this trait.
2024-02-01 08:42:19 -08:00
cac13b4bc7 Cargo.lock changes 2024-01-30 08:53:15 -08:00
8aa5dacfc8 Initial implementation of CheckingPieces and Position::checking_pieces() 2024-01-30 08:53:13 -08:00
26aedd8899 [position] Implement Position::king_bitboard to easily get the king bitboard for a player
Rewrite king_square() and is_king_in_check() in terms of king_bitboard().
2024-01-30 08:36:03 -08:00
357b811518 [position] Add a fen! macro 2024-01-30 08:35:02 -08:00
8cdb9f13b4 [position] Apply capture and push masks to move generator results 2024-01-30 08:34:29 -08:00
98c8ef6e24 [position] Remove the unused Position::move_is_legal 2024-01-29 20:12:41 -08:00
13e166e059 [position] Remove an unnecessary type annotation 2024-01-29 20:12:26 -08:00
b93f8684fa [position] Plumb capture mask and push mask arguments through all move generators
These masks will help when generating moves that address checks.
Create BitBoard::EMPTY and BitBoard::FULL.
2024-01-29 20:12:12 -08:00
9c4360c886 [position] Remove move_generator::MoveListSet 2024-01-29 19:02:02 -08:00
8aa44e56f2 [position] Fix a few warnings related to imports; make position::Flags public 2024-01-29 19:01:44 -08:00
52b19b87d8 [position] Implement FromFen for Position, Piece, and Color
I can now create Positions from FEN strings!
2024-01-29 16:10:08 -08:00
3239f288d7 [bitboard] Bitboards for kingside and queenside per color
Add two small BitBoard slices that represent kingside and queenside squares per
color.

Add doc comments to DARK_SQUARES and LIGHT_SQUARES.
Add getters on BitBoard for getting a boardside bitboard.
Clean up imports. Import the whole library module and refer to library things in
BitBoard by path.
2024-01-29 15:00:53 -08:00
d6bd6aec0f [position] Lazily build move lists in MoveGenerator structs
Merge branch 'lazily-build-move-lists'
2024-01-29 14:47:25 -08:00
1d7dada987 [bitboard, core, position] Implement proper castle move generation
Add a field to MoveSet called special that flags special moves that should be
generated in the iter() method. This field is a u8. It only tracks castles in
the first and second bits (kingside and queenside, respectively). The move iterator
chains two maps over Option<()> that produce the kingside and queenside castle
moves.

With that done, finish the implementation of Position::player_can_castle by
adding checks for whether the squares between the rook and king are clear, and
that the king would not pass through a check. This is done with BitBoards!

Finally, implement some logic in PositionBuilder that updates the position's
castling flags based on the positions of king and rooks.

Supporting changes:
- Add Color:ALL and iterate on that slice
- Add Castle::ALL and iterator on that slice
- Add a CastlingParameters struct that contains BitBoard properties that describe
  squares that should be clear of pieces and squares that should not be attacked.
2024-01-29 14:46:41 -08:00
83a4e47e56 MoveGenerator::iter() returns an iterator of moves-by-value 2024-01-29 14:46:41 -08:00
ea22f7c5c7 Clean up some test imports 2024-01-29 14:46:41 -08:00
2d5710ccb1 Clean up Pawn::pushes a little bit 2024-01-29 14:46:41 -08:00
296a57d7ac Remove move list arguments from king and pawn move set constuction
These are harder.
2024-01-29 14:46:41 -08:00
5e3ef9d21e Remove the move lists from bishop, knight, queen, and rook move set construction
These are the easy ones.
2024-01-29 14:46:41 -08:00
cd3cb82192 Add an assert_move_list! macro to help with verifying move lists 2024-01-29 14:46:41 -08:00
d910ff708e Remove the move list argument from MoveList::quiet_moves and capture_moves
Produce an iterator of Moves in MoveList::moves
2024-01-29 14:46:41 -08:00
c558800385 [position] Fix a bug in the pawn pushes move generator 2024-01-29 14:46:22 -08:00
1f78d4811a [core] Declare Rank::PAWN_STARTING_RANKS
This is a slice that declares the pawn starting ranks for each color.
2024-01-29 14:46:16 -08:00
cb48413ce7 [bitboard] Implement BitBoard::is_single_square()
Returns true if there's only one square set in the bitboard.
2024-01-29 14:46:16 -08:00
0f664f6c80 [bitboard] Implement BitBoard::as_bits; let rank and file take &u8 instead of usize 2024-01-29 14:46:16 -08:00
21c81f237a [core] Implement as_index() for range_bound_struct 2024-01-29 14:46:16 -08:00
dab787170c [position] Clean up rook unit tests
Use test_position! instead of position!
Spell out the PlacedPiece constructor in the test_position! macro.
2024-01-28 10:28:01 -08:00