@@ -10,6 +10,8 @@ internal class AlphaBetaSolver<TPosition, TStep> : ISolver<TStep>
1010 private readonly ISearchManager searchManager ;
1111 private readonly Action < string > logger ;
1212
13+ private const int maxDepth = 7 ; // TODO: magic number!
14+
1315 public AlphaBetaSolver (
1416 AlphaBetaPruning < TPosition , TStep > alphaBetaPruning ,
1517 IRules < TPosition , TStep > rules ,
@@ -31,7 +33,7 @@ public TStep OptimizeNextStep(IList<TStep> history)
3133 var bestStep = default ( TStep ) ;
3234 var guess = 0 ;
3335
34- for ( int i = 2 ; i < 7 ; i += 2 ) // TODO: magic number (7)!
36+ for ( int i = 2 ; i < maxDepth ; i += 2 )
3537 {
3638 searchManager . DepthLimit = i ;
3739 var ( OptimalSteps , Value ) = BestNodeSearch ( position , guess ) ;
@@ -45,17 +47,25 @@ public TStep OptimizeNextStep(IList<TStep> history)
4547
4648 private ( ICollection < TStep > OptimalSteps , int Value ) BestNodeSearch ( TPosition position , int guess )
4749 {
48- int alpha = - int . MaxValue ;
49- int beta = int . MaxValue ;
50+ int alpha = - sbyte . MaxValue - maxDepth ;
51+ int beta = sbyte . MaxValue + maxDepth ;
5052
5153 IList < TStep > candidates = rules . LegalStepsAt ( position ) . ToList ( ) ;
5254
55+ var optimalValue = 0 ;
56+
5357 while ( alpha + 1 < beta && candidates . Count > 1 )
5458 {
5559 var newCandidates = new List < TStep > ( ) ;
5660
5761 foreach ( var move in candidates )
5862 {
63+ if ( newCandidates . Count > 1 )
64+ {
65+ newCandidates . Add ( move ) ;
66+ continue ;
67+ }
68+
5969 int value = NullWindowTest ( position , move , guess ) ;
6070
6171 if ( value >= guess )
@@ -66,6 +76,7 @@ public TStep OptimizeNextStep(IList<TStep> history)
6676
6777 if ( newCandidates . Count > 0 )
6878 {
79+ optimalValue = guess ;
6980 candidates = newCandidates ;
7081 alpha = guess ;
7182 }
@@ -74,10 +85,10 @@ public TStep OptimizeNextStep(IList<TStep> history)
7485 beta = guess ;
7586 }
7687
77- guess = NextGuess ( alpha , beta , candidates . Count ) ;
88+ guess = newCandidates . Count > 0 ? guess + 1 : guess - 1 ;
7889 }
7990
80- return ( candidates , guess ) ;
91+ return ( candidates , optimalValue ) ;
8192 }
8293
8394 private int NullWindowTest ( TPosition position , TStep step , int guess )
@@ -88,31 +99,6 @@ private int NullWindowTest(TPosition position, TStep step, int guess)
8899 return value ;
89100 }
90101
91- private int NextGuess ( int alpha , int beta , int count )
92- {
93- if ( alpha <= 0 )
94- {
95- beta = Math . Min ( beta , int . MaxValue / 2 ) ;
96- }
97-
98- if ( beta >= 0 )
99- {
100- alpha = Math . Max ( alpha , - int . MaxValue / 2 ) ;
101- }
102-
103- var guess = ( int ) ( alpha + ( count - 1.0 ) / count * ( beta - alpha ) ) ;
104-
105- if ( guess == alpha )
106- {
107- return guess + 1 ;
108- }
109- else if ( guess == beta )
110- {
111- return guess - 1 ;
112- }
113- return guess ;
114- }
115-
116102 private TPosition CreatePosition ( IEnumerable < TStep > history )
117103 {
118104 var position = rules . InitialPosition ( ) ;
0 commit comments