Axololly.Chess.MoveGenerator
1.0.0
See the version list below for details.
dotnet add package Axololly.Chess.MoveGenerator --version 1.0.0
NuGet\Install-Package Axololly.Chess.MoveGenerator -Version 1.0.0
<PackageReference Include="Axololly.Chess.MoveGenerator" Version="1.0.0" />
paket add Axololly.Chess.MoveGenerator --version 1.0.0
#r "nuget: Axololly.Chess.MoveGenerator, 1.0.0"
// Install Axololly.Chess.MoveGenerator as a Cake Addin #addin nuget:?package=Axololly.Chess.MoveGenerator&version=1.0.0 // Install Axololly.Chess.MoveGenerator as a Cake Tool #tool nuget:?package=Axololly.Chess.MoveGenerator&version=1.0.0
Chess Move Generator
If you can't beat em, join em. And that's what I did when I stayed inside for the last month every evening developing this chess move generator.
If I ever hate myself enough to make a Python version, I'll link it here.
Statistics
Quick section about the statistics of this project:
Source Code
File name | Lines | Characters | File size |
---|---|---|---|
Board.cs |
1,121 | 42,128 | 42.23 KB |
Board960.cs |
984 | 38,495 | 38.55 KB |
Utilities.cs |
232 | 7,451 | 7.50 KB |
Bitmasks.cs |
86 | 2,679 | 2.70 KB |
Castling.cs |
45 | 1,742 | 1.74 KB |
Castling960.cs |
97 | 3,656 | 3.66 KB |
Generator.cs |
561 | 19,372 | 19.46 KB |
MagicBitboards.cs |
318 | 17,169 | 17.08 KB |
Tables.cs |
59 | 1,851 | 1.86 KB |
Perft.cs |
373 | 12,147 | 12.23 KB |
Bitboard.cs |
119 | 3,768 | 3.79 KB |
CastlingRights.cs |
60 | 2,081 | 2.09 KB |
Using my project
Quick heads up: I used .NET 8.0
for this.
I have no idea where or how you publish a C# package, so I'll just list how you use the code here, if you ever build the code yourself.
Example Code
using Chess;
class Program
{
static void Main()
{
Board board = new();
var nextMoves = board.GenerateLegalMoves();
Console.WriteLine($"Board:\n{board}\n");
Console.WriteLine($"Next moves: [{string.Join(", ", nextMoves)}]");
}
}
Result
Board:
r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R N B Q K B N R
Next moves: [b1a3, b1c3, g1f3, g1h3, a2a3, b2b3, c2c3, d2d3, e2e3, f2f3, g2g3, h2h3, a2a4, b2b4, c2c4, d2d4, e2e4, f2f4, g2g4, h2h4]
Note about Chess960
Chess960 is the exact same, just change the using
directory to be Chess960
and use the Board960
class instead of the Board
class. The methods for controlling the state of the board are identical.
Move Generation Order
Rooks
- Using magic bitboards
- Includes the rook part of queen moves
Bishops
- Using magic bitboards
- includes the diagonal part of queen moves
Knights
Pawns
- Single pushes (with and without promotion)
- Double pushes
- Left attacks (with and without promotion)
- Right attacks (with and without promotion)
- En-passant (thanks to
@Disservin
forSmallbrain
and its EP code)
Normal King Moves
- Evading checks
- Not stepping on squares attacked by the opponent
- Not capturing pieces protected by the opponent
Castling
- Not supported if in check
- Not supported if pieces are blocking the path
- Not supported if pieces are attacking the path
- Not supported if already castled
All of how I programmed my move generation can be found in the conveniently named folder in this repository called Move Generation
.
At some point, I'll write up a bunch of documentation, well, documenting everything about this project in the Documentation
folder, which, as of writing this, is about two and a half, maybe three weeks behind schedule. Over the next few days, I'll update it with everything it's missing.
Perft Data
I translated TheRealGioviok's
raw text file of example FEN positions and their associated depths, and converted it into a JSON file 74,272 lines long. Substantially longer, but easier to manage.
Each document is structured like this:
{
"FEN": ...,
"Depths": {
"1": ...,
"2": ...,
"3": ...,
"4": ...,
"5": ...,
"6": ...
}
}
And in total, there are around 6,600 of these that you can use to verify that your engine returns the correct move counts.
If you want to perft test my own project, you can do it with the Perft
and Perft960
classes, which have the available features:
|Type|Method|Explanation|
|:-|:-|:-|
|Basic|.BasePerftTest()
|Returns the number of counted nodes at the end of the function.|
|Move-by-Move|.MoveByMovePerftTest()
|Prints the move and number of counted nodes, returning the total at the end.|
|Global|.GlobalPerftTest()
|Prints the number of counted nodes until a given depth is reached.|
|Output|.OutputPerftTest()
|Redirects the console output into a file for further review.|
All of these work with Chess960 boards - you just need to use Perft960
instead of Perft
.
Thanks
Honestly, thank you to everyone in the Stockfish Discord server, from Analog Hors and her magic bitboards article, all the way to Disservin, who I already mentioned above for his code relating to en-passant moves.
It means a lot to me, knowing I've finally completed a project that frustrated me back when I started in June. I learnt a new language, made new friends, and, most importantly, had a lot of fun with this.
So proud of how far I've come. Time to celebrate! 🎉
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.