示例#1
0
        public override bool IsLegalMove(Move move, ChessGame game)
        {
            ChessUtility.ThrowIfNull(move, "move");
            ChessUtility.ThrowIfNull(game, "game");
            BoardPosition origin      = move.OriginalPosition;
            BoardPosition destination = move.NewPosition;

            BoardDistance posDelta = new BoardDistance(origin, destination);

            if (posDelta.X != posDelta.Y)
            {
                return(false);
            }
            bool increasingRank = destination.Rank > origin.Rank;
            bool increasingFile = (int)destination.File > (int)origin.File;

            for (int f = (int)origin.File + (increasingFile ? 1 : -1), r = origin.Rank + (increasingRank ? 1 : -1);
                 increasingFile?f <(int)destination.File : f> (int) destination.File;
                 f += increasingFile ? 1 : -1, r += increasingRank ? 1 : -1)
            {
                if (game.GetPieceAt((ChessFile)f, r) != null)
                {
                    return(false);
                }
            }
            return(true);
        }
示例#2
0
        public override bool IsLegalMove(Move move, ChessGame game)
        {
            ChessUtility.ThrowIfNull(move, "move");
            ChessUtility.ThrowIfNull(game, "game");
            BoardPosition origin      = move.OriginalPosition;
            BoardPosition destination = move.NewPosition;

            BoardDistance posDelta = new BoardDistance(origin, destination);

            if ((posDelta.X != 2 || posDelta.Y != 1) && (posDelta.X != 1 || posDelta.Y != 2))
            {
                return(false);
            }
            return(true);
        }
示例#3
0
        public override bool IsLegalMove(Move move, ChessGame game)
        {
            ChessUtility.ThrowIfNull(move, "move");
            BoardPosition origin      = move.OriginalPosition;
            BoardPosition destination = move.NewPosition;
            BoardDistance distance    = new BoardDistance(origin, destination);

            if ((distance.X != 1 || distance.Y != 1) &&
                (distance.X != 0 || distance.Y != 1) &&
                (distance.X != 1 || distance.Y != 0) &&
                (distance.X != 2 || distance.Y != 0))
            {
                return(false);
            }
            if (distance.X != 2)
            {
                return(true);
            }
            return(CanCastle(origin, destination, game));
        }
示例#4
0
        public override bool IsLegalMove(Move move, ChessGame game)
        {
            ChessUtility.ThrowIfNull(move, "move");
            ChessUtility.ThrowIfNull(game, "game");
            BoardPosition origin      = move.OriginalPosition;
            BoardPosition destination = move.NewPosition;

            ChessPiece promotion = null;

            if (move.Promotion.HasValue && ValidPromotionPieces.Contains(move.Promotion.Value))
            {
                promotion = game.MapPgnCharToPiece(char.ToUpper(move.Promotion.Value), move.Player);
            }
            BoardDistance posDelta = new BoardDistance(origin, destination);

            if ((posDelta.X != 0 || posDelta.Y != 1) && (posDelta.X != 1 || posDelta.Y != 1) &&
                (posDelta.X != 0 || posDelta.Y != 2))
            {
                return(false);
            }
            if (Owner == ChessPlayer.White)
            {
                if (origin.Rank > destination.Rank)
                {
                    return(false);
                }
                if (destination.Rank == 8)
                {
                    if (promotion == null)
                    {
                        return(false);
                    }
                    if (promotion.Owner != ChessPlayer.White)
                    {
                        return(false);
                    }
                    if (!ValidPromotionPieces.Contains(promotion.GetFENLetter()))
                    {
                        return(false);
                    }
                }
            }
            if (Owner == ChessPlayer.Black)
            {
                if (origin.Rank < destination.Rank)
                {
                    return(false);
                }
                if (destination.Rank == 1)
                {
                    if (promotion == null)
                    {
                        return(false);
                    }
                    if (promotion.Owner != ChessPlayer.Black)
                    {
                        return(false);
                    }
                    if (!ValidPromotionPieces.Contains(promotion.GetFENLetter()))
                    {
                        return(false);
                    }
                }
            }
            bool checkEnPassant = false;

            if (posDelta.Y == 2)
            {
                if ((origin.Rank != 2 && Owner == ChessPlayer.White) ||
                    (origin.Rank != 7 && Owner == ChessPlayer.Black))
                {
                    return(false);
                }
                if (origin.Rank == 2 && game.GetPieceAt(origin.File, 3) != null)
                {
                    return(false);
                }
                if (origin.Rank == 7 && game.GetPieceAt(origin.File, 6) != null)
                {
                    return(false);
                }
            }
            ChessPiece pieceAtDestination = game.GetPieceAt(destination);

            if (posDelta.X == 0 && (posDelta.Y == 1 || posDelta.Y == 2))
            {
                if (pieceAtDestination != null)
                {
                    return(false);
                }
            }
            else
            {
                if (pieceAtDestination == null)
                {
                    checkEnPassant = true;
                }
                else if (pieceAtDestination.Owner == Owner)
                {
                    return(false);
                }
            }
            if (checkEnPassant)
            {
                ReadOnlyCollection <DetailedMove> _moves = game.Moves;
                if (_moves.Count == 0)
                {
                    return(false);
                }
                if ((origin.Rank != 5 && Owner == ChessPlayer.White) ||
                    (origin.Rank != 4 && Owner == ChessPlayer.Black))
                {
                    return(false);
                }
                Move latestMove = _moves[_moves.Count - 1];
                if (latestMove.Player != ChessUtility.GetOpponentOf(Owner))
                {
                    return(false);
                }
                if (!(game.GetPieceAt(latestMove.NewPosition) is Pawn))
                {
                    return(false);
                }
                if (game.GetPieceAt(latestMove.NewPosition).Owner == Owner)
                {
                    return(false);
                }
                if (Owner == ChessPlayer.White)
                {
                    if (latestMove.OriginalPosition.Rank != 7 || latestMove.NewPosition.Rank != 5)
                    {
                        return(false);
                    }
                }
                else // (m.Player == Players.Black)
                {
                    if (latestMove.OriginalPosition.Rank != 2 || latestMove.NewPosition.Rank != 4)
                    {
                        return(false);
                    }
                }
                if (destination.File != latestMove.NewPosition.File)
                {
                    return(false);
                }
            }
            return(true);
        }