Esempio n. 1
0
        private static IEnumerable <SquareCoordinate> GetPawnMobility(SquareCoordinate square, Board board, Color pieceColor)
        {
            var ret = new List <SquareCoordinate>();

            SquareCoordinate?allowedSquare = null;

            if (DoubleForwardMoveAllowed(board, pieceColor, square, out allowedSquare))
            {
                ret.Add(allowedSquare.Value);
            }

            if (SingleForwardMoveAllowed(board, pieceColor, square, out allowedSquare))
            {
                ret.Add(allowedSquare.Value);
            }

            if (IsPawnCaptureAllowed(board, pieceColor, square, square.File - 1, out allowedSquare))
            {
                ret.Add(allowedSquare.Value);
            }

            if (IsPawnCaptureAllowed(board, pieceColor, square, square.File + 1, out allowedSquare))
            {
                ret.Add(allowedSquare.Value);
            }

            return(ret);
        }
Esempio n. 2
0
 private bool IsNotCheckOnKingMove(Board board, SquareCoordinate startingCoordinate, SquareCoordinate endingCoordinate, Color kingColor)
 {
     return(!board.IsControlledByColor(endingCoordinate, kingColor.OpponentColor) &&
            !board.IsInOpponentControlAfterMove(board[startingCoordinate],
                                                board[endingCoordinate],
                                                board[endingCoordinate]));
 }
Esempio n. 3
0
        private static void TryAddSquare(List <SquareCoordinate> ret,
                                         SquareCoordinate startingSquare,
                                         int newRank,
                                         int newFile,
                                         Board board,
                                         Color pieceColor,
                                         SquareInfluenceType availabilityType)
        {
            if (!IsValidPosition(newRank, newFile))
            {
                return;
            }

            var newSquare = new SquareCoordinate(newRank, newFile);

            switch (availabilityType)
            {
            case SquareInfluenceType.Control:
                ret.Add(newSquare);
                break;

            case SquareInfluenceType.Mobility:
                if (IsInPieceMobility(board, startingSquare, newSquare, pieceColor))
                {
                    ret.Add(newSquare);
                }
                break;

            default:
                throw new NotImplementedException();
            }
        }
Esempio n. 4
0
        private static IEnumerable <SquareCoordinate> GetStraightInBetweenPosition(SquareCoordinate startingCoordinate, SquareCoordinate endingCoordinate)
        {
            var ret = new List <SquareCoordinate>();

            var fileShift = endingCoordinate.File - startingCoordinate.File;

            if (fileShift != 0)
            {
                var shiftDirection = Math.Sign(fileShift);
                var shiftAbs       = Math.Abs(fileShift);
                for (int i = 1; i < shiftAbs; i++)
                {
                    var newFile = startingCoordinate.File + (shiftDirection * i);
                    ret.Add(new SquareCoordinate(startingCoordinate.Rank, newFile));
                }
            }
            else
            {
                var rankShift      = endingCoordinate.Rank - startingCoordinate.Rank;
                var shiftDirection = Math.Sign(rankShift);
                var shiftAbs       = Math.Abs(rankShift);
                for (int i = 1; i < shiftAbs; i++)
                {
                    var newRank = startingCoordinate.Rank + (shiftDirection * i);
                    ret.Add(new SquareCoordinate(newRank, startingCoordinate.File));
                }
            }

            return(ret);
        }
Esempio n. 5
0
        private static bool EnPassantAllowed(Board board, Color color, SquareCoordinate square, out SquareCoordinate?allowedSquare)
        {
            if (square.Rank == color.EnPassantStartingRank)
            {
                var lastMove = board.GetLastMove();
                if (lastMove != null)
                {
                    var lastMoveEnPassantInfo = lastMove.GetAllowEnPassantOnNextMoveInfo();
                    if (lastMoveEnPassantInfo.AllowEnPassant)
                    {
                        var leftCaptureFile  = square.File - 1;
                        var rightCaptureFile = square.File + 1;

                        if (lastMoveEnPassantInfo.IsEnPassantFile(leftCaptureFile))
                        {
                            allowedSquare = new SquareCoordinate(color.EnPassantStartingRank, leftCaptureFile);
                            return(true);
                        }
                        else if (lastMoveEnPassantInfo.IsEnPassantFile(rightCaptureFile))
                        {
                            allowedSquare = new SquareCoordinate(color.EnPassantStartingRank, rightCaptureFile);
                            return(true);
                        }
                    }
                }
            }

            allowedSquare = null;
            return(false);
        }
Esempio n. 6
0
        internal static bool IsOnTwoOneL(this SquareCoordinate startingCoordinate, SquareCoordinate endingCoordinate)
        {
            var rankIncrement = Math.Abs(startingCoordinate.Rank - endingCoordinate.Rank);
            var fileIncrement = Math.Abs(startingCoordinate.File - endingCoordinate.File);

            return(IsATwoOneL(rankIncrement, fileIncrement) ||
                   IsATwoOneL(fileIncrement, rankIncrement));
        }
Esempio n. 7
0
        private bool IsNotBreakingAbsolutePin(Board board, SquareCoordinate startingCoordinate, SquareCoordinate endingCoordinate, Color movingSide)
        {
            var kingPosition = board.GetKingSquare(movingSide);

            return(!board.IsInOpponentControlAfterMove(board[startingCoordinate],
                                                       board[endingCoordinate],
                                                       kingPosition));
        }
Esempio n. 8
0
        internal static bool IsKingValidMove(SquareCoordinate startingCoordinate, SquareCoordinate endingCoordinate)
        {
            var rankDiff = Math.Abs(startingCoordinate.Rank - endingCoordinate.Rank);
            var fileDiff = Math.Abs(startingCoordinate.File - endingCoordinate.File);

            return((rankDiff != 0 || fileDiff != 0) &&
                   (rankDiff == 1 || rankDiff == 0) &&
                   (fileDiff == 1 || fileDiff == 0));
        }
Esempio n. 9
0
 internal ThreefoldRepetitionPositionUnit(SquareCoordinate startingSquare,
                                          SquareCoordinate endingSquare,
                                          Piece piece,
                                          Color nextTurn)
 {
     _startingSquare = startingSquare;
     _endingSquare   = endingSquare;
     _piece          = piece;
     _nextTurn       = nextTurn;
 }
Esempio n. 10
0
        private static bool SingleForwardMoveAllowed(Board board, Color color, SquareCoordinate square, out SquareCoordinate?allowedSquare)
        {
            var singleRankMove = square.Rank + color.MovingDirection;

            var isAllowed = !board.IsAnyPieceInSquare(singleRankMove, square.File) &&
                            IsValidPosition(singleRankMove, square.File);

            allowedSquare = isAllowed
                ? new SquareCoordinate(singleRankMove, square.File)
                : (SquareCoordinate?)null;

            return(isAllowed);
        }
Esempio n. 11
0
        internal static bool IsOnDiagonal(this SquareCoordinate startingCoordinate, SquareCoordinate endingCoordinate)
        {
            var rankIncrement = startingCoordinate.Rank - endingCoordinate.Rank;
            var fileIncrement = startingCoordinate.File - endingCoordinate.File;

            if (fileIncrement == 0)
            {
                return(false);
            }

            return(fileIncrement != 0 &&
                   Math.Abs((double)rankIncrement / fileIncrement) == 1.00);
        }
Esempio n. 12
0
        private static bool IsPawnCaptureAllowed(Board board, Color color, SquareCoordinate square, int captureFile, out SquareCoordinate?allowedSquare)
        {
            var endingRank = square.Rank + color.MovingDirection;

            var isAllowed = IsValidPosition(square.Rank, captureFile) &&
                            board.IsAnyOpponentPieceInSquare(new SquareCoordinate(endingRank, captureFile), color);

            allowedSquare = isAllowed
                ? new SquareCoordinate(endingRank, captureFile)
                : (SquareCoordinate?)null;

            return(isAllowed);
        }
Esempio n. 13
0
        internal static IEnumerable <SquareCoordinate> GetKingControl(SquareCoordinate startingCoordinate)
        {
            var ret = new List <SquareCoordinate>();

            SquareCoordinate?newPos = null;

            //Old square
            ret.Add(startingCoordinate);
            //Upper left
            if (TryGetNewPosition(startingCoordinate.Rank + 1, startingCoordinate.File - 1, out newPos))
            {
                ret.Add(newPos.Value);
            }
            //Upper middle
            if (TryGetNewPosition(startingCoordinate.Rank + 1, startingCoordinate.File, out newPos))
            {
                ret.Add(newPos.Value);
            }
            //Upper right
            if (TryGetNewPosition(startingCoordinate.Rank + 1, startingCoordinate.File + 1, out newPos))
            {
                ret.Add(newPos.Value);
            }
            //Middle right
            if (TryGetNewPosition(startingCoordinate.Rank, startingCoordinate.File + 1, out newPos))
            {
                ret.Add(newPos.Value);
            }
            //Middle left
            if (TryGetNewPosition(startingCoordinate.Rank, startingCoordinate.File - 1, out newPos))
            {
                ret.Add(newPos.Value);
            }
            //Bottom right
            if (TryGetNewPosition(startingCoordinate.Rank - 1, startingCoordinate.File + 1, out newPos))
            {
                ret.Add(newPos.Value);
            }
            //Bottom middle
            if (TryGetNewPosition(startingCoordinate.Rank - 1, startingCoordinate.File, out newPos))
            {
                ret.Add(newPos.Value);
            }
            //Bottom left
            if (TryGetNewPosition(startingCoordinate.Rank - 1, startingCoordinate.File - 1, out newPos))
            {
                ret.Add(newPos.Value);
            }
            return(ret);
        }
Esempio n. 14
0
        internal static IEnumerable <SquareCoordinate> GetPawnAvailability(SquareCoordinate startingCoordinate, Board board, Color pieceColor, SquareInfluenceType availabilityType)
        {
            switch (availabilityType)
            {
            case SquareInfluenceType.Control:
                return(GetPawnControl(startingCoordinate, board, pieceColor));

            case SquareInfluenceType.Mobility:
                return(GetPawnMobility(startingCoordinate, board, pieceColor));

            default:
                throw new NotImplementedException();
            }
        }
Esempio n. 15
0
        internal static IEnumerable <SquareCoordinate> GetAllInBetweenSquares(this SquareCoordinate startingCoordinate, SquareCoordinate endingCoordinate)
        {
            var ret = new List <SquareCoordinate>();

            if (startingCoordinate.IsOnDiagonal(endingCoordinate))
            {
                Extensions.AddRange(ret, GetDiagonalInBetweenPositions(startingCoordinate, endingCoordinate));
            }
            if (startingCoordinate.IsOnRankOrFile(endingCoordinate))
            {
                Extensions.AddRange(ret, GetStraightInBetweenPosition(startingCoordinate, endingCoordinate));
            }

            return(ret);
        }
Esempio n. 16
0
        private static bool DoubleForwardMoveAllowed(Board board, Color color, SquareCoordinate square, out SquareCoordinate?allowedSquare)
        {
            var singleRankMove = square.Rank + color.MovingDirection;
            var doubleRankMove = square.Rank + color.MovingDirection * 2;

            var isAllowed = square.Rank == color.PawnFirstRank &&
                            !board.IsAnyPieceInSquare(singleRankMove, square.File) &&
                            !board.IsAnyPieceInSquare(doubleRankMove, square.File);

            allowedSquare = isAllowed
                ? new SquareCoordinate(doubleRankMove, square.File)
                : (SquareCoordinate?)null;

            return(isAllowed);
        }
Esempio n. 17
0
        internal static IEnumerable <SquareCoordinate> GetLineAvailability(SquareCoordinate startingCoordinate, Board board, Color pieceColor, SquareInfluenceType availabilityType)
        {
            var ret = new List <SquareCoordinate>();

            //Upper line
            for (int i = 1; i < 8; i++)
            {
                var rank = startingCoordinate.Rank + i;
                var file = startingCoordinate.File;
                if (TryAddPieceGetIsLast(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType))
                {
                    break;
                }
            }
            //Bottom line
            for (int i = 1; i < 8; i++)
            {
                var rank = startingCoordinate.Rank - i;
                var file = startingCoordinate.File;
                if (TryAddPieceGetIsLast(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType))
                {
                    break;
                }
            }
            //Right line
            for (int i = 1; i < 8; i++)
            {
                var rank = startingCoordinate.Rank;
                var file = startingCoordinate.File + i;
                if (TryAddPieceGetIsLast(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType))
                {
                    break;
                }
            }
            //Left line
            for (int i = 1; i < 8; i++)
            {
                var rank = startingCoordinate.Rank;
                var file = startingCoordinate.File - i;
                if (TryAddPieceGetIsLast(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType))
                {
                    break;
                }
            }

            return(ret);
        }
Esempio n. 18
0
        private static bool TryAddPieceGetIsLast(List <SquareCoordinate> ret,
                                                 SquareCoordinate startingSquare,
                                                 int newRank,
                                                 int newFile,
                                                 Board board,
                                                 Color pieceColor,
                                                 SquareInfluenceType availabilityType)
        {
            if (!IsValidPosition(newRank, newFile))
            {
                return(true);
            }

            TryAddSquare(ret, startingSquare, newRank, newFile, board, pieceColor, availabilityType);

            return(board.IsAnyPieceInSquare(new SquareCoordinate(newRank, newFile)));
        }
Esempio n. 19
0
        private static IEnumerable <SquareCoordinate> GetDiagonalInBetweenPositions(SquareCoordinate startingCoordinate, SquareCoordinate endingCoordinate)
        {
            var ret = new List <SquareCoordinate>();

            var shift         = Math.Abs(endingCoordinate.Rank - startingCoordinate.Rank);
            var fileDirection = Math.Sign(endingCoordinate.File - startingCoordinate.File);
            var rankDirection = Math.Sign(endingCoordinate.Rank - startingCoordinate.Rank);

            for (int i = 1; i < shift; i++)
            {
                var rank = startingCoordinate.Rank + (rankDirection * i);
                var file = startingCoordinate.File + (fileDirection * i);
                ret.Add(new SquareCoordinate(rank, file));
            }

            return(ret);
        }
Esempio n. 20
0
        internal static IEnumerable <SquareCoordinate> GetKnightAvailability(SquareCoordinate startingCoordinate, Board board, Color pieceColor, SquareInfluenceType availabilityType)
        {
            var ret = new List <SquareCoordinate>();

            //Upper right, up
            var rank = startingCoordinate.Rank + 2;
            var file = startingCoordinate.File + 1;

            TryAddSquare(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType);
            //Upper right, right
            rank = startingCoordinate.Rank + 1;
            file = startingCoordinate.File + 2;
            TryAddSquare(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType);

            //Upper left, up
            rank = startingCoordinate.Rank + 2;
            file = startingCoordinate.File - 1;
            TryAddSquare(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType);
            //Upper left, right
            rank = startingCoordinate.Rank + 1;
            file = startingCoordinate.File - 2;
            TryAddSquare(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType);

            //Bottom right, bottom
            rank = startingCoordinate.Rank - 2;
            file = startingCoordinate.File + 1;
            TryAddSquare(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType);
            //Bottom right, right
            rank = startingCoordinate.Rank - 1;
            file = startingCoordinate.File + 2;
            TryAddSquare(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType);

            //Bottom left, bottom
            rank = startingCoordinate.Rank - 2;
            file = startingCoordinate.File - 1;
            TryAddSquare(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType);
            //Bottom left, right
            rank = startingCoordinate.Rank - 1;
            file = startingCoordinate.File - 2;
            TryAddSquare(ret, startingCoordinate, rank, file, board, pieceColor, availabilityType);

            return(ret);
        }
Esempio n. 21
0
        private static IEnumerable <SquareCoordinate> GetPawnControl(SquareCoordinate startingCoordinate, Board board, Color color)
        {
            var ret = new List <SquareCoordinate>();

            var captureRank      = startingCoordinate.Rank + color.MovingDirection;
            var leftCaptureFile  = startingCoordinate.File - 1;
            var rightCaptureFile = startingCoordinate.File + 1;

            if (IsValidPosition(captureRank, leftCaptureFile))
            {
                ret.Add(new SquareCoordinate(captureRank, leftCaptureFile));
            }

            if (IsValidPosition(captureRank, rightCaptureFile))
            {
                ret.Add(new SquareCoordinate(captureRank, rightCaptureFile));
            }

            return(ret);
        }
Esempio n. 22
0
        internal static IEnumerable <SquareCoordinate> GetPawnFrontSquares(SquareCoordinate square, Color color)
        {
            var ret = new List <SquareCoordinate>();

            //Left
            if (IsValidPosition(square.Rank + color.MovingDirection, square.File - 1))
            {
                ret.Add(new SquareCoordinate(square.Rank + color.MovingDirection, square.File - 1));
            }

            //In front
            if (IsValidPosition(square.Rank + color.MovingDirection, square.File))
            {
                ret.Add(new SquareCoordinate(square.Rank + color.MovingDirection, square.File));
            }

            //Right
            if (IsValidPosition(square.Rank + color.MovingDirection, square.File + 1))
            {
                ret.Add(new SquareCoordinate(square.Rank + color.MovingDirection, square.File + 1));
            }

            return(ret);
        }
Esempio n. 23
0
 internal bool IsControlledByColor(SquareCoordinate coordinate, Color color)
 {
     return(_piecesInfluenceManager.IsControlledByColor(this[coordinate], color));
 }
Esempio n. 24
0
 public Square this[SquareCoordinate coordinate]
 {
     get { return(_position[coordinate]); }
 }
Esempio n. 25
0
 internal bool IsAnyOpponentPieceInSquare(SquareCoordinate coordinate, Color color)
 {
     return(_position[coordinate].ContainsOpponentPiece(color));
 }
Esempio n. 26
0
 internal bool IsAnyPieceInSquare(SquareCoordinate coordinate)
 {
     return(_position[coordinate].ContainsPiece());
 }
Esempio n. 27
0
 internal bool CanPieceOfColorGoToSquare(SquareCoordinate coordinate, Color color)
 {
     return(_position[coordinate].Piece == null ||
            _position[coordinate].Piece.Color.IsOpponentColor(color));
 }
Esempio n. 28
0
 private Square CreateSquare(SquareCoordinate coordinate)
 {
     return(new Square(coordinate));
 }
Esempio n. 29
0
        private KeyValuePair <SquareCoordinate, Square> CreateSquare(int rank, int file)
        {
            var coordinate = new SquareCoordinate(rank, file);

            return(new KeyValuePair <SquareCoordinate, Square>(coordinate, CreateSquare(coordinate)));
        }
Esempio n. 30
0
 internal static int GetFileDifference(this SquareCoordinate startingCoordinate, SquareCoordinate endingCoordinate)
 {
     return(endingCoordinate.File - startingCoordinate.File);
 }