Skip to content

Commit

Permalink
uci interface rewrite + fixed opening book (illegal move issue) (#40)
Browse files Browse the repository at this point in the history
* fix opening book bug + rewrote uci ifc
* pruned logs
  • Loading branch information
sictransit authored Dec 7, 2024
1 parent f0c5175 commit 3320b76
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 96 deletions.
9 changes: 5 additions & 4 deletions Common/Lookup/OpeningBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,14 @@ public IEnumerable<OpeningBookMove> GetMoves(ulong hash)

public Move? GetTheoryMove(Board board)
{
// Check all legal moves on the board.
foreach (var newBoard in board.PlayLegalMoves())
{
var theoryMove = GetMove(newBoard);

if (theoryMove != null)
// Check if the new board is in the opening book.
if (GetMove(newBoard) != null)
{
return theoryMove;
// If the move returns us to the opening book, return that move.
return newBoard.Counters.LastMove;
}
}

Expand Down
28 changes: 17 additions & 11 deletions Engine/Patzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@
using SicTransit.Woodpusher.Model.Enums;
using SicTransit.Woodpusher.Model.Extensions;
using System.Diagnostics;
using System.Text;

namespace SicTransit.Woodpusher.Engine
{
public class Patzer : IEngine
{
public Board Board { get; private set; }

private bool timeIsUp = false;
private volatile bool timeIsUp = false;
private int maxDepth = 0;
private int selDepth = 0;
private uint nodeCount = 0;
private const uint EngineMaxDepth = 128;

private OpeningBook? openingBook;

private readonly Action<string>? infoCallback;
private readonly Action<string, bool>? infoCallback;

private const int transpositionTableSize = 1_000_000;

Expand All @@ -38,7 +39,7 @@ public class Patzer : IEngine

private EngineOptions engineOptions;

public Patzer(Action<string>? infoCallback = null)
public Patzer(Action<string, bool>? infoCallback = null)
{
this.infoCallback = infoCallback;

Expand Down Expand Up @@ -67,12 +68,12 @@ private void SetBoard(Board board)
repetitionTable[Board.Hash] = 1;
}

private void SendCallbackInfo(string info) => infoCallback?.Invoke(info);
private void SendCallbackInfo(string message, bool info) => infoCallback?.Invoke(message, info);

public void Play(Move move)
{
var color = Board.ActiveColor.Is(Piece.White) ? "White" : "Black";
Log.Debug("{Color} plays: {Move}", color, move);
Log.Debug("{Color} plays: {Move}", Board.ActiveColor.Is(Piece.White) ? "White" : "Black", move);

Board = Board.Play(move);

if (Board.Counters.HalfmoveClock == 0)
Expand Down Expand Up @@ -107,12 +108,17 @@ public void Position(string fen, IEnumerable<AlgebraicMove>? algebraicMoves = nu

public void Perft(int depth)
{
ulong nodes = 0;
ulong totalNodes = 0;
var sb = new StringBuilder();

foreach (var board in Board.PlayLegalMoves())
{
nodes += depth > 1 ? board.Perft(depth) : 1;
var nodes = depth > 1 ? board.Perft(depth) : 1;
sb.AppendLine($"{board.Counters.LastMove.ToAlgebraicMoveNotation()}: {nodes}");

totalNodes += nodes;
}
SendCallbackInfo(Environment.NewLine + $"Nodes searched: {nodes}");
SendCallbackInfo(sb.ToString() + Environment.NewLine + $"Nodes searched: {totalNodes}", false);
}

public AlgebraicMove? FindBestMove(int timeLimit = 1000)
Expand Down Expand Up @@ -149,7 +155,7 @@ public void Perft(int depth)

try
{
Log.Information("Thinking time: {TimeLimit} ms", timeLimit);
Log.Information("{Player} thinking time: {TimeLimit} ms", Board.ActiveColor.Is(Piece.White) ? "White" : "Black", timeLimit);

bestMove = SearchForBestMove(stopwatch, timeLimit);
}
Expand Down Expand Up @@ -308,7 +314,7 @@ private void UpdateBestLine(Move bestMove)
}
}

private void SendInfo(string info) => SendCallbackInfo($"info {info}");
private void SendInfo(string info) => SendCallbackInfo($"info {info}", true);

private void SendDebugInfo(string debugInfo) => SendInfo($"string debug {debugInfo}");

Expand Down
10 changes: 7 additions & 3 deletions EngineTests/PatzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ public class PatzerTests
private Patzer patzer;
private readonly List<string> traceLines = [];

private void PatzerCallback(string s)
private void PatzerCallback(string message, bool info)
{
Trace.WriteLine(s);
traceLines.Add(s);
if (!info)
{
Trace.WriteLine(message);
}

traceLines.Add(message);
}

[TestInitialize]
Expand Down
39 changes: 30 additions & 9 deletions Woodpusher/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Serilog;
using SicTransit.Woodpusher.Common;
using SicTransit.Woodpusher.Engine;

namespace SicTransit.Woodpusher
{
Expand All @@ -10,24 +9,46 @@ public static void Main(string[] args)
{
Logging.EnableLogging(Serilog.Events.LogEventLevel.Debug, false);

static void ConsoleOutput(string s)
static void ConsoleOutput(string message, bool isInfo = false)
{
Console.WriteLine(s);
Console.WriteLine(message);

Log.Information("Sent: {Message}", s);
if (isInfo)
{
Log.Debug("Sent: {Message}", message);
}
else
{
Log.Information("Sent: {Message}", message);
}
}

var uci = new UniversalChessInterface(ConsoleOutput, new Patzer(ConsoleOutput));
using var quitSource = new CancellationTokenSource();

var uci = new UniversalChessInterface(ConsoleOutput, quitSource.Token);

uci.Run();

while (!uci.Quit)
while (!quitSource.Token.IsCancellationRequested)
{
var line = Console.ReadLine();
var line = Console.ReadLine()?.Trim();

Log.Information("Received: {Line}", line);

if (line != null)
if (!string.IsNullOrEmpty(line))
{
uci.ProcessCommand(line);
switch (line)
{
case "quit":
quitSource.Cancel();
break;
case "stop":
uci.Stop();
break;
default:
uci.EnqueueCommand(line);
break;
}
}
}
}
Expand Down
Loading

0 comments on commit 3320b76

Please sign in to comment.