| | | 1 | | using Rudim.Common; |
| | | 2 | | using System; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | |
| | | 7 | | namespace Rudim.CLI.UCI |
| | | 8 | | { |
| | 8 | 9 | | internal class GoCommand(UciClient uciClient) : IUciCommand |
| | | 10 | | { |
| | | 11 | | private CancellationTokenSource _currentSearch = null; |
| | 8 | 12 | | private Move _bestMove = Move.NoMove; |
| | | 13 | | |
| | | 14 | | public void StopSearch() |
| | | 15 | | { |
| | 0 | 16 | | if (_currentSearch != null) |
| | | 17 | | { |
| | 0 | 18 | | _currentSearch.Cancel(); |
| | 0 | 19 | | if (_bestMove != Move.NoMove) |
| | | 20 | | { |
| | 0 | 21 | | OutputBestMove(_bestMove); |
| | | 22 | | } |
| | | 23 | | } |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | private void OutputBestMove(Move move) |
| | | 27 | | { |
| | 0 | 28 | | CliClient.WriteLine("bestmove " + move.Source + move.Target + move.GetPromotionChar()); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async void Run(string[] parameters) |
| | | 32 | | { |
| | | 33 | | // Cancel any existing search |
| | 0 | 34 | | _currentSearch?.Cancel(); |
| | 0 | 35 | | _currentSearch = new CancellationTokenSource(); |
| | 0 | 36 | | _bestMove = Move.NoMove; |
| | | 37 | | |
| | 0 | 38 | | int depth = GetParameter("depth", parameters, 8); |
| | 0 | 39 | | int winc = GetParameter("winc", parameters, -1); |
| | 0 | 40 | | int binc = GetParameter("binc", parameters, -1); |
| | 0 | 41 | | int wtime = GetParameter("wtime", parameters, -1); |
| | 0 | 42 | | int btime = GetParameter("btime", parameters, -1); |
| | 0 | 43 | | int movetime = GetParameter("movetime", parameters, -1); |
| | 0 | 44 | | bool infinite = GetOptionlessParameter("infinite", parameters); // Not yet implemented |
| | | 45 | | |
| | 0 | 46 | | int clock = uciClient.Board.SideToMove == Side.White ? wtime : btime; |
| | 0 | 47 | | int increment = uciClient.Board.SideToMove == Side.White ? winc : binc; |
| | 0 | 48 | | int allottedTime = movetime == -1 ? (clock == -1 ? -1 : TimeManagement.CalculateMoveTime(uciClient.Board.Mov |
| | | 49 | | |
| | | 50 | | |
| | 0 | 51 | | if (!infinite) |
| | | 52 | | { |
| | 0 | 53 | | if (allottedTime == -1) |
| | | 54 | | { |
| | 0 | 55 | | _bestMove = await Task.Run(() => uciClient.Board.FindBestMove(depth, _currentSearch.Token, ref uciCl |
| | | 56 | | } |
| | | 57 | | else |
| | | 58 | | { |
| | 0 | 59 | | Task<Move> searchTask = Task.Run(() => uciClient.Board.FindBestMove(Constants.MaxSearchDepth, _curre |
| | 0 | 60 | | Task timeoutTask = Task.Delay(allottedTime); |
| | | 61 | | |
| | 0 | 62 | | if (await Task.WhenAny(searchTask, timeoutTask) == timeoutTask) |
| | | 63 | | { |
| | 0 | 64 | | await _currentSearch.CancelAsync(); |
| | 0 | 65 | | _bestMove = await searchTask; |
| | | 66 | | } |
| | | 67 | | else |
| | | 68 | | { |
| | 0 | 69 | | _bestMove = await searchTask; |
| | | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | OutputBestMove(_bestMove); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static bool GetOptionlessParameter(string name, string[] parameters) |
| | | 79 | | { |
| | 0 | 80 | | return parameters.Contains(name); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | private static T GetParameter<T>(string name, string[] parameters, T fallback) |
| | | 84 | | { |
| | 0 | 85 | | for (int i = 0; i < parameters.Length; ++i) |
| | | 86 | | { |
| | 0 | 87 | | if (parameters[i] == name) |
| | | 88 | | { |
| | 0 | 89 | | return (T)Convert.ChangeType(parameters[i + 1], typeof(T)); |
| | | 90 | | } |
| | | 91 | | } |
| | 0 | 92 | | return fallback; |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | } |