Update difficulty handling in ChessEngine

The difficulty handling logic has been modified to better determine when to use the best move in the ChessEngine class. A new method called ShouldUseBestMove(int difficulty) has been added to provide a probabilistic approach towards choosing the best move based on the provided difficulty level.
This commit is contained in:
Anduin Xue
2024-07-06 09:18:18 +00:00
parent e780e7af7e
commit 4d6033f73f
@@ -43,7 +43,7 @@ public class ChessEngine
var result = _engine.IDDFS(depth, 10);
_engine.Game.ResetCurrentPositionToBeforeSearchState();
if (difficulty > 4)
if (ShouldUseBestMove(difficulty))
{
// If difficulty is higher than 5, we should use the best move
return result.BestMove.ToEPDString(positionClone);
@@ -54,4 +54,12 @@ public class ChessEngine
return result.Moves[new Random().Next(result.Moves.Count)].ToEPDString(positionClone);
}
}
private bool ShouldUseBestMove(int difficulty)
{
// For difficulties 1-6, gradually increase the probability of returning true
var probability = (difficulty - 1) / 5.0; // 0.0 for 1, 0.2 for 2, ..., 1.0 for 6
var random = new Random();
return random.NextDouble() < probability;
}
}