storing castling rights in 8 bits instead of 32
This commit is contained in:
1 parent
d3a26b29df
commit
4c217fa478
4 files changed
+26
-32
No files matched your search
+14
-24
@@ -6,7 +6,7 @@ void Board::Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Board::Reset() {
|
void Board::Reset() {
|
||||||
castlingRights = { false, false, false, false };
|
castlingRights = 0;
|
||||||
enPassantTarget = -1;
|
enPassantTarget = -1;
|
||||||
halfMoves = 0;
|
halfMoves = 0;
|
||||||
fullMoves = 0;
|
fullMoves = 0;
|
||||||
@@ -124,16 +124,16 @@ void Board::SetByFen(const char* fen) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'K':
|
case 'K':
|
||||||
castlingRights[White] = true;
|
castlingRights |= whiteKingRight;
|
||||||
break;
|
break;
|
||||||
case 'Q':
|
case 'Q':
|
||||||
castlingRights[White + 1] = true;
|
castlingRights |= whiteQueenRight;
|
||||||
break;
|
break;
|
||||||
case 'k':
|
case 'k':
|
||||||
castlingRights[Black * 2] = true;
|
castlingRights |= blackKingRight;
|
||||||
break;
|
break;
|
||||||
case 'q':
|
case 'q':
|
||||||
castlingRights[Black * 2 + 1] = true;
|
castlingRights |= blackQueenRight;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (*fen != '-') {
|
if (*fen != '-') {
|
||||||
@@ -229,10 +229,10 @@ void Board::PrintBoard() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::cout << " Castling rights: ";
|
std::cout << " Castling rights: ";
|
||||||
if (castlingRights[0]) std::cout << "K"; else std::cout << "-";
|
if (castlingRights & whiteKingRight) std::cout << "K"; else std::cout << "-";
|
||||||
if (castlingRights[1]) std::cout << "Q"; else std::cout << "-";
|
if (castlingRights & whiteQueenRight) std::cout << "Q"; else std::cout << "-";
|
||||||
if (castlingRights[2]) std::cout << "k"; else std::cout << "-";
|
if (castlingRights & blackKingRight) std::cout << "k"; else std::cout << "-";
|
||||||
if (castlingRights[3]) std::cout << "q"; else std::cout << "-";
|
if (castlingRights & blackQueenRight) std::cout << "q"; else std::cout << "-";
|
||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,18 +266,8 @@ int Board::GetPieceColor(int square) {
|
|||||||
return (colors[Black].IsSet(square));
|
return (colors[Black].IsSet(square));
|
||||||
}
|
}
|
||||||
|
|
||||||
U64 Board::GetAttackMaps(bool side) {
|
|
||||||
U64 combined = 0ULL;
|
|
||||||
for (int type = Pawn; type <= King; type++) {
|
|
||||||
combined |= attackMaps[side][type];
|
|
||||||
}
|
|
||||||
return combined;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Board::InCheck(bool side) {
|
bool Board::InCheck(bool side) {
|
||||||
Bitboard myKingSquare = colors[side] & pieces[King];
|
|
||||||
|
|
||||||
return GetAttackMaps(!side) & myKingSquare;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::SetPiece(int piece, int square, bool color) {
|
void Board::SetPiece(int piece, int square, bool color) {
|
||||||
@@ -299,13 +289,13 @@ static void UpdateCastlingRights(Board &board, int square, int type, int color)
|
|||||||
int kingSideRook = color ? h8 : h1;
|
int kingSideRook = color ? h8 : h1;
|
||||||
|
|
||||||
if (square == queenSideRook) {
|
if (square == queenSideRook) {
|
||||||
board.castlingRights[color * 2 + 1] = false;
|
board.castlingRights &= color ? blackQueenRight : whiteQueenRight;
|
||||||
} else if (square == kingSideRook) {
|
} else if (square == kingSideRook) {
|
||||||
board.castlingRights[color * 2] = false;
|
board.castlingRights &= color ? blackKingRight : whiteKingRight;
|
||||||
}
|
}
|
||||||
} else if (type == King) {
|
} else if (type == King) {
|
||||||
board.castlingRights[color * 2 + 1] = false;
|
board.castlingRights &= color ? blackQueenRight : whiteQueenRight;
|
||||||
board.castlingRights[color * 2] = false;
|
board.castlingRights &= color ? blackKingRight : whiteKingRight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -409,7 +399,7 @@ void Board::MakeMove(Move move) {
|
|||||||
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
|
UpdateCastlingRights(*this, move.MoveFrom(), attackerPiece, attackerColor);
|
||||||
|
|
||||||
halfMoves++;
|
halfMoves++;
|
||||||
if (halfMoves & 2 == 0) fullMoves++;
|
if (halfMoves % 2 == 0) fullMoves++;
|
||||||
sideToMove = !attackerColor;
|
sideToMove = !attackerColor;
|
||||||
enPassantTarget = newEpTarget;
|
enPassantTarget = newEpTarget;
|
||||||
}
|
}
|
||||||
+1
-4
@@ -3,7 +3,6 @@
|
|||||||
#include "bitboards.h"
|
#include "bitboards.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
#include "move.h"
|
#include "move.h"
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
class Board {
|
class Board {
|
||||||
public:
|
public:
|
||||||
@@ -13,7 +12,6 @@ public:
|
|||||||
|
|
||||||
std::array<Bitboard, 6> pieces;
|
std::array<Bitboard, 6> pieces;
|
||||||
std::array<Bitboard, 2> colors;
|
std::array<Bitboard, 2> colors;
|
||||||
U64 attackMaps[2][64];
|
|
||||||
|
|
||||||
Bitboard occupied;
|
Bitboard occupied;
|
||||||
|
|
||||||
@@ -24,7 +22,7 @@ public:
|
|||||||
int halfMoves = 0;
|
int halfMoves = 0;
|
||||||
int fullMoves = 0;
|
int fullMoves = 0;
|
||||||
|
|
||||||
std::array<bool, 4> castlingRights = { false, false, false, false };
|
uint8_t castlingRights = 0;
|
||||||
int enPassantTarget;
|
int enPassantTarget;
|
||||||
|
|
||||||
void Init();
|
void Init();
|
||||||
@@ -39,7 +37,6 @@ public:
|
|||||||
int GetPieceType(int square);
|
int GetPieceType(int square);
|
||||||
int GetPieceColor(int square);
|
int GetPieceColor(int square);
|
||||||
|
|
||||||
U64 GetAttackMaps(bool side);
|
|
||||||
bool InCheck(bool side);
|
bool InCheck(bool side);
|
||||||
|
|
||||||
void SetPiece(int piece, int square, bool color);
|
void SetPiece(int piece, int square, bool color);
|
||||||
|
|||||||
+3
-3
@@ -510,7 +510,7 @@ static void GenKingMoves(Board &board, bool color) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Checking for king side castle
|
// Checking for king side castle
|
||||||
if (board.castlingRights[color * 2]) {
|
if (board.castlingRights & (whiteKingRight | blackKingRight)) {
|
||||||
Bitboard mask = color ? 0xe000000000000000 : 0xe0;
|
Bitboard mask = color ? 0xe000000000000000 : 0xe0;
|
||||||
|
|
||||||
if ((board.occupied & mask).PopCount() == 1) {
|
if ((board.occupied & mask).PopCount() == 1) {
|
||||||
@@ -518,8 +518,8 @@ static void GenKingMoves(Board &board, bool color) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checking for quuen side castle
|
// Checking for queen side castle
|
||||||
if (board.castlingRights[color * 2 + 1]) {
|
if (board.castlingRights & (whiteQueenRight | blackQueenRight)) {
|
||||||
Bitboard mask = color ? 0xf00000000000000 : 0xf;
|
Bitboard mask = color ? 0xf00000000000000 : 0xf;
|
||||||
|
|
||||||
if ((board.occupied & mask).PopCount() == 1) {
|
if ((board.occupied & mask).PopCount() == 1) {
|
||||||
|
|||||||
+8
-1
@@ -20,6 +20,13 @@ enum PIECE {
|
|||||||
King
|
King
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum CASTLING_RIGHTS {
|
||||||
|
whiteKingRight = 1,
|
||||||
|
whiteQueenRight = 2,
|
||||||
|
blackKingRight = 4,
|
||||||
|
blackQueenRight = 8
|
||||||
|
};
|
||||||
|
|
||||||
constexpr std::array<const char, 12> PIECE_LETTERS = {
|
constexpr std::array<const char, 12> PIECE_LETTERS = {
|
||||||
'P', 'p',
|
'P', 'p',
|
||||||
'N', 'n',
|
'N', 'n',
|
||||||
@@ -87,4 +94,4 @@ enum DIRECTION {
|
|||||||
noEa = north + east,
|
noEa = north + east,
|
||||||
soWe = south + west,
|
soWe = south + west,
|
||||||
soEa = south + east
|
soEa = south + east
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user