diff options
| author | Adam <adammegarules1@gmail.com> | 2026-08-15 17:01:54 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-08-15 17:01:54 +0200 |
| commit | 4eb36e67eca586a662298bfe8cc5392fbfa698e0 (patch) | |
| tree | 6c232d59d1566377fbaeec035f4f799618f63421 /src/board | |
| parent | c1d6113da12bfbe92ae718d93dd56e25556e52cb (diff) | |
implementing memory optimazied move using uint16_t instead of a four integers and one byte
Diffstat (limited to 'src/board')
| -rw-r--r-- | src/board/board.cpp | 120 | ||||
| -rw-r--r-- | src/board/board.hpp | 18 |
2 files changed, 73 insertions, 65 deletions
diff --git a/src/board/board.cpp b/src/board/board.cpp index 3b1383a..a533b63 100644 --- a/src/board/board.cpp +++ b/src/board/board.cpp @@ -88,14 +88,18 @@ uint8_t getToValueFromMove(uint16_t move) { return (move >> 8) & 63; }; Position FindKing(Game *g, bool color) { return IndexToPosition(__builtin_ctzll(g->PieceBitboards[color][KING])); } -UndoMove MakeMove(Move move, Game *g) { +UndoMove MakeMove(uint16_t move, Game *g) { + uint8_t fromSquare = getFromValueFromMove(move); + uint8_t toSquare = getToValueFromMove(move); + PieceType promotion = getPromotionTypeFromMove(move); + UndoMove undo = {}; - undo.from = move.From; - undo.to = move.To; + undo.from = fromSquare; + undo.to = toSquare; - undo.movedPiece = g->pieces[PositionToIndex(move.From)]; - undo.capturedPiece = g->pieces[PositionToIndex(move.To)]; + undo.movedPiece = g->pieces[fromSquare]; + undo.capturedPiece = g->pieces[toSquare]; undo.oldTurn = g->turn; undo.oldCanEnpassant = g->canEnpassant; @@ -112,8 +116,8 @@ UndoMove MakeMove(Move move, Game *g) { undo.OldBlackCastleQueen = g->blackCastleQueen; // play move - Piece piece = g->pieces[PositionToIndex(move.From)]; - Piece piece2 = g->pieces[PositionToIndex(move.To)]; + Piece piece = g->pieces[fromSquare]; + Piece piece2 = g->pieces[toSquare]; if (piece2.type != NONEPIECE) { if (piece2.color == g->turn) { @@ -130,8 +134,9 @@ UndoMove MakeMove(Move move, Game *g) { } // Playing enpasstant - if (piece.type == PAWN && g->canEnpassant && move.To == g->enPassant) { - Position capturedPawn = move.To; + if (piece.type == PAWN && g->canEnpassant && + IndexToPosition(toSquare) == g->enPassant) { + Position capturedPawn = IndexToPosition(toSquare); capturedPawn.rank += piece.color ? -1 : 1; undo.wasEnPassantCapture = true; @@ -157,33 +162,37 @@ UndoMove MakeMove(Move move, Game *g) { // Adding enpassant if (piece.type == PAWN) { - Position to = move.To; - to.rank -= piece.color ? 2 : -2; - if (to == move.From) { + int to = toSquare; + // check if a pawn has moved two squares + to -= piece.color ? 2 * 8 : -2 * 8; + if (to == fromSquare) { g->canEnpassant = true; - Position target = move.From; - target.rank += piece.color ? 1 : -1; - g->enPassant = target; + int target = fromSquare; + target += piece.color ? 8 : -8; + g->enPassant = IndexToPosition(target); }; } - // Promotions - if (piece.type == PAWN && move.To.rank == (piece.color ? 7 : 0)) { - piece.type = move.promotion == NONEPIECE ? QUEEN : move.promotion; + // promotion is always set and should only be aplied when reached final rank + // (default: knight) + if (piece.type == PAWN && + IndexToPosition(toSquare).rank == (piece.color ? 7 : 0)) { + piece.type = promotion; } // caslte - if (piece.type == KING && move.From.rank == (piece.color ? 0 : 7) && - std::abs(move.To.file - move.From.file) == 2) { + if (piece.type == KING && + IndexToPosition(fromSquare).rank == (piece.color ? 0 : 7) && + std::abs(toSquare - fromSquare) == 2) { undo.wasCastle = true; - undo.CastledSide = move.To.file > move.From.file; + undo.CastledSide = toSquare > fromSquare; if (undo.CastledSide) { - Position rookFrom = move.From; + Position rookFrom = IndexToPosition(fromSquare); rookFrom.file = 7; - Position rookTo = move.From; + Position rookTo = IndexToPosition(toSquare); rookTo.file = 5; uint64_t from = 1ULL << PositionToIndex(rookFrom); @@ -203,10 +212,10 @@ UndoMove MakeMove(Move move, Game *g) { g->pieces[PositionToIndex(rookFrom)] = {false, NONEPIECE}; } else { // Queenside: a -> d - Position rookFrom = move.From; + Position rookFrom = IndexToPosition(fromSquare); rookFrom.file = 0; - Position rookTo = move.From; + Position rookTo = IndexToPosition(toSquare); rookTo.file = 3; uint64_t from = 1ULL << PositionToIndex(rookFrom); @@ -241,20 +250,21 @@ UndoMove MakeMove(Move move, Game *g) { } } + // if rook has moved if (piece.type == ROOK) { - if (piece.color) { // White - if (move.From.rank == 0 && move.From.file == 0) { // a1 + if (piece.color) { // White + if (fromSquare == 0) { // a1 g->whiteCastleQueen = false; } - if (move.From.rank == 0 && move.From.file == 7) { // h1 + if (fromSquare == 7) { // h1 g->whiteCastleKing = false; } - } else { // Black - if (move.From.rank == 7 && move.From.file == 0) { // a8 + } else { // Black + if (fromSquare == 7 * 8) { // a8 g->blackCastleQueen = false; } - if (move.From.rank == 7 && move.From.file == 7) { // h8 + if (fromSquare == 7 * 9) { // h8 g->blackCastleKing = false; } } @@ -262,24 +272,28 @@ UndoMove MakeMove(Move move, Game *g) { // this part is written with ai if (piece2.type == ROOK) { - if (piece2.color) { // White rook captured - if (move.To.rank == 0 && move.To.file == 0) // a1 + if (piece2.color) { // White rook captured + if (toSquare == 0) { // a1 g->whiteCastleQueen = false; - - if (move.To.rank == 0 && move.To.file == 7) // h1 + } + if (toSquare == 7) { // h1 g->whiteCastleKing = false; - } else { // Black rook captured - if (move.To.rank == 7 && move.To.file == 0) // a8 + } + } else { // Black rook captured + if (toSquare == 7 * 8) { // a8 g->blackCastleQueen = false; + } - if (move.To.rank == 7 && move.To.file == 7) // h8 + if (toSquare == 7 * 9) { + // h8 g->blackCastleKing = false; + } } } // playing the moves - uint64_t from = 1ULL << PositionToIndex(move.From); - uint64_t to = 1ULL << PositionToIndex(move.To); + uint64_t from = 1ULL << fromSquare; + uint64_t to = 1ULL << toSquare; // remove moving piece from source g->PieceBitboards[piece.color][undo.movedPiece.type] ^= from; @@ -297,14 +311,15 @@ UndoMove MakeMove(Move move, Game *g) { // add moving piece to destination g->PieceBitboards[piece.color][piece.type] ^= to; - if (piece.color) + if (piece.color) { g->WhitePieceBitboard ^= from | to; - else + } else { g->BlackPieceBitboard ^= from | to; + } g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard; - g->pieces[PositionToIndex(move.To)] = piece; - g->pieces[PositionToIndex(move.From)] = {false, NONEPIECE}; + g->pieces[toSquare] = piece; + g->pieces[fromSquare] = {.color = false, .type = NONEPIECE}; // changing who turn it is g->turn = !g->turn; @@ -326,9 +341,9 @@ UndoMove MakeMove(Move move, Game *g) { return undo; }; void UnMakeMove(UndoMove undo, Game *g) { - g->pieces[PositionToIndex(undo.from)] = undo.movedPiece; + g->pieces[undo.from] = undo.movedPiece; - g->pieces[PositionToIndex(undo.to)] = undo.capturedPiece; + g->pieces[undo.to] = undo.capturedPiece; if (undo.wasEnPassantCapture) { g->pieces[PositionToIndex(undo.enPassantCapturedSquare)] = @@ -353,10 +368,10 @@ void UnMakeMove(UndoMove undo, Game *g) { if (undo.wasCastle) { if (undo.CastledSide) { // f -> h - Position rookFrom = undo.from; + Position rookFrom = IndexToPosition(undo.from); rookFrom.file = 5; - Position rookTo = undo.from; + Position rookTo = IndexToPosition(undo.from); rookTo.file = 7; g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)]; @@ -366,14 +381,17 @@ void UnMakeMove(UndoMove undo, Game *g) { }; } else { // d -> a - Position rookFrom = undo.from; + Position rookFrom = IndexToPosition(undo.from); rookFrom.file = 3; - Position rookTo = undo.from; + Position rookTo = IndexToPosition(undo.from); rookTo.file = 0; g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)]; - g->pieces[PositionToIndex(rookFrom)] = {false, NONEPIECE}; + g->pieces[PositionToIndex(rookFrom)] = { + .color = false, + .type = NONEPIECE, + }; } } diff --git a/src/board/board.hpp b/src/board/board.hpp index b27a9d1..b24811c 100644 --- a/src/board/board.hpp +++ b/src/board/board.hpp @@ -43,23 +43,13 @@ PieceType getPromotionTypeFromMove(uint16_t move); uint8_t getFromValueFromMove(uint16_t move); uint8_t getToValueFromMove(uint16_t move); -struct Move { - Position From; - Position To; - - PieceType promotion = NONEPIECE; - bool operator==(const Move &other) const { - return From == other.From && To == other.To && promotion == other.promotion; - } -}; - enum Flag : std::uint8_t { EXACT, LOWERBOUND, UPPERBOUND }; struct TranspositionsEntry { int depth = -1; int Eval = 0; Flag flag = EXACT; - Move bestMove = {}; + uint16_t bestMove = {}; }; struct Game { Piece pieces[64]; @@ -85,8 +75,8 @@ struct UndoMove { Piece movedPiece; Piece capturedPiece; - Position from; - Position to; + uint8_t from; + uint8_t to; bool wasEnPassantCapture = false; Position enPassantCapturedSquare; @@ -112,7 +102,7 @@ struct UndoMove { }; int PositionToIndex(Position i); -UndoMove MakeMove(Move move, Game *g); +UndoMove MakeMove(uint16_t move, Game *g); void UnMakeMove(UndoMove undo, Game *g); Position FindKing(Game *g, bool color); |
