Exemplo n.º 1
0
        public static Move MoveFromUciNotation(string uciMoveNotation)
        {
            if (uciMoveNotation.Length < 4)
            {
                throw new ArgumentException($"Long algebraic notation expected. '{uciMoveNotation}' is too short!");
            }
            if (uciMoveNotation.Length > 5)
            {
                throw new ArgumentException($"Long algebraic notation expected. '{uciMoveNotation}' is too long!");
            }

            //expected format is the long algebraic notation without piece names
https:      //en.wikipedia.org/wiki/Algebraic_notation_(chess)
            //Examples: e2e4, e7e5, e1g1(white short castling), e7e8q(for promotion)
            string fromSquare = uciMoveNotation.Substring(0, 2);
            string toSquare  = uciMoveNotation.Substring(2, 2);
            int    fromIndex = Notation.ToSquareIndex(fromSquare);
            int    toIndex   = Notation.ToSquareIndex(toSquare);


            int flags = 0;

            //the presence of a 5th character should mean promotion
            if (uciMoveNotation.Length == 5)
            {
                //promotion flags
                char promotion = uciMoveNotation[4];
                flags = promotion switch
                {
                    'N' => Flag.PromoteToKnight,
                    'B' => Flag.PromoteToBishop,
                    'R' => Flag.PromoteToRook,
                    'Q' => Flag.PromoteToQueen,
                    'n' => Flag.PromoteToKnight,
                    'b' => Flag.PromoteToBishop,
                    'r' => Flag.PromoteToRook,
                    'q' => Flag.PromoteToQueen,
                    _ => throw new NotImplementedException()
                };
            }

            return(new Move(fromIndex, toIndex, flags));
        }