@@ -17,62 +17,68 @@ Move Search::search(ChessBoard &board, const int timeAllowed) {
17
17
search.logger .logToFile (" starting search\n " );
18
18
19
19
const auto timeOut = std::chrono::milliseconds (timeAllowed);
20
-
21
20
const auto start = std::chrono::steady_clock::now ();
21
+
22
22
int i = 1 ;
23
- for (; i < 64 ; ++i) {
24
- search.logger .log (std::format (" info searching depth {}\n " , i));
25
- search.logger .logToFile (std::format (" starting depth {}\n " , i));
23
+ try {
24
+ for (; i < 64 ; ++i) {
25
+ search.logger .log (std::format (" info searching depth {}\n " , i));
26
+ search.logger .logToFile (std::format (" starting depth {}\n " , i));
26
27
27
- std::thread thread (&Search::threadedSearch, &search, i);
28
+ std::thread thread (&Search::threadedSearch, &search, i);
28
29
29
- std::unique_lock<std::mutex> lk (search.cv_m );
30
- search.stop = false ;
31
- search.finished = false ;
30
+ std::unique_lock<std::mutex> lk (search.cv_m );
31
+ search.stop = false ;
32
+ search.finished = false ;
32
33
33
- const auto timeAvailable = start + timeOut - std::chrono::steady_clock::now ();
34
+ const auto timeAvailable = start + timeOut - std::chrono::steady_clock::now ();
34
35
35
- if (search.cv .wait_for (lk, timeAvailable, [&] { return search.finished ; })) {
36
- thread.join ();
37
- } else {
38
- search.stop = true ;
39
- thread.join ();
40
- break ;
41
- }
36
+ if (search.cv .wait_for (lk, timeAvailable, [&] { return search.finished ; })) {
37
+ thread.join ();
38
+ } else {
39
+ search.stop = true ;
40
+ thread.join ();
41
+ break ;
42
+ }
42
43
43
- bool endEarly = false ;
44
+ bool endEarly = false ;
44
45
45
- search.lastPV = search.collectPV (i, endEarly);
46
+ search.lastPV = search.collectPV (i, endEarly);
46
47
47
- if (endEarly) break ;
48
+ if (endEarly) break ;
48
49
49
- if (search.lastPV .size () > i) {
50
- i = search.lastPV .size ();
50
+ if (search.lastPV .size () > i) {
51
+ i = search.lastPV .size ();
52
+ }
51
53
}
52
- }
53
54
#ifdef wasm
54
- printf (" Depth: %d\n " , i - 1 );
55
- int score = Evaluator::evaluate (board);
56
- if (tt.contains (board.hashCode )) {
57
- const TranspositionTable::Entry entry = tt.getEntry (board.hashCode , 0 );
58
- score = entry.score ;
55
+ printf (" Depth: %d\n " , i - 1 );
56
+ int score = Evaluator::evaluate (board);
57
+ if (tt.contains (board.hashCode )) {
58
+ const TranspositionTable::Entry entry = tt.getEntry (board.hashCode , 0 );
59
+ score = entry.score ;
60
+ }
61
+ printf (" Evaluation: %d\n PV: " , score);
62
+ for (const Move&move: search.lastPV ) {
63
+ printf (" %s%s " , Util::positionToString (move.start ).c_str (), Util::positionToString (move.end ).c_str ());
64
+ }
65
+ const int occupancy = tt.occupancy ();
66
+ printf (" \n Board hash: %llu" , board.hashCode );
67
+ printf (" \n TT reads: %d" , tt.reads );
68
+ printf (" \n TT writes: %d" , tt.writes );
69
+ printf (" \n TT collisions: %d" , tt.collisions );
70
+ printf (" \n TT occupancy: %d" , occupancy);
71
+ printf (" \n **************************\n " );
72
+ search.logger .sendInt (" updateDepth" , i - 1 );
73
+ search.logger .sendInt (" updateTTOccupancy" , tt.occupancy ());
74
+ #endif
75
+
76
+ tt.resetCounters ();
59
77
}
60
- printf (" Evaluation: %d\n PV: " , score);
61
- for (const Move&move: search.lastPV ) {
62
- printf (" %s%s " , Util::positionToString (move.start ).c_str (), Util::positionToString (move.end ).c_str ());
78
+ catch (std::exception &e) {
79
+ std::cout << " Exception while searching: " << e.what () << " | Will try to return PV[0]" << std::endl;
63
80
}
64
- const int occupancy = tt.occupancy ();
65
- printf (" \n Board hash: %llu" , board.hashCode );
66
- printf (" \n TT reads: %d" , tt.reads );
67
- printf (" \n TT writes: %d" , tt.writes );
68
- printf (" \n TT collisions: %d" , tt.collisions );
69
- printf (" \n TT occupancy: %d" , occupancy);
70
- printf (" \n **************************\n " );
71
- search.logger .sendInt (" updateDepth" , i - 1 );
72
- search.logger .sendInt (" updateTTOccupancy" , tt.occupancy ());
73
- #endif
74
81
75
- tt.resetCounters ();
76
82
search.lastPV = search.collectPV (i);
77
83
78
84
if (search.lastPV .empty ()) {
0 commit comments