Skip to content

Commit

Permalink
Randomise moves of equal value
Browse files Browse the repository at this point in the history
  • Loading branch information
TollyH committed Nov 21, 2023
1 parent d000b42 commit a441b78
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 Chess
{
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 @@ -336,9 +338,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(ChessGame 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(ChessGame 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.CurrentTurnWhite ? double.NegativeInfinity : double.PositiveInfinity, false, false, 0, 0, typeof(Pieces.Queen), new());
foreach (PossibleMove potentialMove in moves)
Expand Down Expand Up @@ -377,8 +380,9 @@ public static async Task<PossibleMove> EstimateBestPossibleMove(ChessGame 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(ChessGame game, int maxDepth, CancellationToken cancellationToken)
public static async Task<PossibleMove[]> EvaluatePossibleMoves(ChessGame game, int maxDepth, bool randomise, CancellationToken cancellationToken)
{
List<Task<PossibleMove>> evaluationTasks = new();

Expand Down Expand Up @@ -418,8 +422,14 @@ public static async Task<PossibleMove[]> EvaluatePossibleMoves(ChessGame 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 @@ -530,7 +530,7 @@ private void UpdateEvaluationMeter(BoardAnalysis.PossibleMove? bestMove, bool wh
{
return default;
}
bestMove ??= await BoardAnalysis.EstimateBestPossibleMove(game, 4, cancellationToken);
bestMove ??= await BoardAnalysis.EstimateBestPossibleMove(game, 4, true, cancellationToken);
return bestMove.Value;
}

Expand Down

0 comments on commit a441b78

Please sign in to comment.