From 63f5c61063ec39011072d21a1e45199c62cd247d Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 2 Apr 2025 05:35:12 +0200 Subject: [PATCH] semi-fixed marlinformat --- README.md | 65 ------------------------------------------------ source/datagen.h | 25 ++++++++++++++++++- source/utils.cpp | 47 ++++++++++++++++------------------ 3 files changed, 45 insertions(+), 92 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 57c3b17..0000000 --- a/README.md +++ /dev/null @@ -1,65 +0,0 @@ -![Eleanor](https://i.ibb.co/T1ZV9wN/91dea356-a725-45fd-b7a2-cd92afddcba4-1.jpg) -# Eleanor is a chess engine -## Features - -- Bitboard board representation -- Magic Bitboards -- Legal move generation -- Precalculated attack tables -- Storing moves in a 16 bit word -- Copy/make moving -- Simple evaluation (material and piece-square tables, MVV-LVA) -- Principal Variation Search -- Move ordering -- Time management -- UCI - -## Installation - -> Prerequisites: -> - A C++ compiler -> - Access to `make` command - - 1. Clone the repo to your PC - - 2. Run the command `make` in the root directory - > You can specify the compiler with the flag `CXX` - > and the executable name with `EXE`. - > By default they are `g++` and `Eleanor` respectively. - -3. Run the executable - -## How to use -There are some commands that you can use the engine with. - -- **`position`**: Set the current position of the board with [FEN](https://www.chess.com/terms/fen-chess). -`position [FEN]` or `position startpos` can be used. -- **`ucinewgame`** resets the position and game state -- **`quit`** terminates the program -- **`go`** is the command that asks what is the best move in the current position (search), -but we have to give it some parameters: -`wtime` `btime` `winc` `binc` -The first letter tells the color of player -wtime sets the remaining time of the white player -and winc sets the increments for the white player. -**It's important that these parameters are given in milliseconds!** -- **`stop`** stops the current search -- **`bench`** tests the search speed of the engine on a set of positions - -Also, the engine can be connected to any GUI software that has UCI support. - -## Sources I used -[Chess Programming Wiki](https://www.chessprogramming.org/Main_Page "Chess Programming Wiki") - -[Maksim Korzh's chess engine in C series](https://www.youtube.com/watch?v=QUNP-UjujBM&list=PLmN0neTso3Jxh8ZIylk74JpwfiWNI76Cs "Maksim Korzh's chess engine in C series") - -[Stockfish discord helping community](https://discord.com/invite/GWDRS3kU6R "Stockfish discord community") - -[Logo is generated by AI](https://deepai.org/ "Logo is generated by AI") - -
- -**The project is still in progress and developed constantly. Contributions are highly appreciated.** - -###### Last edited: 2025-01-23 - diff --git a/source/datagen.h b/source/datagen.h index 629115a..684f71e 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -1,6 +1,7 @@ #pragma once #include "board.h" #include "utils.h" +#include namespace DATAGEN { @@ -19,15 +20,37 @@ struct MarlinFormat { uint8_t extra; // 1 byte: Reserved for future use or additional data void packFrom(Board& board, int16_t eval, uint8_t wdl) { + const uint8_t stm = board.sideToMove == Black ? (1 << 7) : 0; + occupancy = board.occupied; pieces = UTILS::CompressPieces(board); - uint8_t stm_ep_square = (board.sideToMove<< 7) | (board.enPassantTarget & 0x3F); + stmEPSquare = stm | (board.enPassantTarget == noEPTarget ? 64 : board.enPassantTarget); + halfmoveClock = board.halfMoves; fullmoveNumber = board.fullMoves; this->eval = eval; this->wdl = wdl; extra = 0; } + + void writeToFile(const std::string& filename) const { + std::ofstream outFile(filename, std::ios::binary); + if (!outFile) { + std::cerr << "Error opening file for writing!" << std::endl; + return; + } + + outFile.write(reinterpret_cast(&occupancy), sizeof(occupancy)); + outFile.write(reinterpret_cast(&pieces), sizeof(pieces)); + outFile.write(reinterpret_cast(&stmEPSquare), sizeof(stmEPSquare)); + outFile.write(reinterpret_cast(&halfmoveClock), sizeof(halfmoveClock)); + outFile.write(reinterpret_cast(&fullmoveNumber), sizeof(fullmoveNumber)); + outFile.write(reinterpret_cast(&eval), sizeof(eval)); + outFile.write(reinterpret_cast(&wdl), sizeof(wdl)); + outFile.write(reinterpret_cast(&extra), sizeof(extra)); + + outFile.close(); + } }; struct ScoredMove { diff --git a/source/utils.cpp b/source/utils.cpp index aff6ff8..5de5d36 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -148,38 +148,33 @@ Move parseMove(Board &board, std::string_view str) { } std::array CompressPieces(Board &board) { - const uint8_t UNMOVED_ROOK = 0x08; + const uint8_t UNMOVED_ROOK = 6; std::array compressed{}; + Bitboard occupancy = board.occupied; + int index = 0; - for (int square = 0; square < 64; square++) { - int pieceCode = 0; + while (occupancy) { + int square = occupancy.getLS1BIndex(); + int pieceType = board.GetPieceType(square); + int pieceCode = pieceType; - for (int pieceType = Pawn; pieceType <= King; pieceType++) { - Bitboard pieceBitboard = board.pieces[pieceType]; - - if (pieceBitboard.IsSet(square)) { - if (board.colors[White] & board.pieces[pieceType]) { - pieceCode = pieceType; // White: 1-6 - } else { - pieceCode = pieceType + 6; // Black: 7-12 - } - - if (pieceType == Rook) { - if ((board.colors[White] & board.pieces[Rook]) && (board.castlingRights & (whiteKingRight | whiteQueenRight))) { - pieceCode |= UNMOVED_ROOK; - } - if ((board.colors[Black] & board.pieces[Rook]) && (board.castlingRights & (blackKingRight | blackQueenRight))) { - pieceCode |= UNMOVED_ROOK; - } - } - break; - } + if (pieceType == Rook) { + if ((square == a1 && (board.castlingRights & whiteQueenRight)) + || (square == h1 && (board.castlingRights & whiteKingRight)) + || (square == a8 && (board.castlingRights & blackQueenRight)) + || (square == h8 && (board.castlingRights & blackKingRight))) + pieceCode = UNMOVED_ROOK; } - int byteIndex = square / 4; - int shift = (square % 4) * 4; + pieceCode |= (board.sideToMove == Black ? (1 << 3) : 0); - compressed[byteIndex] |= (pieceCode & 0x0F) << shift; + int byteIndex = index / 2; + int bitShift = (index % 2) * 4; + + compressed[byteIndex] |= (pieceCode & 0x0F) << bitShift; + + index++; + occupancy.PopBit(square); } return compressed;