A simple chess board adapter that records moves as UCI strings. It accepts local and remote moves, stores them in a list, can export the move list as a space separated string, and import such a state string by parsing UCI moves.
#nullable enable annotations
namespace LichessNET.Gameplay;
public sealed class RecordingChessBoardAdapter : IChessBoardAdapter
{
private readonly List<string> _moves = new();
public IReadOnlyList<string> Moves => _moves;
public bool TryApplyLocalMove(string uci)
{
return TryRecordMove(uci);
}
public bool TryApplyRemoteMove(string uci)
{
return TryRecordMove(uci);
}
public string ExportState()
{
return string.Join(" ", _moves);
}
public void ImportState(string state)
{
_moves.Clear();
if (string.IsNullOrWhiteSpace(state))
return;
foreach (var move in state.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
if (UciMove.TryParse(move, out var parsed))
_moves.Add(parsed.ToString());
}
}
private bool TryRecordMove(string uci)
{
if (!UciMove.TryParse(uci, out var move))
return false;
_moves.Add(move.ToString());
return true;
}
}