1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-29 16:23:09 +00:00

Introduce and use same_color_squares()

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2010-07-25 11:05:01 +01:00
parent a84e4b2049
commit 9b1d5bd534
4 changed files with 16 additions and 11 deletions

View file

@ -152,7 +152,7 @@ Value EvaluationFunction<KBNK>::apply(const Position& pos) const {
Square loserKSq = pos.king_square(weakerSide); Square loserKSq = pos.king_square(weakerSide);
Square bishopSquare = pos.piece_list(strongerSide, BISHOP, 0); Square bishopSquare = pos.piece_list(strongerSide, BISHOP, 0);
if (square_color(bishopSquare) == BLACK) if (same_color_squares(bishopSquare, SQ_A1))
{ {
winnerKSq = flop_square(winnerKSq); winnerKSq = flop_square(winnerKSq);
loserKSq = flop_square(loserKSq); loserKSq = flop_square(loserKSq);
@ -398,7 +398,7 @@ ScaleFactor ScalingFunction<KBPsK>::apply(const Position& pos) const {
Square queeningSq = relative_square(strongerSide, make_square(pawnFile, RANK_8)); Square queeningSq = relative_square(strongerSide, make_square(pawnFile, RANK_8));
Square kingSq = pos.king_square(weakerSide); Square kingSq = pos.king_square(weakerSide);
if ( square_color(queeningSq) != square_color(bishopSq) if ( !same_color_squares(queeningSq, bishopSq)
&& file_distance(square_file(kingSq), pawnFile) <= 1) && file_distance(square_file(kingSq), pawnFile) <= 1)
{ {
// The bishop has the wrong color, and the defending king is on the // The bishop has the wrong color, and the defending king is on the
@ -675,12 +675,12 @@ ScaleFactor ScalingFunction<KBPKB>::apply(const Position& pos) const {
// Case 1: Defending king blocks the pawn, and cannot be driven away // Case 1: Defending king blocks the pawn, and cannot be driven away
if ( square_file(weakerKingSq) == square_file(pawnSq) if ( square_file(weakerKingSq) == square_file(pawnSq)
&& relative_rank(strongerSide, pawnSq) < relative_rank(strongerSide, weakerKingSq) && relative_rank(strongerSide, pawnSq) < relative_rank(strongerSide, weakerKingSq)
&& ( square_color(weakerKingSq) != square_color(strongerBishopSq) && ( !same_color_squares(weakerKingSq, strongerBishopSq)
|| relative_rank(strongerSide, weakerKingSq) <= RANK_6)) || relative_rank(strongerSide, weakerKingSq) <= RANK_6))
return SCALE_FACTOR_ZERO; return SCALE_FACTOR_ZERO;
// Case 2: Opposite colored bishops // Case 2: Opposite colored bishops
if (square_color(strongerBishopSq) != square_color(weakerBishopSq)) if (!same_color_squares(strongerBishopSq, weakerBishopSq))
{ {
// We assume that the position is drawn in the following three situations: // We assume that the position is drawn in the following three situations:
// //
@ -724,7 +724,7 @@ ScaleFactor ScalingFunction<KBPPKB>::apply(const Position& pos) const {
Square wbsq = pos.piece_list(strongerSide, BISHOP, 0); Square wbsq = pos.piece_list(strongerSide, BISHOP, 0);
Square bbsq = pos.piece_list(weakerSide, BISHOP, 0); Square bbsq = pos.piece_list(weakerSide, BISHOP, 0);
if (square_color(wbsq) == square_color(bbsq)) if (same_color_squares(wbsq, bbsq))
// Not opposite-colored bishops, no scaling // Not opposite-colored bishops, no scaling
return SCALE_FACTOR_NONE; return SCALE_FACTOR_NONE;
@ -753,7 +753,7 @@ ScaleFactor ScalingFunction<KBPPKB>::apply(const Position& pos) const {
// some square in the frontmost pawn's path. // some square in the frontmost pawn's path.
if ( square_file(ksq) == square_file(blockSq1) if ( square_file(ksq) == square_file(blockSq1)
&& relative_rank(strongerSide, ksq) >= relative_rank(strongerSide, blockSq1) && relative_rank(strongerSide, ksq) >= relative_rank(strongerSide, blockSq1)
&& square_color(ksq) != square_color(wbsq)) && !same_color_squares(ksq, wbsq))
return SCALE_FACTOR_ZERO; return SCALE_FACTOR_ZERO;
else else
return SCALE_FACTOR_NONE; return SCALE_FACTOR_NONE;
@ -763,14 +763,14 @@ ScaleFactor ScalingFunction<KBPPKB>::apply(const Position& pos) const {
// in front of the frontmost pawn's path, and the square diagonally behind // in front of the frontmost pawn's path, and the square diagonally behind
// this square on the file of the other pawn. // this square on the file of the other pawn.
if ( ksq == blockSq1 if ( ksq == blockSq1
&& square_color(ksq) != square_color(wbsq) && !same_color_squares(ksq, wbsq)
&& ( bbsq == blockSq2 && ( bbsq == blockSq2
|| (pos.attacks_from<BISHOP>(blockSq2) & pos.pieces(BISHOP, weakerSide)) || (pos.attacks_from<BISHOP>(blockSq2) & pos.pieces(BISHOP, weakerSide))
|| rank_distance(r1, r2) >= 2)) || rank_distance(r1, r2) >= 2))
return SCALE_FACTOR_ZERO; return SCALE_FACTOR_ZERO;
else if ( ksq == blockSq2 else if ( ksq == blockSq2
&& square_color(ksq) != square_color(wbsq) && !same_color_squares(ksq, wbsq)
&& ( bbsq == blockSq1 && ( bbsq == blockSq1
|| (pos.attacks_from<BISHOP>(blockSq1) & pos.pieces(BISHOP, weakerSide)))) || (pos.attacks_from<BISHOP>(blockSq1) & pos.pieces(BISHOP, weakerSide))))
return SCALE_FACTOR_ZERO; return SCALE_FACTOR_ZERO;
@ -804,7 +804,7 @@ ScaleFactor ScalingFunction<KBPKN>::apply(const Position& pos) const {
if ( square_file(weakerKingSq) == square_file(pawnSq) if ( square_file(weakerKingSq) == square_file(pawnSq)
&& relative_rank(strongerSide, pawnSq) < relative_rank(strongerSide, weakerKingSq) && relative_rank(strongerSide, pawnSq) < relative_rank(strongerSide, weakerKingSq)
&& ( square_color(weakerKingSq) != square_color(strongerBishopSq) && ( !same_color_squares(weakerKingSq, strongerBishopSq)
|| relative_rank(strongerSide, weakerKingSq) <= RANK_6)) || relative_rank(strongerSide, weakerKingSq) <= RANK_6))
return SCALE_FACTOR_ZERO; return SCALE_FACTOR_ZERO;

View file

@ -400,7 +400,7 @@ void Position::print(Move move) const {
char c = (color_of_piece_on(sq) == BLACK ? '=' : ' '); char c = (color_of_piece_on(sq) == BLACK ? '=' : ' ');
Piece piece = piece_on(sq); Piece piece = piece_on(sq);
if (piece == EMPTY && square_color(sq) == BLACK) if (piece == EMPTY && same_color_squares(sq, SQ_A1))
piece = EMPTY_BLACK_SQ; piece = EMPTY_BLACK_SQ;
cout << c << pieceLetters.from_piece(piece) << c << '|'; cout << c << pieceLetters.from_piece(piece) << c << '|';

View file

@ -540,7 +540,7 @@ inline bool Position::opposite_colored_bishops() const {
return piece_count(WHITE, BISHOP) == 1 return piece_count(WHITE, BISHOP) == 1
&& piece_count(BLACK, BISHOP) == 1 && piece_count(BLACK, BISHOP) == 1
&& square_color(piece_list(WHITE, BISHOP, 0)) != square_color(piece_list(BLACK, BISHOP, 0)); && !same_color_squares(piece_list(WHITE, BISHOP, 0), piece_list(BLACK, BISHOP, 0));
} }
inline bool Position::has_pawn_on_7th(Color c) const { inline bool Position::has_pawn_on_7th(Color c) const {

View file

@ -139,6 +139,11 @@ inline Color square_color(Square s) {
return Color((int(square_file(s)) + int(square_rank(s))) & 1); return Color((int(square_file(s)) + int(square_rank(s))) & 1);
} }
inline bool same_color_squares(Square s1, Square s2) {
int s = int(s1) ^ int(s2);
return (((s >> 3) ^ s) & 1) == 0;
}
inline int file_distance(File f1, File f2) { inline int file_distance(File f1, File f2) {
return abs(int(f1) - int(f2)); return abs(int(f1) - int(f2));
} }