diff --git a/source/datagen.cpp b/source/datagen.cpp index 9af39ce..373dc89 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -19,16 +19,34 @@ static void PlayRandMoves(Board &board) { bool isLegal = board.MakeMove(board.moveList[dist(rng)]); if (!isLegal) count--; } + + MOVEGEN::GenerateMoves(board); } static bool IsGameOver(Board &board) { - // TODO + bool isDraw = SEARCH::IsDraw(board); + if (isDraw) return true; + + int moveSeen = 0; + + for (int i = 0; i < board.currentMoveIndex; i++) { + Board copy = board; + bool isLegal = copy.MakeMove(board.moveList[i]); + + if (!isLegal) continue; + positionIndex--; + moveSeen++; + } + + if (moveSeen == 0) return true; + return false; } void Run(int games) { for (int i = 0; i < games; i++) { + Game game; Board board; - MarlinFormat position; + uint8_t wdl = 1; positionIndex = 0; std::memset(SEARCH::killerMoves, 0, sizeof(SEARCH::killerMoves)); @@ -36,25 +54,28 @@ void Run(int games) { PlayRandMoves(board); + int staticEval = HCE::Evaluate(board); + SEARCH::SearchResults safeResults; - - // while (!IsGameOver(board)) { - // SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams()); - // if (!results.bestMove) break; - // safeResults = results; + MOVEGEN::GenerateMoves(board); - // board.MakeMove(results.bestMove); - // } - - if (std::abs(safeResults.score) >= SEARCH::inf - SEARCH::MAX_DEPTH) { + while (!IsGameOver(board)) { + SEARCH::SearchResults results = SEARCH::SearchPosition(board, SearchParams()); + + if (!results.bestMove) break; + safeResults = results; + + game.moves.push_back(ScoredMove(results.bestMove, results.score)); + board.MakeMove(results.bestMove); + MOVEGEN::GenerateMoves(board); + } + + if (std::abs(safeResults.score) >= std::abs(SEARCH::MATE_SCORE) - SEARCH::MAX_PLY) { wdl = board.sideToMove ? 2 : 0; } - position.packFrom(board, HCE::Evaluate(board), wdl); - board.PrintBoard(); - std::cout << "Eval: " << position.eval << std::endl; - std::cout << "WDL: " << int(position.wdl) << std::endl; + game.format.packFrom(board, staticEval, wdl); } } } \ No newline at end of file diff --git a/source/datagen.h b/source/datagen.h index 03a5b18..41eb5fd 100644 --- a/source/datagen.h +++ b/source/datagen.h @@ -44,6 +44,8 @@ struct Game { MarlinFormat format; std::vector moves; + Game(){}; + Game(MarlinFormat f, std::vector m) { format = f; moves = m; diff --git a/source/search.cpp b/source/search.cpp index a42e8a2..9ab0558 100644 --- a/source/search.cpp +++ b/source/search.cpp @@ -323,6 +323,9 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { score = -PVS(copy, newDepth, -beta, -alpha, ply + 1).score; } + moveSeen++; + positionIndex--; + if (searchStopped) return 0; if (currMove.IsQuiet()) { @@ -330,9 +333,6 @@ SearchResults PVS(Board board, int depth, int alpha, int beta, int ply) { seenQuietsCount++; } - moveSeen++; - positionIndex--; - // Fail high (beta cutoff) if (score >= beta) { if (!currMove.IsCapture()) { diff --git a/source/utils.cpp b/source/utils.cpp index 0dc7d10..a2ca6cb 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -148,11 +148,10 @@ Move parseMove(Board &board, std::string_view str) { } std::array CompressPieces(Board &board) { - const uint8_t UNMOVED_ROOK = 0x10; // Special value for unmoved rooks + const uint8_t UNMOVED_ROOK = 0x08; // We only have 4 bits, so we use bit 3 to mark unmoved rooks + std::array compressed{}; // Zero-initialize - std::array compressed; - - for (int square = a1; square <= h8; square++) { + for (int square = 0; square < 64; square++) { int pieceCode = 0; for (int pieceType = Pawn; pieceType <= King; pieceType++) { @@ -160,38 +159,25 @@ std::array CompressPieces(Board &board) { if (pieceBitboard.IsSet(square)) { if (board.colors[White] & board.pieces[pieceType]) { - pieceCode = pieceType + 1; // White pieces: 1-6 + pieceCode = pieceType; // White: 1-6 } else { - pieceCode = pieceType + 7; // Black pieces: 7-12 + pieceCode = pieceType + 6; // Black: 7-12 } - // If it's a rook and hasn't moved (check castling rights) + // Check for unmoved rook if (pieceType == Rook) { - if (board.sideToMove == White) { - // If the white rooks are still eligible for castling, they haven't moved - if ((board.castlingRights & (whiteKingRight | whiteQueenRight)) != 0) { - pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook - } - } else if (board.sideToMove == Black) { - // If the black rooks are still eligible for castling, they haven't moved - if ((board.castlingRights & (blackKingRight | blackQueenRight)) != 0) { - pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook - } + if ((board.castlingRights & (whiteKingRight | whiteQueenRight | blackKingRight | blackQueenRight)) != 0) { + pieceCode |= UNMOVED_ROOK; // Mark as unmoved rook (bit 3 set) } } break; } } - // Determine the byte index for storing the piece code (since we are packing 2 pieces per byte) - int byteIndex = square / 2; + int byteIndex = square / 4; + int shift = (square % 4) * 4; - // Pack the piece code into the byte array - if (square % 2 == 0) { - compressed[byteIndex] = (compressed[byteIndex] & 0xF0) | (pieceCode & 0x0F); - } else { - compressed[byteIndex] = (compressed[byteIndex] & 0x0F) | ((pieceCode & 0x0F) << 4); - } + compressed[byteIndex] |= (pieceCode & 0x0F) << shift; // Pack 4-bit pieceCode } return compressed;