11#include "ai.h"
22#include <limits.h>
33
4- SearchInfo info = {0 };
5-
64// ==================== Move Ordering ====================
75
86static int get_move_score (Chessboard * board , const Move * move )
@@ -37,10 +35,10 @@ static void order_moves(Chessboard *board, Move *moves, int count)
3735
3836// ==================== Quiescence Search ====================
3937
40- static int quiescence_search (Chessboard * board , int alpha , int beta )
38+ static int quiescence_search (Chessboard * board , int alpha , int beta , SearchInfo * info )
4139{
4240 int stand_pat = evaluate_position (board );
43- info . nb_positions_evaluated += 1 ;
41+ info -> nb_positions_evaluated += 1 ;
4442
4543 if (stand_pat >= beta )
4644 return beta ;
@@ -56,7 +54,7 @@ static int quiescence_search(Chessboard *board, int alpha, int beta)
5654 continue ;
5755
5856 play_move (board , moves [i ]);
59- int score = - quiescence_search (board , - beta , - alpha );
57+ int score = - quiescence_search (board , - beta , - alpha , info );
6058 unplay_move (board , moves [i ]);
6159
6260 if (score >= beta )
@@ -70,10 +68,10 @@ static int quiescence_search(Chessboard *board, int alpha, int beta)
7068
7169// ==================== Alpha-Beta ====================
7270
73- int alpha_beta (Chessboard * board , int depth , int alpha , int beta )
71+ int alpha_beta (Chessboard * board , int depth , int alpha , int beta , SearchInfo * info )
7472{
7573 if (depth == 0 )
76- return quiescence_search (board , alpha , beta );
74+ return quiescence_search (board , alpha , beta , info );
7775
7876 Move moves [250 ];
7977 int count = get_all_legal_moves (board , moves );
@@ -92,7 +90,7 @@ int alpha_beta(Chessboard *board, int depth, int alpha, int beta)
9290 for (int i = 0 ; i < count ; i ++ )
9391 {
9492 play_move (board , moves [i ]);
95- int score = - alpha_beta (board , depth - 1 , - beta , - alpha );
93+ int score = - alpha_beta (board , depth - 1 , - beta , - alpha , info );
9694 unplay_move (board , moves [i ]);
9795
9896 if (score > best )
@@ -109,21 +107,21 @@ int alpha_beta(Chessboard *board, int depth, int alpha, int beta)
109107
110108// ==================== Root Search ====================
111109
112- static int get_score (Chessboard * board , Move move , int depth )
110+ static int get_score (Chessboard * board , Move move , int depth , SearchInfo * info )
113111{
114112 play_move (board , move );
115113 int score ;
116114
117115 if (depth == 0 )
118116 score = evaluate_position (board ); // Potentiellement quiecence search au lieu de evaluate mais probablement pas d'impact
119117 else
120- score = - alpha_beta (board , depth - 1 , - INFINI , INFINI );
118+ score = - alpha_beta (board , depth - 1 , - INFINI , INFINI , info );
121119
122120 unplay_move (board , move );
123121 return score ;
124122}
125123
126- static ScoredMove search_best_move (Chessboard * board , int depth )
124+ static ScoredMove search_best_move (Chessboard * board , int depth , SearchInfo * info )
127125{
128126 Move legal_moves [250 ];
129127 int nbmoves = get_all_legal_moves (board , legal_moves );
@@ -136,14 +134,11 @@ static ScoredMove search_best_move(Chessboard *board, int depth)
136134
137135 ScoredMove best ;
138136 best .move = legal_moves [0 ];
139- best .score = get_score (board , legal_moves [0 ], depth );
137+ best .score = get_score (board , legal_moves [0 ], depth , info );
140138
141139 for (int i = 1 ; i < nbmoves ; i ++ )
142140 {
143- int score = get_score (board , legal_moves [i ], depth );
144-
145- //print_move(&legal_moves[i]);
146- //printf("score: %i\n", score);
141+ int score = get_score (board , legal_moves [i ], depth , info );
147142
148143 if (score > best .score )
149144 {
@@ -152,14 +147,17 @@ static ScoredMove search_best_move(Chessboard *board, int depth)
152147 }
153148 }
154149
155- //printf("nb_pos: %i, score: %i\n", info.nb_positions_evaluated, best.score);
156-
157150 return best ;
158151}
159152
160- Move get_best_move (Chessboard board )
153+ SearchInfo get_best_move (Chessboard board )
161154{
155+ SearchInfo info = {0 };
162156 info .nb_positions_evaluated = 0 ;
163- ScoredMove best_move = search_best_move (& board , 3 );
164- return best_move .move ;
157+ info .depth = 3 ;
158+ info .log [0 ] = '\0' ;
159+
160+ info .move = search_best_move (& board , info .depth , & info );
161+ snprintf (info .log , SEARCH_LOG_SIZE , "depth=%d evaluations=%d" , info .depth , info .nb_positions_evaluated );
162+ return info ;
165163}
0 commit comments