mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 00:33:09 +00:00
Shuffle put_piece() and friends signatures
It is more consistent with the others member functions. No functional change.
This commit is contained in:
parent
3184852bdc
commit
8f10f6c9cd
2 changed files with 44 additions and 47 deletions
|
@ -182,7 +182,7 @@ void Position::init() {
|
|||
Position& Position::operator=(const Position& pos) {
|
||||
|
||||
std::memcpy(this, &pos, sizeof(Position));
|
||||
startState = *st;
|
||||
std::memcpy(&startState, st, sizeof(StateInfo));
|
||||
st = &startState;
|
||||
nodes = 0;
|
||||
|
||||
|
@ -265,7 +265,7 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) {
|
|||
|
||||
else if ((idx = PieceToChar.find(token)) != string::npos)
|
||||
{
|
||||
put_piece(sq, color_of(Piece(idx)), type_of(Piece(idx)));
|
||||
put_piece(color_of(Piece(idx)), type_of(Piece(idx)), sq);
|
||||
++sq;
|
||||
}
|
||||
}
|
||||
|
@ -375,8 +375,8 @@ void Position::set_state(StateInfo* si) const {
|
|||
si->psq += psq[color_of(pc)][type_of(pc)][s];
|
||||
}
|
||||
|
||||
if (ep_square() != SQ_NONE)
|
||||
si->key ^= Zobrist::enpassant[file_of(ep_square())];
|
||||
if (si->epSquare != SQ_NONE)
|
||||
si->key ^= Zobrist::enpassant[file_of(si->epSquare)];
|
||||
|
||||
if (sideToMove == BLACK)
|
||||
si->key ^= Zobrist::side;
|
||||
|
@ -498,7 +498,7 @@ Bitboard Position::attackers_to(Square s, Bitboard occupied) const {
|
|||
return (attacks_from<PAWN>(s, BLACK) & pieces(WHITE, PAWN))
|
||||
| (attacks_from<PAWN>(s, WHITE) & pieces(BLACK, PAWN))
|
||||
| (attacks_from<KNIGHT>(s) & pieces(KNIGHT))
|
||||
| (attacks_bb<ROOK>(s, occupied) & pieces(ROOK, QUEEN))
|
||||
| (attacks_bb<ROOK >(s, occupied) & pieces(ROOK, QUEEN))
|
||||
| (attacks_bb<BISHOP>(s, occupied) & pieces(BISHOP, QUEEN))
|
||||
| (attacks_from<KING>(s) & pieces(KING));
|
||||
}
|
||||
|
@ -566,7 +566,7 @@ bool Position::pseudo_legal(const Move m) const {
|
|||
return MoveList<LEGAL>(*this).contains(m);
|
||||
|
||||
// Is not a promotion, so promotion piece must be empty
|
||||
if (promotion_type(m) - 2 != NO_PIECE_TYPE)
|
||||
if (promotion_type(m) - KNIGHT != NO_PIECE_TYPE)
|
||||
return false;
|
||||
|
||||
// If the 'from' square is not occupied by a piece belonging to the side to
|
||||
|
@ -587,9 +587,7 @@ bool Position::pseudo_legal(const Move m) const {
|
|||
return false;
|
||||
|
||||
if ( !(attacks_from<PAWN>(from, us) & pieces(~us) & to) // Not a capture
|
||||
|
||||
&& !((from + pawn_push(us) == to) && empty(to)) // Not a single push
|
||||
|
||||
&& !( (from + 2 * pawn_push(us) == to) // Not a double push
|
||||
&& (rank_of(from) == relative_rank(us, RANK_2))
|
||||
&& empty(to)
|
||||
|
@ -634,10 +632,9 @@ bool Position::gives_check(Move m, const CheckInfo& ci) const {
|
|||
|
||||
Square from = from_sq(m);
|
||||
Square to = to_sq(m);
|
||||
PieceType pt = type_of(piece_on(from));
|
||||
|
||||
// Is there a direct check?
|
||||
if (ci.checkSq[pt] & to)
|
||||
if (ci.checkSq[type_of(piece_on(from))] & to)
|
||||
return true;
|
||||
|
||||
// Is there a discovered check?
|
||||
|
@ -730,7 +727,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool gives
|
|||
assert(pt == KING);
|
||||
|
||||
Square rfrom, rto;
|
||||
do_castling<true>(from, to, rfrom, rto);
|
||||
do_castling<true>(us, from, to, rfrom, rto);
|
||||
|
||||
captured = NO_PIECE_TYPE;
|
||||
st->psq += psq[us][ROOK][rto] - psq[us][ROOK][rfrom];
|
||||
|
@ -764,7 +761,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool gives
|
|||
st->nonPawnMaterial[them] -= PieceValue[MG][captured];
|
||||
|
||||
// Update board and piece lists
|
||||
remove_piece(capsq, them, captured);
|
||||
remove_piece(them, captured, capsq);
|
||||
|
||||
// Update material hash key and prefetch access to materialTable
|
||||
k ^= Zobrist::psq[them][captured][capsq];
|
||||
|
@ -798,7 +795,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool gives
|
|||
|
||||
// Move the piece. The tricky Chess960 castling is handled earlier
|
||||
if (type_of(m) != CASTLING)
|
||||
move_piece(from, to, us, pt);
|
||||
move_piece(us, pt, from, to);
|
||||
|
||||
// If the moving piece is a pawn do some special extra work
|
||||
if (pt == PAWN)
|
||||
|
@ -818,8 +815,8 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool gives
|
|||
assert(relative_rank(us, to) == RANK_8);
|
||||
assert(promotion >= KNIGHT && promotion <= QUEEN);
|
||||
|
||||
remove_piece(to, us, PAWN);
|
||||
put_piece(to, us, promotion);
|
||||
remove_piece(us, PAWN, to);
|
||||
put_piece(us, promotion, to);
|
||||
|
||||
// Update hash keys
|
||||
k ^= Zobrist::psq[us][PAWN][to] ^ Zobrist::psq[us][promotion][to];
|
||||
|
@ -907,19 +904,19 @@ void Position::undo_move(Move m) {
|
|||
assert(pt == promotion_type(m));
|
||||
assert(pt >= KNIGHT && pt <= QUEEN);
|
||||
|
||||
remove_piece(to, us, pt);
|
||||
put_piece(to, us, PAWN);
|
||||
remove_piece(us, pt, to);
|
||||
put_piece(us, PAWN, to);
|
||||
pt = PAWN;
|
||||
}
|
||||
|
||||
if (type_of(m) == CASTLING)
|
||||
{
|
||||
Square rfrom, rto;
|
||||
do_castling<false>(from, to, rfrom, rto);
|
||||
do_castling<false>(us, from, to, rfrom, rto);
|
||||
}
|
||||
else
|
||||
{
|
||||
move_piece(to, from, us, pt); // Put the piece back at the source square
|
||||
move_piece(us, pt, to, from); // Put the piece back at the source square
|
||||
|
||||
if (st->capturedType)
|
||||
{
|
||||
|
@ -936,7 +933,7 @@ void Position::undo_move(Move m) {
|
|||
assert(st->capturedType == PAWN);
|
||||
}
|
||||
|
||||
put_piece(capsq, ~us, st->capturedType); // Restore the captured piece
|
||||
put_piece(~us, st->capturedType, capsq); // Restore the captured piece
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -951,19 +948,19 @@ void Position::undo_move(Move m) {
|
|||
/// Position::do_castling() is a helper used to do/undo a castling move. This
|
||||
/// is a bit tricky, especially in Chess960.
|
||||
template<bool Do>
|
||||
void Position::do_castling(Square from, Square& to, Square& rfrom, Square& rto) {
|
||||
void Position::do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto) {
|
||||
|
||||
bool kingSide = to > from;
|
||||
rfrom = to; // Castling is encoded as "king captures friendly rook"
|
||||
rto = relative_square(sideToMove, kingSide ? SQ_F1 : SQ_D1);
|
||||
to = relative_square(sideToMove, kingSide ? SQ_G1 : SQ_C1);
|
||||
rto = relative_square(us, kingSide ? SQ_F1 : SQ_D1);
|
||||
to = relative_square(us, kingSide ? SQ_G1 : SQ_C1);
|
||||
|
||||
// Remove both pieces first since squares could overlap in Chess960
|
||||
remove_piece(Do ? from : to, sideToMove, KING);
|
||||
remove_piece(Do ? rfrom : rto, sideToMove, ROOK);
|
||||
remove_piece(us, KING, Do ? from : to);
|
||||
remove_piece(us, ROOK, Do ? rfrom : rto);
|
||||
board[Do ? from : to] = board[Do ? rfrom : rto] = NO_PIECE; // Since remove_piece doesn't do it for us
|
||||
put_piece(Do ? to : from, sideToMove, KING);
|
||||
put_piece(Do ? rto : rfrom, sideToMove, ROOK);
|
||||
put_piece(us, KING, Do ? to : from);
|
||||
put_piece(us, ROOK, Do ? rto : rfrom);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -180,11 +180,11 @@ private:
|
|||
|
||||
// Other helpers
|
||||
Bitboard check_blockers(Color c, Color kingColor) const;
|
||||
void put_piece(Square s, Color c, PieceType pt);
|
||||
void remove_piece(Square s, Color c, PieceType pt);
|
||||
void move_piece(Square from, Square to, Color c, PieceType pt);
|
||||
void put_piece(Color c, PieceType pt, Square s);
|
||||
void remove_piece(Color c, PieceType pt, Square s);
|
||||
void move_piece(Color c, PieceType pt, Square from, Square to);
|
||||
template<bool Do>
|
||||
void do_castling(Square from, Square& to, Square& rfrom, Square& rto);
|
||||
void do_castling(Color us, Square from, Square& to, Square& rfrom, Square& rto);
|
||||
|
||||
// Data members
|
||||
Piece board[SQUARE_NB];
|
||||
|
@ -389,7 +389,7 @@ inline Thread* Position::this_thread() const {
|
|||
return thisThread;
|
||||
}
|
||||
|
||||
inline void Position::put_piece(Square s, Color c, PieceType pt) {
|
||||
inline void Position::put_piece(Color c, PieceType pt, Square s) {
|
||||
|
||||
board[s] = make_piece(c, pt);
|
||||
byTypeBB[ALL_PIECES] |= s;
|
||||
|
@ -400,21 +400,7 @@ inline void Position::put_piece(Square s, Color c, PieceType pt) {
|
|||
pieceCount[c][ALL_PIECES]++;
|
||||
}
|
||||
|
||||
inline void Position::move_piece(Square from, Square to, Color c, PieceType pt) {
|
||||
|
||||
// index[from] is not updated and becomes stale. This works as long as index[]
|
||||
// is accessed just by known occupied squares.
|
||||
Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
|
||||
byTypeBB[ALL_PIECES] ^= from_to_bb;
|
||||
byTypeBB[pt] ^= from_to_bb;
|
||||
byColorBB[c] ^= from_to_bb;
|
||||
board[from] = NO_PIECE;
|
||||
board[to] = make_piece(c, pt);
|
||||
index[to] = index[from];
|
||||
pieceList[c][pt][index[to]] = to;
|
||||
}
|
||||
|
||||
inline void Position::remove_piece(Square s, Color c, PieceType pt) {
|
||||
inline void Position::remove_piece(Color c, PieceType pt, Square s) {
|
||||
|
||||
// WARNING: This is not a reversible operation. If we remove a piece in
|
||||
// do_move() and then replace it in undo_move() we will put it at the end of
|
||||
|
@ -431,4 +417,18 @@ inline void Position::remove_piece(Square s, Color c, PieceType pt) {
|
|||
pieceCount[c][ALL_PIECES]--;
|
||||
}
|
||||
|
||||
inline void Position::move_piece(Color c, PieceType pt, Square from, Square to) {
|
||||
|
||||
// index[from] is not updated and becomes stale. This works as long as index[]
|
||||
// is accessed just by known occupied squares.
|
||||
Bitboard from_to_bb = SquareBB[from] ^ SquareBB[to];
|
||||
byTypeBB[ALL_PIECES] ^= from_to_bb;
|
||||
byTypeBB[pt] ^= from_to_bb;
|
||||
byColorBB[c] ^= from_to_bb;
|
||||
board[from] = NO_PIECE;
|
||||
board[to] = make_piece(c, pt);
|
||||
index[to] = index[from];
|
||||
pieceList[c][pt][index[to]] = to;
|
||||
}
|
||||
|
||||
#endif // #ifndef POSITION_H_INCLUDED
|
||||
|
|
Loading…
Add table
Reference in a new issue