15
15
#include " move_ordering.hpp"
16
16
namespace move_ordering
17
17
{
18
+ chess::Square getLeastValuable (chess::Bitboard attackers, const chess::Position &board, chess::Color side)
19
+ {
20
+ static const std::array<int , 6 > pieceOrder = {
21
+ (int )chess::PieceType::PAWN,
22
+ (int )chess::PieceType::KNIGHT,
23
+ (int )chess::PieceType::BISHOP,
24
+ (int )chess::PieceType::ROOK,
25
+ (int )chess::PieceType::QUEEN,
26
+ (int )chess::PieceType::KING
27
+ };
28
+
29
+ for (int pt : pieceOrder)
30
+ {
31
+ while (attackers)
32
+ {
33
+ chess::Square sq=attackers.pop ();
34
+ if (board.at (sq).type () == (chess::PieceType::underlying)pt &&
35
+ board.at (sq).color () == side)
36
+ return sq;
37
+ }
38
+ }
39
+
40
+ // fallback — if no attacker found (shouldn't happen)
41
+ return chess::Square::NO_SQ;
42
+ }
43
+ chess::Bitboard recomputeAttackers (chess::Square target, chess::Bitboard occupied, const chess::Board& board)
44
+ {
45
+ chess::Bitboard attackers = 0 ;
46
+
47
+ while (occupied)
48
+ {
49
+ auto sq=occupied.pop ();
50
+ const chess::Piece piece = board.at (sq);
51
+ if (piece == chess::Piece::NONE) continue ;
52
+
53
+ chess::Color c = piece.color ();
54
+ chess::PieceType pt = piece.type ();
55
+
56
+ switch (pt)
57
+ {
58
+ case (int )chess::PieceType::PAWN:
59
+ if (chess::attacks::pawn (c, sq).check (target.index ()))
60
+ attackers |= chess::Bitboard::fromSquare (sq);
61
+ break ;
62
+
63
+ case (int )chess::PieceType::KNIGHT:
64
+ if (chess::attacks::knight (sq).check (target.index ()))
65
+ attackers |= chess::Bitboard::fromSquare (sq);
66
+ break ;
67
+
68
+ case (int )chess::PieceType::BISHOP:
69
+ if (chess::attacks::bishop (sq, occupied).check (target.index ()))
70
+ attackers |= chess::Bitboard::fromSquare (sq);
71
+ break ;
72
+
73
+ case (int )chess::PieceType::ROOK:
74
+ if (chess::attacks::rook (sq, occupied).check (target.index ()))
75
+ attackers |= chess::Bitboard::fromSquare (sq);
76
+ break ;
77
+
78
+ case (int )chess::PieceType::QUEEN:
79
+ if (chess::attacks::queen (sq, occupied).check (target.index ()))
80
+ attackers |= chess::Bitboard::fromSquare (sq);
81
+ break ;
82
+
83
+ case (int )chess::PieceType::KING:
84
+ if (chess::attacks::king (sq).check (target.index ()))
85
+ attackers |= chess::Bitboard::fromSquare (sq);
86
+ break ;
87
+
88
+ default :
89
+ break ;
90
+ }
91
+ }
92
+
93
+ return attackers;
94
+ }
95
+ int see (const chess::Position &board, chess::Move move)
96
+ {
97
+ int to = move.to ().index ();
98
+ int gain[32 ], d = 0 ;
99
+ chess::Bitboard occupied = board.occ ();
100
+ chess::Color stm = board.sideToMove ();
101
+ gain[0 ] = piece_value (board.at (to).type ());
102
+ chess::Bitboard attackers = chess::attacks::attackers (board, ~stm, move.to ());
103
+
104
+ while (attackers)
105
+ {
106
+ chess::Square from = getLeastValuable (attackers, board, stm);
107
+ if (from == chess::Square::NO_SQ) break ; // safety check
108
+
109
+ gain[++d] = piece_value (board.at (from).type ()) - gain[d - 1 ];
110
+
111
+ if (std::max (-gain[d - 1 ], gain[d]) < 0 )
112
+ break ;
113
+
114
+ occupied ^= 1ULL << from.index ();
115
+ attackers = recomputeAttackers (to, occupied, board);
116
+ stm = ~stm;
117
+ }
118
+
119
+ for (int i = d - 1 ; i >= 0 ; --i)
120
+ {
121
+ gain[i] = -std::max (-gain[i], gain[i + 1 ]);
122
+ }
123
+
124
+ return gain[0 ];
125
+ }
18
126
19
127
void moveOrder (chess::Position &board, chess::Movelist &moves, int ply)
20
128
{
@@ -36,9 +144,9 @@ namespace move_ordering
36
144
// Capture scoring
37
145
if (board.isCapture (move))
38
146
{
39
- auto victim = board.at (move.to ()).type ();
40
- auto attacker = board.at (move.from ()).type ();
41
- score += SCORE_CAPTURE + mvvLva (victim, attacker );
147
+ // auto victim = board.at(move.to()).type();
148
+ // auto attacker = board.at(move.from()).type();
149
+ score += SCORE_CAPTURE + see (board, move );
42
150
}
43
151
44
152
// Counter move bonus (simple equality check)
@@ -58,7 +166,7 @@ namespace move_ordering
58
166
59
167
// Promotion bonus
60
168
if (move.typeOf () == chess::Move::PROMOTION)
61
- score += std::vector< int >{ 100 , 300 , 320 , 500 , 900 }[( int ) move.promotionType ()- 1 ] ;
169
+ score += piece_value ( move.promotionType ()) ;
62
170
63
171
// Set the score
64
172
move.setScore (score);
@@ -70,4 +178,4 @@ namespace move_ordering
70
178
return a.score () > b.score ();
71
179
});
72
180
}
73
- }
181
+ }
0 commit comments