Skip to content

Commit bb7dea0

Browse files
committed
1. rewritten evaluation (missing PSQT here)
2. Enhanced search, move ordering and time management
1 parent a6556a8 commit bb7dea0

File tree

9 files changed

+1699
-723
lines changed

9 files changed

+1699
-723
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
*
33

44
# But not these directories and files
5-
!*/
65
!*.cpp
76
!*.hpp
87
!*.h

eval.cpp

Lines changed: 479 additions & 633 deletions
Large diffs are not rendered by default.

eval.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,5 +218,10 @@ namespace chess
218218
#define MAX_SCORE_CP 31000
219219
#define MATE(i) MAX_MATE-i
220220
#define MATE_DISTANCE(i) (i - MAX_MATE)
221-
int eval(chess::Position &board);
222-
constexpr int16_t ASPIRATION_DELTA = 30;
221+
int16_t eval(const chess::Position &board);
222+
inline int16_t eval(chess::Position &board) {
223+
return eval(static_cast<const chess::Position&>(board));
224+
}
225+
constexpr int16_t ASPIRATION_DELTA = 30;
226+
int16_t piece_value(chess::PieceType p);
227+
int16_t passed(const chess::Position &pos);

main.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
using namespace std;
1616

1717
// Constants
18-
#define MAX_SCORE_CP 31000
19-
#define MATE(ply) (32000 - (ply))
2018
#define IS_MATE_SCORE(score) (abs(score) >= MAX_SCORE_CP)
21-
#define MATE_DISTANCE(score) (MATE(0) - abs(score))
2219
constexpr unsigned INFINITE_TIME = std::numeric_limits<unsigned>::max();
23-
constexpr unsigned DEFAULT_TIME_LIMIT = 5000;
2420

2521
// Thread handle
2622
static std::thread search_thread;
@@ -32,7 +28,7 @@ void run_search(chess::Position board, int depth, unsigned time_limit_ms, bool i
3228

3329
search::tt.newSearch();
3430
search::start_time = std::chrono::high_resolution_clock::now();
35-
search::time_limit = std::chrono::milliseconds((int)(time_limit_ms*0.2));
31+
search::time_limit = std::chrono::milliseconds((int)(time_limit_ms*0.01));
3632
search::PV stablePV;
3733

3834
clock_t t1 = clock();
@@ -58,7 +54,7 @@ void run_search(chess::Position board, int depth, unsigned time_limit_ms, bool i
5854
score = search::alphaBeta(board, alpha, beta, d);
5955
}
6056

61-
if (score > prev_score)
57+
if (search::pv.cmove>0)
6258
{
6359
memcpy(&stablePV, &search::pv, sizeof(search::PV));
6460
}
@@ -73,7 +69,7 @@ void run_search(chess::Position board, int depth, unsigned time_limit_ms, bool i
7369
if (!IS_MATE_SCORE(score))
7470
cout << " cp " << score;
7571
else
76-
cout << " mate " << (score < 0 ? "-" : "") << ((MATE_DISTANCE(score) + 1) / 2);
72+
cout << " mate " << ((MATE_DISTANCE(score) + 1) / 2);
7773
cout << " time " << int(elapsed_ms)
7874
<< " nodes " << search::nodes
7975
<< " nps " << int(search::nodes / max(elapsed_ms / 1000.0, 1e-3))
@@ -202,11 +198,14 @@ int main()
202198
}
203199

204200
search::stop_requested = false;
205-
unsigned time_limit_ms = infinite ? INFINITE_TIME : (movetime >= 0 ? movetime : DEFAULT_TIME_LIMIT);
201+
unsigned time_limit_ms = INFINITE_TIME;
202+
if (!infinite&&movetime>=0){
203+
time_limit_ms=movetime;
204+
}
206205
chess::Position board_copy = board;
207206

208207
search_thread = std::thread(run_search, board_copy, depth, time_limit_ms, infinite);
209-
//search_thread.detach();
208+
search_thread.detach();
210209
}
211210
else if (token == "d")
212211
{
@@ -219,9 +218,6 @@ int main()
219218

220219
board.setFen("rnb1kbnr/pppppppp/8/8/8/8/PPPPPPP1/RNBQKBNR w KQkq - 0 1");
221220
cout << "White up a queen: " << eval(board) << endl;
222-
223-
board.setFen("7k/8/8/8/8/8/8/7R b - - 0 1");
224-
cout << "Mate Eval: " << eval(board) << endl;
225221
}
226222
}
227223

move_ordering.cpp

Lines changed: 113 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,114 @@
1515
#include "move_ordering.hpp"
1616
namespace move_ordering
1717
{
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+
}
18126

19127
void moveOrder(chess::Position &board, chess::Movelist &moves, int ply)
20128
{
@@ -36,9 +144,9 @@ namespace move_ordering
36144
// Capture scoring
37145
if (board.isCapture(move))
38146
{
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);
42150
}
43151

44152
// Counter move bonus (simple equality check)
@@ -58,7 +166,7 @@ namespace move_ordering
58166

59167
// Promotion bonus
60168
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());
62170

63171
// Set the score
64172
move.setScore(score);
@@ -70,4 +178,4 @@ namespace move_ordering
70178
return a.score() > b.score();
71179
});
72180
}
73-
}
181+
}

0 commit comments

Comments
 (0)