@@ -4,24 +4,10 @@ namespace search
44{
55 // Transposition table
66 TranspositionTable tt (20 );
7- // Principal variation
8- chess::Move pv[256 ];
97
10- void updatePV (int ply, chess::Move best_move) {
11- chess::Move newPV[256 ];
12- newPV[ply] = best_move;
13-
14- int i = ply + 1 ;
15- for (; i < 256 && pv[i] != chess::Move (); ++i)
16- newPV[i] = pv[i];
17-
18- newPV[i] = chess::Move (); // null-terminate
19-
20- std::memcpy (pv + ply, newPV + ply, sizeof (chess::Move) * (i - ply + 1 ));
21- }
22-
23- int16_t alphaBeta (chess::Position &board, int16_t alpha, int16_t beta, int depth, int ply)
8+ int16_t alphaBeta (chess::Position &board, int16_t alpha, int16_t beta, int depth, int ply, PV *pv)
249 {
10+ PV _pv={0 };
2511 nodes++;
2612 auto ttEntry = tt.probe (board.hash ());
2713 if (ttEntry)
@@ -38,12 +24,6 @@ void updatePV(int ply, chess::Move best_move) {
3824 return ttEntry->score ();
3925 }
4026 }
41- if (ply == 0 )
42- {
43- memset (pv, 0 , sizeof (pv));
44- nodes = 0 ;
45- tt.clear_stats ();
46- }
4727 if (depth == 0 )
4828 return quiescence (board, alpha, beta, ply);
4929
@@ -64,7 +44,7 @@ void updatePV(int ply, chess::Move best_move) {
6444 for (const auto &move : moves)
6545 {
6646 board.makeMove (move);
67- int16_t score = -alphaBeta (board, -beta, -alpha, depth - 1 , ply + 1 );
47+ int16_t score = -alphaBeta (board, -beta, -alpha, depth - 1 , ply + 1 , &_pv );
6848 board.pop ();
6949
7050 if (score >= beta)
@@ -76,7 +56,11 @@ void updatePV(int ply, chess::Move best_move) {
7656 {
7757 alpha = score;
7858 bestmove = move;
79- updatePV (ply, bestmove); // Move + child PV
59+ pv->argmove [0 ] = move;
60+
61+ memcpy (pv->argmove + 1 , _pv.argmove , _pv.cmove * sizeof (chess::Move));
62+
63+ pv->cmove = _pv.cmove + 1 ;
8064 foundPV = true ;
8165 }
8266 }
@@ -111,12 +95,20 @@ void updatePV(int ply, chess::Move best_move) {
11195 }
11296 return alpha;
11397 }
114-
98+ PV pv;
99+ int16_t alphaBeta (chess::Position &board, int16_t alpha, int16_t beta, int depth)
100+ {
101+ memset (&pv, 0 , sizeof (pv));
102+ pv.cmove = 0 ;
103+ nodes = 0 ;
104+ tt.clear_stats ();
105+ return alphaBeta (board, alpha, beta, depth, 0 , &pv);
106+ }
115107 void printPV (chess::Position &board)
116108 {
117109 std::cout << " Principal Variation: " ;
118- for (int i = 0 ; i < 256 && pv[i] != chess::Move (); i++)
119- std::cout << pv[i] << " " ;
110+ for (int i = 0 ; i < pv. cmove && pv. argmove [i] != chess::Move (); i++)
111+ std::cout << pv. argmove [i] << " " ;
120112 std::cout << std::endl;
121113 }
122114}
0 commit comments