private static Move DetermineMove(Board board, UCIChessMove uciMove)
        {
            string from      = uciMove.From;
            string to        = uciMove.To;
            var    fromPos   = Position.Parse(from);
            var    toPos     = Position.Parse(to);
            var    fromPiece = board[fromPos];

            int fileDiff = Math.Abs((int)fromPiece.Position.File - (int)toPos.File);

            if (fromPiece.Type == PieceType.King && (fileDiff == 3 || fileDiff == 4))
            {
                King king = (King)fromPiece;
                Rook rook;
                if (fileDiff == 4)
                {
                    rook = (Rook)board[File.A, fromPiece.Position.Rank];
                }
                else
                {
                    rook = (Rook)board[File.H, fromPiece.Position.Rank];
                }

                return(MoveBuilder.CreateCastleMove(king, rook));
            }

            if (uciMove.PromotionChar != null)
            {
                string promotionChar = uciMove.PromotionChar.ToUpper();
                var    promotionType = (from p in (PieceType[])Enum.GetValues(typeof(PieceType))
                                        where p.ToNotationLetter().ToUpper() == promotionChar.ToUpper()
                                        select p).First();
                var promotionMoves = MoveBuilder.CreatePromotionMoves((Pawn)fromPiece, toPos, board[toPos] != null);
                return((from m in promotionMoves
                        where m.Promotion.NewPieceType.Type == promotionType
                        select m).First());
            }

            // Check for en passant
            var existingPieceRank = fromPiece.Position.Rank;
            var isCorrectRank     = fromPiece.Color == PieceColor.Black ? existingPieceRank == Rank._4 : existingPieceRank == Rank._5;

            try
            {
                int vertDir          = fromPiece.Color == PieceColor.White ? -1 : 1;
                var existingPieceTo  = board[toPos];
                var capturedPiecePos = toPos.MoveVert(vertDir);
                var capturedPiece    = board[capturedPiecePos];
                if (capturedPiece.Color == fromPiece.Color.Opposite() &&
                    capturedPiece.MoveHistory.Count == 1)
                {
                    // This is an en passant
                    return(MoveBuilder.CreateEnPassantMove((Pawn)fromPiece, toPos, capturedPiecePos));
                }
            }
            catch { }

            // Only other option is that this is a normal move.
            return(MoveBuilder.CreateNormalMove(fromPiece, toPos, board[toPos] != null));
        }
Exemple #2
0
        private Move CheckCastleWithRookLocation(Board board, Position rookPos)
        {
            var pieceInPos = board[rookPos];

            if (pieceInPos == null)
            {
                return(null);
            }
            if (pieceInPos.Type != PieceType.Rook)
            {
                return(null);
            }
            if (pieceInPos.Color != this.Color)
            {
                return(null);
            }
            if (pieceInPos.MoveHistory != null && pieceInPos.MoveHistory.Count > 0)
            {
                return(null);
            }

            int startFile = (int)rookPos.File;
            int myFile    = (int)Position.File;
            int increment = (myFile > startFile) ? 1 : -1;

            // Rook is eligible. Check pieces in between.
            for (int f = startFile + increment; f != myFile; f += increment)
            {
                var pieceInFile = board[(File)f, Position.Rank];
                if (pieceInFile != null)
                {
                    return(null);
                }
                bool kingWillPassThroughThisSquare = Math.Abs(myFile - f) <= 2;
                if (kingWillPassThroughThisSquare)
                {
                    var fakeMove = MoveBuilder.CreateNormalMove(this,
                                                                new Position((File)f, Position.Rank), false);
                    if (board.DoesMovePutKingInCheck(this.Color, fakeMove))
                    {
                        return(null);
                    }
                }
            }

            return(MoveBuilder.CreateCastleMove(this, (Rook)pieceInPos));
        }