Пример #1
0
        protected static bool TryParse(string pieceString, out PieceName pieceName, out Position position)
        {
            if (string.IsNullOrWhiteSpace(pieceString))
            {
                throw new ArgumentNullException(nameof(pieceString));
            }

            pieceString = pieceString.Trim();

            try
            {
                Match match = Regex.Match(pieceString, PieceRegex, RegexOptions.IgnoreCase);

                string nameString     = match.Groups[1].Value;
                string positionString = match.Groups[2].Value;

                pieceName = EnumUtils.ParseShortName(nameString);
                position  = Position.Parse(positionString);

                return(true);
            }
            catch (Exception) { }

            pieceName = PieceName.INVALID;
            position  = null;
            return(false);
        }
Пример #2
0
        public static string NormalizeBoardSpaceMoveString(string moveString)
        {
            if (string.IsNullOrWhiteSpace(moveString))
            {
                throw new ArgumentNullException(nameof(moveString));
            }

            moveString = moveString.Trim();

            if (moveString.Equals(Move.PassString, StringComparison.InvariantCultureIgnoreCase))
            {
                return(Move.PassString.ToLower());
            }

            string[] moveStringParts = moveString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            PieceName movingPiece = EnumUtils.ParseShortName(moveStringParts[0]);

            if (moveStringParts.Length == 1)
            {
                return(ToBoardSpacePieceName(movingPiece));
            }

            string targetString   = moveStringParts[1].Trim('-', '/', '\\');
            int    seperatorIndex = moveStringParts[1].IndexOfAny(new char[] { '-', '/', '\\' });

            PieceName targetPiece = EnumUtils.ParseShortName(targetString);

            if (seperatorIndex < 0)
            {
                return(string.Format("{0} {1}", ToBoardSpacePieceName(movingPiece), ToBoardSpacePieceName(targetPiece)));
            }

            char seperator = moveStringParts[1][seperatorIndex];

            if (seperatorIndex == 0)
            {
                // Moving piece on the left-hand side of the target piece
                return(string.Format("{0} {1}{2}", ToBoardSpacePieceName(movingPiece), seperator, ToBoardSpacePieceName(targetPiece)));
            }
            else if (seperatorIndex == targetString.Length)
            {
                // Moving piece on the right-hand side of the target piece
                return(string.Format("{0} {1}{2}", ToBoardSpacePieceName(movingPiece), ToBoardSpacePieceName(targetPiece), seperator));
            }

            return(null);
        }
Пример #3
0
        public static Move ParseMoveString(Board board, string moveString)
        {
            if (null == board)
            {
                throw new ArgumentNullException(nameof(board));
            }

            if (string.IsNullOrWhiteSpace(moveString))
            {
                throw new ArgumentNullException(nameof(moveString));
            }

            moveString = moveString.Trim();

            try
            {
                // Attempt to parse as an algebraic move
                return(new Move(moveString));
            }
            catch (Exception) { }

            string[] moveStringParts = moveString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            PieceName movingPiece = EnumUtils.ParseShortName(moveStringParts[0]);

            if (moveStringParts.Length == 1)
            {
                if (board.BoardState == BoardState.NotStarted)
                {
                    // First move is on the origin
                    return(new Move(movingPiece, Position.Origin));
                }

                throw new ArgumentException("You must specify a target piece.", nameof(moveString));
            }

            string targetString   = moveStringParts[1].Trim('-', '/', '\\');
            int    seperatorIndex = moveStringParts[1].IndexOfAny(new char[] { '-', '/', '\\' });

            PieceName targetPiece = EnumUtils.ParseShortName(targetString);

            if (seperatorIndex < 0)
            {
                // Putting a piece on top of another
                return(new Move(movingPiece, board.GetPiecePosition(targetPiece).GetAbove()));
            }

            char seperator = moveStringParts[1][seperatorIndex];

            Position targetPosition = board.GetPiecePosition(targetPiece);

            if (seperatorIndex == 0)
            {
                // Moving piece on the left-hand side of the target piece
                switch (seperator)
                {
                case '-':
                    return(new Move(movingPiece, targetPosition.NeighborAt(Direction.UpLeft)));

                case '/':
                    return(new Move(movingPiece, targetPosition.NeighborAt(Direction.DownLeft)));

                case '\\':
                    return(new Move(movingPiece, targetPosition.NeighborAt(Direction.Up)));
                }
            }
            else if (seperatorIndex == targetString.Length)
            {
                // Moving piece on the right-hand side of the target piece
                switch (seperator)
                {
                case '-':
                    return(new Move(movingPiece, targetPosition.NeighborAt(Direction.DownRight)));

                case '/':
                    return(new Move(movingPiece, targetPosition.NeighborAt(Direction.UpRight)));

                case '\\':
                    return(new Move(movingPiece, targetPosition.NeighborAt(Direction.Down)));
                }
            }

            return(null);
        }