Skip to content

Commit 364e987

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents af01042 + a441b78 commit 364e987

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

BoardAnalysis.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Shogi
99
{
1010
public static class BoardAnalysis
1111
{
12+
private static readonly Random rng = new();
13+
1214
/// <summary>
1315
/// Determine whether a king can be reached by any of the opponents pieces
1416
/// </summary>
@@ -270,9 +272,10 @@ public PossibleMove(Point source, Point destination, double evaluatedFutureValue
270272
/// Use <see cref="EvaluatePossibleMoves"/> to find the best possible move in the current state of the game
271273
/// </summary>
272274
/// <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)
274277
{
275-
PossibleMove[] moves = await EvaluatePossibleMoves(game, maxDepth, cancellationToken);
278+
PossibleMove[] moves = await EvaluatePossibleMoves(game, maxDepth, randomise, cancellationToken);
276279
PossibleMove bestMove = new(default, default,
277280
game.CurrentTurnSente ? double.NegativeInfinity : double.PositiveInfinity, false, false, 0, 0, false, new());
278281
foreach (PossibleMove potentialMove in moves)
@@ -311,8 +314,9 @@ public static async Task<PossibleMove> EstimateBestPossibleMove(ShogiGame game,
311314
/// Evaluate each possible move in the current state of the game
312315
/// </summary>
313316
/// <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>
314318
/// <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)
316320
{
317321
List<Task<PossibleMove>> evaluationTasks = new();
318322

@@ -410,8 +414,14 @@ public static async Task<PossibleMove[]> EvaluatePossibleMoves(ShogiGame game, i
410414
}
411415
try
412416
{
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+
}
413423
// Remove default moves from return value
414-
return (await Task.WhenAll(evaluationTasks)).Where(m => m.Source != m.Destination).ToArray();
424+
return moves.ToArray();
415425
}
416426
catch (TaskCanceledException)
417427
{

MainWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ private void UpdateEvaluationMeter(BoardAnalysis.PossibleMove? bestMove, bool se
534534
{
535535
BoardAnalysis.PossibleMove? bestMove = null;
536536
// Search deeper in minishogi games
537-
bestMove ??= await BoardAnalysis.EstimateBestPossibleMove(game, game.Board.GetLength(0) == 5 ? 4 : 3, cancellationToken);
537+
bestMove ??= await BoardAnalysis.EstimateBestPossibleMove(game, game.Board.GetLength(0) == 5 ? 4 : 3, true, cancellationToken);
538538
return bestMove.Value;
539539
}
540540

0 commit comments

Comments
 (0)