< Summary

Information
Class: Rudim.CLI.UCI.PositionCommand
Assembly: Rudim
File(s): /home/runner/work/rudim/rudim/Rudim/CLI/UCI/PositionCommand.cs
Line coverage
91%
Covered lines: 33
Uncovered lines: 3
Coverable lines: 36
Total lines: 81
Line coverage: 91.6%
Branch coverage
83%
Covered branches: 15
Total branches: 18
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Run(...)100%44100%
ParseFen(...)100%11100%
ParseStartPos(...)100%11100%
ParseMoves(...)75%4475%
FindMoveFromMoveList(...)80%101085.71%

File(s)

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

#LineLine coverage
 1using Rudim.Board;
 2using Rudim.Common;
 3using System.Collections.Generic;
 4using System.Linq;
 5
 6namespace Rudim.CLI.UCI
 7{
 168    public class PositionCommand(UciClient uciClient) : IUciCommand
 9    {
 10        public void Run(string[] parameters)
 11        {
 312            string position = parameters[0];
 313            List<string> positionParameters = parameters.Skip(1).ToList();
 14
 315            if (position == "startpos")
 16            {
 217                List<string> moves = positionParameters.Skip(1).ToList();
 218                ParseStartPos(moves);
 19            }
 20
 321            if (position == "fen")
 22            {
 123                string fen = string.Join(" ", positionParameters.Take(6));
 124                List<string> moves = positionParameters.Skip(7).ToList();
 125                ParseFen(fen, moves);
 26            }
 327        }
 28
 29        private void ParseFen(string fen, IList<string> moves)
 30        {
 131            Global.Reset();
 132            uciClient.Board = BoardState.ParseFEN(fen);
 133            ParseMoves(moves);
 134            Global.SetReady();
 135        }
 36
 37        private void ParseStartPos(IList<string> moves)
 38        {
 239            Global.Reset();
 240            uciClient.Board = BoardState.Default();
 241            ParseMoves(moves);
 242            Global.SetReady();
 243        }
 44
 45        private void ParseMoves(IList<string> moves)
 46        {
 1047            foreach (string moveString in moves)
 48            {
 249                Move move = Move.ParseLongAlgebraic(moveString);
 250                move = FindMoveFromMoveList(move);
 51                // Todo : Check move is valid before making move
 252                if (move == Move.NoMove)
 53                {
 054                    CliClient.WriteLine("Invalid Move");
 055                    return;
 56                }
 257                uciClient.Board.MakeMove(move);
 58            }
 359        }
 60
 61        private Move FindMoveFromMoveList(Move move)
 62        {
 263            uciClient.Board.GenerateMoves();
 264            List<Move> moves = uciClient.Board.Moves;
 4065            for (int i = 0; i < moves.Count; ++i)
 66            {
 2067                if (moves[i].Source == move.Source && moves[i].Target == move.Target)
 68                {
 69                    // Cool little trick I saw on Cosette - requires keeping enums on certain numbers
 70                    // So that the we they differ by only one bit
 71                    // Refactor  this concept better maybe - hardcoded in the enum and here for now
 272                    if (move.Type == MoveTypes.Quiet || ((byte)moves[i].Type.Value & ~8) == (byte)move.Type.Value)
 73                    {
 274                        return moves[i];
 75                    }
 76                }
 77            }
 078            return Move.NoMove;
 79        }
 80    }
 81}