Improve game visibility and adjust search depth
Added several logging options to increase visibility of the game's internal operations. Also limited the chess engine's iterative deepening depth-first search (IDDFS) to a depth of 10 to improve performance efficiency. Additionally, changed the representation of '0' to 'O' due to notation issues in the GamesController.
This commit is contained in:
@@ -66,6 +66,7 @@ public class GamesController(InMemoryDatabase database) : Controller
|
||||
.Filter(t => !string.IsNullOrWhiteSpace(t))
|
||||
.Subscribe(async move =>
|
||||
{
|
||||
move = move.Replace("0", "O");
|
||||
lock (challenge.Game.MovePieceLock)
|
||||
{
|
||||
if (!challenge.Game.Board.IsEndGame &&
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace Aiursoft.ChessServer.Controllers;
|
||||
|
||||
[Route("pve")]
|
||||
public class PveController(
|
||||
ILogger<PveController> logger,
|
||||
ChessEngine engine,
|
||||
Counter counter,
|
||||
InMemoryDatabase database) : Controller
|
||||
@@ -21,6 +22,7 @@ public class PveController(
|
||||
public async Task<IActionResult> New(Guid playerId)
|
||||
{
|
||||
// Add a computer player
|
||||
logger.LogInformation("Creating a new PVE game for player {playerId}.", playerId);
|
||||
var computerId = Guid.NewGuid();
|
||||
database.GetOrAddPlayer(computerId).NickName = "Computer";
|
||||
//var asyncLock = new SemaphoreSlim(1, 1);
|
||||
@@ -51,12 +53,17 @@ public class PveController(
|
||||
var client = await webSocketEndpoint.ConnectAsWebSocketServer();
|
||||
subscription = client.Subscribe(async fen =>
|
||||
{
|
||||
logger.LogInformation("Computer player received fen: {fen}", fen);
|
||||
|
||||
// When fen changes, it means someone has made a move. If it's the computer's turn, let the computer respond.
|
||||
if (ChessBoard.LoadFromFen(fen).Turn == PieceColor.Black)
|
||||
{
|
||||
logger.LogInformation("The fen {fen} means it's the computer's turn. Computer is calculating the best move.", fen);
|
||||
// Wait for the UI to update
|
||||
await Task.Delay(300);
|
||||
var bestMove = engine.GetBestMove(fen);
|
||||
|
||||
logger.LogInformation("Computer calculated the best move: {bestMove}", bestMove);
|
||||
await client.Send(bestMove);
|
||||
}
|
||||
});
|
||||
@@ -65,6 +72,7 @@ public class PveController(
|
||||
finally
|
||||
{
|
||||
subscription?.Unsubscribe();
|
||||
logger.LogInformation("Computer player unsubscribed.");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public class ChessEngine
|
||||
public string GetBestMove(string fen)
|
||||
{
|
||||
_engine.AdjustPosition($"position fen {fen}");
|
||||
return _engine.IDDFS(1, int.MaxValue)
|
||||
return _engine.IDDFS(1, 10)
|
||||
.BestMove
|
||||
.ToEPDString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user