< 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: 31
Uncovered lines: 3
Coverable lines: 34
Total lines: 79
Line coverage: 91.1%
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{
 118    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        }
 35
 36        private void ParseStartPos(IList<string> moves)
 37        {
 238            Global.Reset();
 239            uciClient.Board = BoardState.Default();
 240            ParseMoves(moves);
 241        }
 42
 43        private void ParseMoves(IList<string> moves)
 44        {
 1045            foreach (string moveString in moves)
 46            {
 247                Move move = Move.ParseLongAlgebraic(moveString);
 248                move = FindMoveFromMoveList(move);
 49                // Todo : Check move is valid before making move
 250                if (move == Move.NoMove)
 51                {
 052                    CliClient.WriteLine("Invalid Move");
 053                    return;
 54                }
 255                uciClient.Board.MakeMove(move);
 56            }
 357        }
 58
 59        private Move FindMoveFromMoveList(Move move)
 60        {
 261            uciClient.Board.GenerateMoves();
 262            List<Move> moves = uciClient.Board.Moves;
 4063            for (int i = 0; i < moves.Count; ++i)
 64            {
 2065                if (moves[i].Source == move.Source && moves[i].Target == move.Target)
 66                {
 67                    // Cool little trick I saw on Cosette - requires keeping enums on certain numbers
 68                    // So that the we they differ by only one bit
 69                    // Refactor  this concept better maybe - hardcoded in the enum and here for now
 270                    if (move.Type == MoveTypes.Quiet || ((byte)moves[i].Type.Value & ~8) == (byte)move.Type.Value)
 71                    {
 272                        return moves[i];
 73                    }
 74                }
 75            }
 076            return Move.NoMove;
 77        }
 78    }
 79}