Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
TollyH committed Nov 21, 2023
2 parents af01042 + a441b78 commit 364e987
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions BoardAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Shogi
{
public static class BoardAnalysis
{
private static readonly Random rng = new();

/// <summary>
/// Determine whether a king can be reached by any of the opponents pieces
/// </summary>
Expand Down Expand Up @@ -270,9 +272,10 @@ public PossibleMove(Point source, Point destination, double evaluatedFutureValue
/// Use <see cref="EvaluatePossibleMoves"/> to find the best possible move in the current state of the game
/// </summary>
/// <param name="maxDepth">The maximum number of half-moves in the future to search</param>
public static async Task<PossibleMove> EstimateBestPossibleMove(ShogiGame game, int maxDepth, CancellationToken cancellationToken)
/// <param name="randomise">Whether or not to randomise the order of moves that have the same score</param>
public static async Task<PossibleMove> EstimateBestPossibleMove(ShogiGame game, int maxDepth, bool randomise, CancellationToken cancellationToken)
{
PossibleMove[] moves = await EvaluatePossibleMoves(game, maxDepth, cancellationToken);
PossibleMove[] moves = await EvaluatePossibleMoves(game, maxDepth, randomise, cancellationToken);
PossibleMove bestMove = new(default, default,
game.CurrentTurnSente ? double.NegativeInfinity : double.PositiveInfinity, false, false, 0, 0, false, new());
foreach (PossibleMove potentialMove in moves)
Expand Down Expand Up @@ -311,8 +314,9 @@ public static async Task<PossibleMove> EstimateBestPossibleMove(ShogiGame game,
/// Evaluate each possible move in the current state of the game
/// </summary>
/// <param name="maxDepth">The maximum number of half-moves in the future to search</param>
/// <param name="randomise">Whether or not to randomise the order of moves that have the same score</param>
/// <returns>An array of all possible moves, with information on board value and ability to checkmate</returns>
public static async Task<PossibleMove[]> EvaluatePossibleMoves(ShogiGame game, int maxDepth, CancellationToken cancellationToken)
public static async Task<PossibleMove[]> EvaluatePossibleMoves(ShogiGame game, int maxDepth, bool randomise, CancellationToken cancellationToken)
{
List<Task<PossibleMove>> evaluationTasks = new();

Expand Down Expand Up @@ -410,8 +414,14 @@ public static async Task<PossibleMove[]> EvaluatePossibleMoves(ShogiGame game, i
}
try
{
IEnumerable<PossibleMove> moves =
(await Task.WhenAll(evaluationTasks)).Where(m => m.Source != m.Destination);
if (randomise)
{
return moves.OrderBy(_ => rng.Next()).ToArray();
}
// Remove default moves from return value
return (await Task.WhenAll(evaluationTasks)).Where(m => m.Source != m.Destination).ToArray();
return moves.ToArray();
}
catch (TaskCanceledException)
{
Expand Down
2 changes: 1 addition & 1 deletion MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ private void UpdateEvaluationMeter(BoardAnalysis.PossibleMove? bestMove, bool se
{
BoardAnalysis.PossibleMove? bestMove = null;
// Search deeper in minishogi games
bestMove ??= await BoardAnalysis.EstimateBestPossibleMove(game, game.Board.GetLength(0) == 5 ? 4 : 3, cancellationToken);
bestMove ??= await BoardAnalysis.EstimateBestPossibleMove(game, game.Board.GetLength(0) == 5 ? 4 : 3, true, cancellationToken);
return bestMove.Value;
}

Expand Down

0 comments on commit 364e987

Please sign in to comment.