@@ -9,6 +9,8 @@ namespace Shogi
9
9
{
10
10
public static class BoardAnalysis
11
11
{
12
+ private static readonly Random rng = new ( ) ;
13
+
12
14
/// <summary>
13
15
/// Determine whether a king can be reached by any of the opponents pieces
14
16
/// </summary>
@@ -270,9 +272,10 @@ public PossibleMove(Point source, Point destination, double evaluatedFutureValue
270
272
/// Use <see cref="EvaluatePossibleMoves"/> to find the best possible move in the current state of the game
271
273
/// </summary>
272
274
/// <param name="maxDepth">The maximum number of half-moves in the future to search</param>
273
- public static async Task < PossibleMove > EstimateBestPossibleMove ( ShogiGame game , int maxDepth , CancellationToken cancellationToken )
275
+ /// <param name="randomise">Whether or not to randomise the order of moves that have the same score</param>
276
+ public static async Task < PossibleMove > EstimateBestPossibleMove ( ShogiGame game , int maxDepth , bool randomise , CancellationToken cancellationToken )
274
277
{
275
- PossibleMove [ ] moves = await EvaluatePossibleMoves ( game , maxDepth , cancellationToken ) ;
278
+ PossibleMove [ ] moves = await EvaluatePossibleMoves ( game , maxDepth , randomise , cancellationToken ) ;
276
279
PossibleMove bestMove = new ( default , default ,
277
280
game . CurrentTurnSente ? double . NegativeInfinity : double . PositiveInfinity , false , false , 0 , 0 , false , new ( ) ) ;
278
281
foreach ( PossibleMove potentialMove in moves )
@@ -311,8 +314,9 @@ public static async Task<PossibleMove> EstimateBestPossibleMove(ShogiGame game,
311
314
/// Evaluate each possible move in the current state of the game
312
315
/// </summary>
313
316
/// <param name="maxDepth">The maximum number of half-moves in the future to search</param>
317
+ /// <param name="randomise">Whether or not to randomise the order of moves that have the same score</param>
314
318
/// <returns>An array of all possible moves, with information on board value and ability to checkmate</returns>
315
- public static async Task < PossibleMove [ ] > EvaluatePossibleMoves ( ShogiGame game , int maxDepth , CancellationToken cancellationToken )
319
+ public static async Task < PossibleMove [ ] > EvaluatePossibleMoves ( ShogiGame game , int maxDepth , bool randomise , CancellationToken cancellationToken )
316
320
{
317
321
List < Task < PossibleMove > > evaluationTasks = new ( ) ;
318
322
@@ -410,8 +414,14 @@ public static async Task<PossibleMove[]> EvaluatePossibleMoves(ShogiGame game, i
410
414
}
411
415
try
412
416
{
417
+ IEnumerable < PossibleMove > moves =
418
+ ( await Task . WhenAll ( evaluationTasks ) ) . Where ( m => m . Source != m . Destination ) ;
419
+ if ( randomise )
420
+ {
421
+ return moves . OrderBy ( _ => rng . Next ( ) ) . ToArray ( ) ;
422
+ }
413
423
// Remove default moves from return value
414
- return ( await Task . WhenAll ( evaluationTasks ) ) . Where ( m => m . Source != m . Destination ) . ToArray ( ) ;
424
+ return moves . ToArray ( ) ;
415
425
}
416
426
catch ( TaskCanceledException )
417
427
{
0 commit comments