Commit graph

515 commits

Author SHA1 Message Date
bba88bd0e6 [position] Export Position::zobrist_hash()
Pass-through call to self.board.zobrist_hash() to return the hash value of the
current board position.
2025-06-07 08:50:20 -07:00
046aae72c8 Add a rustfmt.toml file
Reorganize and reformat imports, and make sure comments are wrapped to 80.
2025-06-07 08:48:45 -07:00
fb8a8d6110 [explorer] Add zobrist command
It prints the hash of the board!
2025-06-07 08:09:36 -07:00
d44c5d6a11 [board] Remove an unused test helper function
test_hash() was never used. Remove it.
2025-06-06 21:46:13 -07:00
1686eeb35a [board] A small set of unit tests for Zobrist hashes on Board
A few tests to ensure the zobrist hash changes when changes are made to the Board.
This set isn't exhaustive.
2025-06-06 21:45:48 -07:00
651c982ead [board, moves, position] Make the Peter Ellis Jones gotcha unit tests work
Move this file over to position/tests. That makes it an integration test, technically.
Update it to comply with the current API conventions. This is a pretty radical
change from when I first wrote these!

In the process of running these tests, I found a bug in my PawnMoveGenerator where
it was generating the origin squares for en passant captures incorrectly. Fix
those bugs and write two new tests to exercise those code paths.

One of the test_board! variants was setting .active_color instead of using the
setter. This is a build failure. Fix it.

Move the assert_move_list! macro to the moves crate. This is a more natural home
for it. Additionally, add assert_move_list_contains! and assert_move_list_does_not_contain!
to generate assertions that a collection of moves (anything that implements
.contains()) has or doesn't have a set of moves. Also remove formatted_move_list!,
which is no longer used.

All that done, make the tests pass!
2025-06-06 21:45:07 -07:00
d7f426697d [board, position] Implement Zobrist hashing
This change builds on several previous changes to implement Zobrist hashing of the
board. This hash can be updated incrementally as changes are made to the board.
In order to do that, various properties of the Board struct had to made internal.
In the setters and various mutating members of Board, the hash is updated as
state changes.

The entire hashing mechanism is optional. If no ZobristState is provided when the
Board is created, the hash is never computed.

Plumb the Zobrist state through Position as well so that clients of Position (the
ultimate interface for interacting with the chess engine) can provide global
state to the whole engine.

The explorer crate gives an example of how this works. Some global state is
computed during initialization and then passed to the Position when it's created.
2025-06-05 08:22:34 -07:00
404212363e [board, moves] Make Board::castling_rights and Board::en_passant_target private
Make the struct fields private and export getters and various setters for
manipulating the data.

Update all the references to these fields to use the getters and setters instead.
2025-06-03 20:25:53 -07:00
eaab34587c [board, explorer, moves] Make Board::active_color private
Make the struct attribute private, and export two new methods. A getter, active_color(),
and a setter, set_active_color().

Update all references to the attribute to use the methods.
2025-06-02 17:29:52 -07:00
cae93cb090 [board, position] Return replaced piece when placing a piece with PlacePieceStrategy::Replace
When place_piece() is called with PlacePieceStrategy::Replace, return the replaced
piece in the Ok() variant of the Result as an Option<Piece>.

Plumb this new return type through Board and Position.
2025-06-02 15:54:00 -07:00
09fbe1be22 [board] Make Board::pieces private
Do not allow direct access to the internal piece set. Update call sites to use
Board API instead.
2025-06-02 15:46:10 -07:00
6d0df32f74 [core] Random Number Generator
Implement a random number generator as a thin wrapper around rand::StdRng. Provide
some default seed data to get consistent random numbers.
2025-06-02 15:44:38 -07:00
f47654cc98 [board] Trying out a new naming convention in the check methods
I've been bothered by certain instances where the color has already been unwrapped
from an Option<Color> but a subsequent call takes an Option, so you have to rewrap
it to call the method. I might be overthinking this…

For the Board::*_color_is_in_check set of methods, try out this naming convention:

active_color_is_in_check()
: Operates directly on the Board's active color, no unwrapping required
color_is_in_check()
: Takes an Option<Color> and unwraps it with the active color if None is given
unwrapped_color_is_in_check()
: Takes a Color and operates on it. This method is called by the two above, but
is also public.
2025-06-01 19:07:58 -07:00
f8a3d5831e [moves] Implement From<GeneratedMove> for Move
For easy conversion of a GeneratedMove to a Move.
2025-06-01 19:03:28 -07:00
8f42a4c94e [explorer, moves, position] Implement bespoke make_move and unmake_move methods on Position
Instead of inheriting the MakeMove and UnmakeMove traits by being a BoardProvider,
implement bespoke versions of these two methods. This gives Position a chance to
do some of its own work (tracking captures, move records, etc) when making a move.

Pass the move record by reference to the unmake_move() method. Saves a copy.

Export the Result types for MakeMove and UnmakeMove.
2025-06-01 19:02:53 -07:00
724a98c2e2 [board] Implement iter() on board
This iterator yields (Square, Piece) tuples for all pieces on the board.
2025-06-01 17:28:47 -07:00
f60cb8cf69 [moves, position] Implement unmaking moves on a board
Declare an UnmakeMove trait in the moves crate, just like the MakeMove trait from
an earlier commit. Implement this trait for all types that also implement
BoardProvider.

Bring in a whole pile of unit tests from Claude. (Holy shit, using Claude really
saves time on these tests…) Several of these tests failed, and all of those
failures revealed bugs in either MakeMove or UnmakeMove. Huzzah! Include fixes for
those bugs here.
2025-05-31 20:17:18 -07:00
40e8e055f9 [board, moves, position] Move make_move routines to moves crate
Declare a MakeMove trait and export it from chessfriend_moves. Declare a
BoardProvider trait that both Board and Position implement.

Implement the MakeMove trait for all types that implement BoardProvider, and move
all the move making code to the moves crate.

This change makes it possible to make moves directly on a Board, rather than
requiring a Position. The indirection of declaring and implementing the trait
in the moves crate is required because chessfriend_board is a dependency of
chessfriend_moves. So, it would be a layering violation for Board to implement
make_move() directly. The board crate cannot link the moves crate because that
would introduce a circular dependency.
2025-05-31 19:04:21 -07:00
ecde338602 [position] Remove some unused imports 2025-05-31 15:14:42 -07:00
c3a43fd2ed [board, moves] Update some comments and docs 2025-05-31 15:14:24 -07:00
086f9c5666 [board] Replace PieceSet's derived Hash and PartialEq with bespoke implementation
The piece set duplicates some data to make lookups more efficient. Only use the
necessary, unique data for these functions.
2025-05-31 15:07:20 -07:00
34e8c08c36 [moves, position] Move MoveRecord to the moves crate 2025-05-31 14:32:39 -07:00
b8f45aaece [position] Add some documentation to MoveRecord 2025-05-31 14:28:27 -07:00
a4b713a558 [position] Remove dead move_generator code 2025-05-29 09:21:25 -07:00
942d9fe47b [explorer, moves, position] Implement a moves command in explorer
The moves command writes all possible moves to the terminal.

Move the previous implementation of the moves command, which marked squares that
a piece could move to, to a 'movement' command.
2025-05-28 16:25:55 -07:00
43abbe3fe2 [position] Register a captured piece in the MoveRecord
For unmake_move, so the captured piece can be restored.
2025-05-28 16:23:46 -07:00
2106e05d57 [moves] Implement AllPiecesMoveGenerator
A generator that yields moves for all pieces on the board. This generator combines
the shape-specific move generators developed in prior commits into a single
iterator that yields all moves.

Implement FusedIterator for all generators so AllPiecesMoveGenerator can avoid
doing a bunch of extra work once each iterator has yielded None.
2025-05-28 16:22:16 -07:00
19c6c6701a [position] Fix broken tests build
The make_move tests were trying to access the last capture piece directly from a
slice of captures pieces (implementation prior to CapturesList). Implement
CapturesList::last() to return an Option<&Piece> and use it in the tests.
2025-05-27 12:04:24 -07:00
085697d38f [position] Remove position::position module
Move the implementation of Position to the top-level position.rs file.
2025-05-27 11:59:42 -07:00
db489af50b [position] Move unmake move stuff to an unmake_move module 2025-05-27 11:52:17 -07:00
a8ea248972 [position] Derive Default implementation for Position 2025-05-27 11:51:08 -07:00
e3ca466737 [position] Move capture list to a CapturesList struct
This one has a custom Display implementation for easier integration with Positon's.
2025-05-27 11:51:08 -07:00
eb6f2000a9 [board, moves, position] Implement KingMoveGenerator
Implement a move generator that emits moves for the king(s) of a particular color.
There will, of course, only ever be one king per side in any valid board, but
this iterator can (in theory) handle multiple kings on the board. This iterator
is almost entirely copypasta of the SliderMoveGenerator. The major difference is
castling.

Castle moves are emitted by a helper CastleIterator type. This struct collects
information about whether the given color can castle on each side of the board
and then emits moves for each side, if indicated.

Do some light refactoring of the castle-related methods on Board to accommodate
this move generator. Remove the dependency on internal state and rename the
"can_castle" method to color_can_castle.

In order to facilitate creating castling moves without relying on Board, remove
the origin and target squares from the encoded castling move. Code that makes
a castling move already looks up castling parameters to move the king and rook to
the right squares, so encoding those squares was redundant. This change
necessitated some updates to position.

Lastly, bring in a handful of unit tests courtesy of Claude. Apparently, it's my
new best coding friend. 🙃
2025-05-26 23:37:33 -07:00
f005d94fc2 [bitboard, board, core, moves] Implement SliderMoveGenerator
This generator produces moves for slider pieces: bishops, rooks, and queens. All
of these pieces behave identically, though with different sets of rays that
emanate from the origin square. Claude helped me significantly with the
implementation and unit testing. All the unit tests that took advantage of Claude
for implementation are marked as such with an _ai_claude suffix to the test name.

One unique aspect of this move generator that Claude suggested to me was to use
loop { } instead of a recursive call to next() when the internal iterators expire.
I may try to port this to the other move generators in the future.

To support this move generator, implement a Slider enum in core that represents
one of the three slider pieces.

Add Board::bishops(), Board::rooks() and Board::queens() to return BitBoards of
those pieces. These are analogous to the pawns() and knights() methods that return
their corresponding pieces.

Also in the board create, replace the separate sight method implementations with
a macro. These are all the same, but with a different sight method called under
the hood.

Finally, derive Clone and Debug for the bit_scanner types.
2025-05-26 17:41:43 -07:00
2c6a7828bc [moves] Two new pawn move tests 2025-05-25 11:05:21 -07:00
faca844733 [moves] Knight move generator and tests 2025-05-25 11:05:10 -07:00
3f3842c7c8 [moves] Add several macros to help with testing: ply! and assert_move_list!
ply! implements a small DSL for writing moves in code using a natural-ish
algebraic notation.

assert_move_list! takes a generator and an expected list of moves and asserts
that they're equal. This macro is mostly a copy from one I wrote earlier in the
position crate.
2025-05-25 11:04:49 -07:00
09bf17d66b [moves] Implement promotions and en passant in the PawnMoveGenerator
Add another sub-iterator to the PawnMoveGenerator that produces promotion pushes
or capture moves (depending on move_type) when a pawn moves to the back rank.

Also implement en passant moves.

Fix a bug in the Left and Right captures branches where the wrong neighbors used
to calculate origin squares.

Add a whole bunch of tests. Still missing several cases though.
2025-05-24 18:01:14 -07:00
ab587f379f [board] Fix a bug in PieceSet::opposing_occupancy
This method should compute occupancy of everything except the provided color. Instead
it was computing the inverse.
2025-05-24 17:54:46 -07:00
5466693c1b [position] Remove empty implementation of Position::unmake_move 2025-05-23 18:39:38 -07:00
1da08df430 [board] Implement a couple handy piece getters
Board::find_pieces returns a BitBoard for all the pieces matching the given Piece.

Board::enemies returns a BitBoard of all the enemy pieces of a given Color.

Board::pawns returns a BitBoard of all the pawns of a given Color.
2025-05-23 18:39:18 -07:00
c02f0170b9 [bitboard] Export the bit_scanner module
Clients can access TrailingBitScanner and LeadingBitScanner directly now.
2025-05-23 18:38:15 -07:00
994f17091b [board] Implement Board::unwrap_color
Unwrap an Option<Color> using the board's active color in the default case.
2025-05-23 18:37:13 -07:00
574ab803dd [moves] Implement a move generator for pawns
I used Claude to help me figure this out. First time using AI for coding. It was
actually rather helpful!

Calculate BitBoards representing the various kinds of pawn moves when the move
generator is created, and then iterate through them.

En passant still isn't implemented here. This code has not been tested yet either.
2025-05-23 18:36:22 -07:00
af2bff348f [core] Add an Option<i8> argument to Square::neighbor
This argument allows computing the neighbor n squares away along the direction.
Technically, squares n>1 are not neighbors… but this is a convenient calculation.
2025-05-23 18:34:19 -07:00
3684e9b425 [board, core, bitboard] Clean up casts between Rank, File and BitBoard
Let BitBoard::rank and BitBoard::file take a Rank and File directly, instead of a
u8 by reference. And then make the Rank/File::as_index const and return a value
rather than a reference.

All this allows you to convert between Rank, File, and BitBoard at compile tile
(i.e. as a const method) rather than needing to do runtime stuff.
2025-05-23 18:32:18 -07:00
588f049290 [position] Remove display module 2025-05-23 14:15:38 -07:00
5c5d9d5018 [board] Remove some dead code from Board 2025-05-23 14:14:49 -07:00
a9268ad194 [position] Add move tracking to Position
Create a new MoveRecord struct that tracks the move (aka ply) and irreversible
board properties. This should make it easier to unmake moves in the future.
2025-05-23 10:00:20 -07:00
05c62dcd99 [position] Remove CastleEvaluationError from this crate
It moved to board.
2025-05-23 09:58:29 -07:00