-
Notifications
You must be signed in to change notification settings - Fork 1k
Add a UCI implementation #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
b4ux1t3
wants to merge
2
commits into
SebLague:main
Choose a base branch
from
b4ux1t3:uci-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -396,3 +396,5 @@ FodyWeavers.xsd | |
|
|
||
| # JetBrains Rider | ||
| *.sln.iml | ||
| **/out/ | ||
| **/.idea/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net6.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <RootNamespace>ChessChallenge.Bot</RootNamespace> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Chess-Challenge\Chess-Challenge.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // See https://aka.ms/new-console-template for more information | ||
|
|
||
| using ChessChallenge.Bot; | ||
|
|
||
| var engine = new UciEngine(); | ||
| var message = string.Empty; | ||
| while (message != "quit") | ||
| { | ||
| message = Console.ReadLine(); | ||
| if (!string.IsNullOrEmpty(message)) engine.ReceiveCommand(message); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| using ChessChallenge.API; | ||
| using ChessChallenge.Chess; | ||
| using Board = ChessChallenge.Chess.Board; | ||
| using Move = ChessChallenge.Chess.Move; | ||
| using Timer = ChessChallenge.API.Timer; | ||
|
|
||
| namespace ChessChallenge.Bot; | ||
|
|
||
| public class UciEngine | ||
| { | ||
| private const string UciInit = "uci"; | ||
| private const string UciOkay = "uciok"; | ||
| private const string IsReady = "isready"; | ||
| private const string ReadyOk = "readyok"; | ||
| private const string NewGame = "ucinewgame"; | ||
| private const string Position = "position"; | ||
| private const string BestMove = "bestmove"; | ||
| private const string Go = "go"; | ||
| 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 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 void WriteLineToDisk(string line, string file) | ||
| { | ||
| using StreamWriter outputFile = new StreamWriter(file, true); | ||
|
|
||
| outputFile.WriteLine(line); | ||
|
|
||
| } | ||
| public void ReceiveCommand(string message) | ||
| { | ||
| var messageType = message.Split(' ')[0]; | ||
| WriteLineToDisk($"{DateTimeOffset.Now.ToString(_dateFormat)} -- Received message {message}", _logFile); | ||
| WriteLineToDisk($"{DateTimeOffset.Now.ToString(_dateFormat)}{message}", _uciLog); | ||
| try | ||
| { | ||
| switch (messageType) | ||
| { | ||
| case UciInit: | ||
| Respond(UciOkay); | ||
| break; | ||
| case IsReady: | ||
| Respond(ReadyOk); | ||
| break; | ||
| case NewGame: | ||
| _uciLog = $"./{_uciLog}"; | ||
| _currentBoard = new Board(); | ||
| _currentBoard.LoadPosition(botMatchStartFens); | ||
| break; | ||
| case Position: | ||
| ProcessPositionCommand(message); | ||
| break; | ||
| case Go: | ||
| ProcessGoCommand(message); | ||
| break; | ||
| case Stop: | ||
| // message = Quit; | ||
| // ProcessStopCommand(); | ||
| break; | ||
| case Quit: | ||
| break; | ||
| default: | ||
| message = Quit; | ||
| break; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| if (ex.StackTrace != null) | ||
| { | ||
| var errorMessage = $"{DateTimeOffset.Now.ToString(_dateFormat)} -- {ex.Message}\n{ex.StackTrace}"; | ||
| WriteLineToDisk(errorMessage, _errorFile); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private void ProcessStopCommand() | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| private void ProcessGoCommand(string message) | ||
| { | ||
| var split = message.Split(' '); | ||
| var millis = int.Parse(split[2]); | ||
| var newMove = new Move(_bot.Think(new(_currentBoard), new (millis)).RawValue); | ||
| var moveNameUci = MoveUtility.GetMoveNameUCI(newMove); | ||
| Respond($"{BestMove} {moveNameUci}"); | ||
| } | ||
|
|
||
| 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); | ||
| var moveStrings = message.Split(' '); | ||
| if (moveStrings[^1] == "startpos") return; | ||
| for (var i = 3; i < moveStrings.Length; i++) | ||
| { | ||
| var newMove = MoveUtility.GetMoveFromUCIName(moveStrings[i], _currentBoard); | ||
| _currentBoard.MakeMove(newMove, false); | ||
| } | ||
| } | ||
|
|
||
| private void Respond(string response) | ||
| { | ||
| WriteLineToDisk($"Responding: {response}", _logFile); | ||
| Console.WriteLine(response); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.