Skip to content

Commit

Permalink
First cleanup pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Pilcher committed Jul 31, 2023
1 parent ccdcad8 commit e3f11fd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 55 deletions.
5 changes: 3 additions & 2 deletions Chess-Challenge.Uci/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

var engine = new UciEngine();
var message = string.Empty;
while (message != "quit")
var run = true;
while (run)
{
message = Console.ReadLine();
if (!string.IsNullOrEmpty(message)) engine.ReceiveCommand(message);
run = string.IsNullOrEmpty(message) || engine.ReceiveCommand(message);
}
79 changes: 26 additions & 53 deletions Chess-Challenge.Uci/UciEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,15 @@ public class UciEngine
private const string Stop = "stop";
private const string Quit = "quit";

private const string botMatchStartFens = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";


private MyBot _bot = new MyBot();
private const string BotMatchStartFens = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";

private readonly MyBot _bot = new MyBot();
private Board _currentBoard;

private string _logFile = "./comm-log.txt";
private string _uciLog = "uci-log.txt";
private string _errorFile = "error.log";
private const string _dateFormat = "yyyyMMddTHHmmss";


private char GetPromotionCharacter(PieceType piece) =>
piece switch
{
PieceType.None => 'q',
PieceType.Pawn => 'q',
PieceType.Knight => 'n',
PieceType.Bishop => 'b',
PieceType.Rook => 'r',
PieceType.Queen => 'q',
PieceType.King => 'q',
_ => throw new ArgumentOutOfRangeException(nameof(piece), piece, null)
};
private const string LogFile = "./comm-log.txt";
private const string UciLog = "./uci-log.txt";
private const string ErrorFile = "./error.log";
private const string DateFormat = "yyyyMMddTHHmmss";

private void WriteLineToDisk(string line, string file)
{
Expand All @@ -51,53 +36,50 @@ private void WriteLineToDisk(string line, string file)
outputFile.WriteLine(line);

}
public void ReceiveCommand(string message)
public bool ReceiveCommand(string message)
{
var messageType = message.Split(' ')[0];
WriteLineToDisk($"{DateTimeOffset.Now.ToString(_dateFormat)} -- Received message {message}", _logFile);
WriteLineToDisk($"{DateTimeOffset.Now.ToString(_dateFormat)}{message}", _uciLog);
WriteLineToDisk($"{DateTimeOffset.Now.ToString(DateFormat)} -- Received message {message}", LogFile);
WriteLineToDisk($"{DateTimeOffset.Now.ToString(DateFormat)}{message}", UciLog);
try
{
switch (messageType)
{
case UciInit:
Respond(UciOkay);
break;
return true;
case IsReady:
Respond(ReadyOk);
break;
return true;
case NewGame:
_uciLog = $"./{_uciLog}";
_currentBoard = new Board();
_currentBoard.LoadPosition(botMatchStartFens);
break;
_currentBoard.LoadPosition(BotMatchStartFens);
return true;
case Position:
ProcessPositionCommand(message);
break;
return true;
case Go:
ProcessGoCommand(message);
break;
return true;
case Stop:
// message = Quit;
// ProcessStopCommand();
break;
return true;
case Quit:
break;
default:
message = Quit;
break;
return false;
}
}
catch (Exception ex)
{
if (ex.StackTrace != null)
{
var errorMessage = $"{DateTimeOffset.Now.ToString(_dateFormat)} -- {ex.Message}\n{ex.StackTrace}";
WriteLineToDisk(errorMessage, _errorFile);
var errorMessage = $"{DateTimeOffset.Now.ToString(DateFormat)} -- {ex.Message}\n{ex.StackTrace}";
WriteLineToDisk(errorMessage, ErrorFile);

}
}


return false;

}

private void ProcessStopCommand()
Expand All @@ -115,18 +97,9 @@ private void ProcessGoCommand(string message)
}

private void ProcessPositionCommand(string message)
{
// if (message.Split(' ').Length < 3) return;
// var moveStrings = message.Split(' ').Skip(3).ToArray();
// var moves = new Move[moveStrings.Length];
// for (var i = 0; i < moveStrings.Length; i++)
// {
// var str = moveStrings[i];
// moves[i] = MoveUtility.GetMoveFromUCIName(str, _currentBoard);
// _currentBoard.MakeMove(moves[i], false);
// }
{
_currentBoard = new Board();
_currentBoard.LoadPosition(botMatchStartFens);
_currentBoard.LoadPosition(BotMatchStartFens);
var moveStrings = message.Split(' ');
if (moveStrings[^1] == "startpos") return;
for (var i = 3; i < moveStrings.Length; i++)
Expand All @@ -138,7 +111,7 @@ private void ProcessPositionCommand(string message)

private void Respond(string response)
{
WriteLineToDisk($"Responding: {response}", _logFile);
WriteLineToDisk($"Responding: {response}", LogFile);
Console.WriteLine(response);
}
}

0 comments on commit e3f11fd

Please sign in to comment.