< Summary

Information
Class: Rudim.CLI.UCI.GoCommand
Assembly: Rudim
File(s): /home/runner/work/rudim/rudim/Rudim/CLI/UCI/GoCommand.cs
Line coverage
5%
Covered lines: 2
Uncovered lines: 37
Coverable lines: 39
Total lines: 95
Line coverage: 5.1%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
StopSearch()0%2040%
OutputBestMove(...)100%210%
Run()0%272160%
GetOptionlessParameter(...)100%210%
GetParameter(...)0%2040%

File(s)

/home/runner/work/rudim/rudim/Rudim/CLI/UCI/GoCommand.cs

#LineLine coverage
 1using Rudim.Common;
 2using System;
 3using System.Linq;
 4using System.Threading;
 5using System.Threading.Tasks;
 6
 7namespace Rudim.CLI.UCI
 8{
 89    internal class GoCommand(UciClient uciClient) : IUciCommand
 10    {
 11        private CancellationTokenSource _currentSearch = null;
 812        private Move _bestMove = Move.NoMove;
 13
 14        public void StopSearch()
 15        {
 016            if (_currentSearch != null)
 17            {
 018                _currentSearch.Cancel();
 019                if (_bestMove != Move.NoMove)
 20                {
 021                    OutputBestMove(_bestMove);
 22                }
 23            }
 024        }
 25
 26        private void OutputBestMove(Move move)
 27        {
 028            CliClient.WriteLine("bestmove " + move.Source + move.Target + move.GetPromotionChar());
 029        }
 30
 31        public async void Run(string[] parameters)
 32        {
 33            // Cancel any existing search
 034            _currentSearch?.Cancel();
 035            _currentSearch = new CancellationTokenSource();
 036            _bestMove = Move.NoMove;
 37
 038            int depth = GetParameter("depth", parameters, 8);
 039            int winc = GetParameter("winc", parameters, -1);
 040            int binc = GetParameter("binc", parameters, -1);
 041            int wtime = GetParameter("wtime", parameters, -1);
 042            int btime = GetParameter("btime", parameters, -1);
 043            int movetime = GetParameter("movetime", parameters, -1);
 044            bool infinite = GetOptionlessParameter("infinite", parameters); // Not yet implemented
 45
 046            int clock = uciClient.Board.SideToMove == Side.White ? wtime : btime;
 047            int increment = uciClient.Board.SideToMove == Side.White ? winc : binc;
 048            int allottedTime = movetime == -1 ? (clock == -1 ? -1 : TimeManagement.CalculateMoveTime(uciClient.Board.Mov
 49
 50
 051            if (!infinite)
 52            {
 053                if (allottedTime == -1)
 54                {
 055                    _bestMove = await Task.Run(() => uciClient.Board.FindBestMove(depth, _currentSearch.Token, ref uciCl
 56                }
 57                else
 58                {
 059                    Task<Move> searchTask = Task.Run(() => uciClient.Board.FindBestMove(Constants.MaxSearchDepth, _curre
 060                    Task timeoutTask = Task.Delay(allottedTime);
 61
 062                    if (await Task.WhenAny(searchTask, timeoutTask) == timeoutTask)
 63                    {
 064                        await _currentSearch.CancelAsync();
 065                        _bestMove = await searchTask;
 66                    }
 67                    else
 68                    {
 069                        _bestMove = await searchTask;
 70                    }
 071                }
 72
 073                OutputBestMove(_bestMove);
 74            }
 75
 076        }
 77
 78        private static bool GetOptionlessParameter(string name, string[] parameters)
 79        {
 080            return parameters.Contains(name);
 81        }
 82
 83        private static T GetParameter<T>(string name, string[] parameters, T fallback)
 84        {
 085            for (int i = 0; i < parameters.Length; ++i)
 86            {
 087                if (parameters[i] == name)
 88                {
 089                    return (T)Convert.ChangeType(parameters[i + 1], typeof(T));
 90                }
 91            }
 092            return fallback;
 93        }
 94    }
 95}