@@ -8,7 +8,6 @@ internal class AlphaBetaPruning<TPosition, TStep> : IAlphaBetaPruning<TPosition>
88 private readonly IHeuristicTables < TStep > heuristicTables ;
99 private readonly ISearchManager searchManager ;
1010
11- private const int MaxSearchDepth = 16 ;
1211 private readonly List < TStep > [ ] orderedStepBuffers ;
1312
1413 public AlphaBetaPruning (
@@ -22,8 +21,8 @@ public AlphaBetaPruning(
2221 this . heuristicTables = heuristicTables ?? throw new ArgumentNullException ( nameof ( heuristicTables ) ) ;
2322 this . searchManager = searchManager ?? throw new ArgumentNullException ( nameof ( searchManager ) ) ;
2423
25- orderedStepBuffers = new List < TStep > [ MaxSearchDepth ] ;
26- for ( int i = 0 ; i < MaxSearchDepth ; i ++ )
24+ orderedStepBuffers = new List < TStep > [ searchManager . MaxDepth ] ;
25+ for ( int i = 0 ; i < searchManager . MaxDepth ; i ++ )
2726 {
2827 orderedStepBuffers [ i ] = new List < TStep > ( ) ;
2928 }
@@ -36,11 +35,17 @@ public int Search(TPosition position, int alpha, int beta)
3635
3736 private int SearchRecursively ( TPosition position , int depth , int alpha , int beta )
3837 {
38+ searchManager . CheckTimeBudget ( ) ;
39+ if ( searchManager . IsAborted )
40+ {
41+ return 0 ;
42+ }
43+
3944 if ( depth <= 0 )
4045 {
4146 if ( rules . IsGoal ( position ) )
4247 {
43- return - ( sbyte . MaxValue + depth ) ;
48+ return - WinValue ( depth ) ;
4449 }
4550 return - HeuristicValue ( position , depth ) ;
4651 }
@@ -73,7 +78,7 @@ private int SearchRecursively(TPosition position, int depth, int alpha, int beta
7378
7479 if ( orderedSteps . Count == 0 )
7580 {
76- return - ( rules . IsGoal ( position ) ? sbyte . MaxValue + depth : 0 ) ;
81+ return - ( rules . IsGoal ( position ) ? WinValue ( depth ) : 0 ) ;
7782 }
7883
7984 var bestValue = - int . MaxValue ;
@@ -98,7 +103,7 @@ private int SearchRecursively(TPosition position, int depth, int alpha, int beta
98103 break ;
99104 }
100105 }
101- if ( depth > 1 )
106+ if ( depth > 1 && ! searchManager . IsAborted )
102107 {
103108 var newTransposition = new Transposition < TStep > ( GetEvaluationMode ( bestValue , originalAlpha , beta ) , bestValue , depth , bestStep ) ;
104109 cacheTables . AddTransposition ( position , newTransposition ) ;
@@ -182,5 +187,12 @@ private bool IsOpponentsTurn(int depth)
182187 int distanceFromRoot = searchManager . DepthLimit - depth ;
183188 return distanceFromRoot % 2 != 0 ;
184189 }
190+
191+ // Distance-from-root makes this value independent of DepthLimit (important for TT consistency)
192+ private int WinValue ( int depth )
193+ {
194+ int distanceFromRoot = searchManager . DepthLimit - depth ;
195+ return sbyte . MaxValue + searchManager . MaxDepth - distanceFromRoot ;
196+ }
185197 }
186198}
0 commit comments