bb: Update position.cpp/.h

This commit is contained in:
Calcitem 2020-12-19 10:48:23 +08:00
parent 777a91520e
commit b886da6c96
3 changed files with 46 additions and 48 deletions

View File

@ -728,6 +728,7 @@ bool Position::reset()
memset(board, 0, sizeof(board));
st.key = 0;
memset(byTypeBB, 0, sizeof(byTypeBB));
memset(byColorBB, 0, sizeof(byColorBB));
pieceCountOnBoard[BLACK] = pieceCountOnBoard[WHITE] = 0;
pieceCountInHand[BLACK] = pieceCountInHand[WHITE] = rule.nTotalPiecesEachSide;
@ -784,8 +785,6 @@ bool Position::put_piece(Square s, bool updateCmdlist)
Piece piece = NO_PIECE;
int us = sideToMove;
Bitboard fromTo;
if (phase == PHASE_GAMEOVER ||
action != ACTION_PLACE ||
!onBoard[s] || board[s]) {
@ -801,13 +800,12 @@ bool Position::put_piece(Square s, bool updateCmdlist)
pieceCountInHand[us]--;
pieceCountOnBoard[us]++;
board[s]= piece;
Piece pc = board[s] = piece;
byTypeBB[ALL_PIECES] |= byTypeBB[type_of(pc)] |= s;
byColorBB[color_of(pc)] |= s; // TODO: Put ban?
update_key(s);
byTypeBB[ALL_PIECES] |= s;
byTypeBB[us] |= s;
if (updateCmdlist) {
sprintf(cmdline, "(%1u,%1u)", file_of(s), rank_of(s));
}
@ -876,11 +874,12 @@ bool Position::put_piece(Square s, bool updateCmdlist)
st.rule50++;
}
fromTo = square_bb(currentSquare) | square_bb(s);
byTypeBB[ALL_PIECES] ^= fromTo;
byTypeBB[us] ^= fromTo;
Piece pc = board[s] = board[currentSquare];
board[s] = board[currentSquare];
Bitboard fromTo = currentSquare | s;
byTypeBB[ALL_PIECES] ^= fromTo;
byTypeBB[type_of(pc)] ^= fromTo;
byColorBB[color_of(pc)] ^= fromTo;
update_key(s);
revert_key(currentSquare);
@ -934,14 +933,20 @@ bool Position::remove_piece(Square s, bool updateCmdlist)
revert_key(s);
if (rule.hasBannedLocations && phase == PHASE_PLACING) {
board[s]= BAN_STONE;
// Remove and put ban
Piece pc = board[s];
byTypeBB[type_of(pc)] ^= s;
byColorBB[color_of(pc)] ^= s;
pc = board[s] = BAN_STONE;
update_key(s);
byTypeBB[them] ^= s;
byTypeBB[BAN] |= s;
} else { // Remove
board[s]= NO_PIECE;
byTypeBB[type_of(pc)] |= s;
//byColorBB[color_of(pc)] |= s;
} else {
// Remove only
Piece pc = board[s] = NO_PIECE;
byTypeBB[ALL_PIECES] ^= s;
byTypeBB[them] ^= s;
byTypeBB[type_of(pc)] ^= s;
byColorBB[color_of(pc)] ^= s;
}
if (updateCmdlist) {
@ -1176,10 +1181,12 @@ void Position::remove_ban_stones()
for (int r = 0; r < RANK_NB; r++) {
s = static_cast<Square>(f * RANK_NB + r);
if (board[s] == BAN_STONE) {
revert_key(s);
if (board[s] == BAN_STONE) {
Piece pc = board[s];
byTypeBB[ALL_PIECES] ^= s;
byTypeBB[type_of(pc)] ^= s;
board[s] = NO_PIECE;
byTypeBB[ALL_PIECES] ^= s; // Need to remove?
revert_key(s);
}
}
}
@ -1250,6 +1257,7 @@ Position &Position::operator= (const Position &other)
memcpy(this->board, other.board, sizeof(this->board));
memcpy(this->byTypeBB, other.byTypeBB, sizeof(this->byTypeBB));
memcpy(this->byColorBB, other.byColorBB, sizeof(this->byColorBB));
memcpy(&millList, &other.millList, sizeof(millList));
millListSize = other.millListSize;

View File

@ -172,6 +172,7 @@ public:
// Data members
Piece board[SQUARE_NB];
Bitboard byTypeBB[PIECE_TYPE_NB];
Bitboard byColorBB[COLOR_NB];
// TODO: [0] is sum of Black and White
int pieceCountInHand[COLOR_NB]{ 0, 12, 12 }; // TODO
int pieceCountOnBoard[COLOR_NB]{ 0, 0, 0 };
@ -299,9 +300,8 @@ inline bool Position::select_piece(File f, Rank r)
inline void Position::put_piece(Piece pc, Square s)
{
board[s] = pc;
byTypeBB[ALL_PIECES] |= s;
byTypeBB[type_of(pc)] |= s;
//byColorBB[color_of(pc)] |= s;
byTypeBB[ALL_PIECES] |= byTypeBB[type_of(pc)] |= s;
byColorBB[color_of(pc)] |= s;
//index[s] = pieceCount[pc]++;
//pieceList[pc][index[s]] = s;
//pieceCount[make_piece(color_of(pc), ALL_PIECES)]++;
@ -341,22 +341,10 @@ inline bool Position::undo_move_piece(Square from, Square to)
inline bool Position::move_piece(Square from, Square to)
{
#if 0
// index[from] is not updated and becomes stale. This works as long as index[]
// is accessed just by known occupied squares.
Piece pc = board[from];
Bitboard fromTo = from | to;
byTypeBB[ALL_PIECES] ^= fromTo;
byTypeBB[type_of(pc)] ^= fromTo;
byColorBB[color_of(pc)] ^= fromTo;
board[from] = NO_PIECE;
board[to] = pc;
index[to] = index[from];
pieceList[pc][index[to]] = to;
#endif
if (select_piece(from)) {
return put_piece(to);
if (put_piece(to)) {
return true;
}
}
return false;

View File

@ -247,8 +247,6 @@ enum PieceType : uint16_t
ALL_PIECES = 0,
PIECE_TYPE_NB = 4,
STONE = 5,
IN_HAND = 0x10,
ON_BOARD = 0x20,
};
@ -429,22 +427,26 @@ constexpr Piece make_piece(Color c, PieceType pt)
return NO_PIECE;
}
inline Color color_of(Piece pc)
{
return Color(pc >> 4);
}
constexpr PieceType type_of(Piece pc)
{
if (pc & 0x30) {
//return STONE; // TODO
}
if (pc == BAN_STONE) {
return BAN;
}
return NO_PIECE_TYPE;
}
if (color_of(pc) == BLACK) {
return BLACK_STONE;
}
inline Color color_of(Piece pc)
{
return Color(pc >> 4);
if (color_of(pc) == WHITE) {
return WHITE_STONE;
}
return NO_PIECE_TYPE;
}
constexpr bool is_ok(Square s)