Example #1
0
        private static bool IsAttackedByPawn(
            BoardSquare targetSquare,
            Board board,
            Player attacker)
        {
            var pawnAttackDeltaRow = attacker == Player.Black ? -1 : 1;
            var possiblePawnRow    = targetSquare.Row - pawnAttackDeltaRow;

            if (possiblePawnRow < 0 || possiblePawnRow >= Board.RowCount)
            {
                return(false);
            }
            if (targetSquare.Column > 0)
            {
                var possiblePawnSquare = new BoardSquare(
                    possiblePawnRow,
                    targetSquare.Column - 1);
                if (IsAttackerPawnAt(possiblePawnSquare, attacker, board))
                {
                    return(true);
                }
            }
            if (targetSquare.Column + 1 < Board.ColumnCount)
            {
                var possiblePawnSquare = new BoardSquare(
                    possiblePawnRow,
                    targetSquare.Column + 1);
                if (IsAttackerPawnAt(possiblePawnSquare, attacker, board))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
        private static bool IsAttackerPawnAt(BoardSquare square, Player player, Board board)
        {
            if (board.IsEmpty(square))
            {
                return(false);
            }
            var pieceAtSquare = board.GetPieceAt(square);

            return(pieceAtSquare.player == player && pieceAtSquare.piece == Piece.Pawn);
        }
Example #3
0
        private static bool IsAllSquaresEmptyAlongBishopAttack(
            Bitmask64 targetBishopMovesBitmask,
            BoardSquare targetSquare,
            BoardSquare attackerSquare,
            long freeSquares)
        {
            long rayAttack = targetBishopMovesBitmask.Value &
                             GetBitmaskInBetweenRows(targetSquare.Row, attackerSquare.Row).Value &
                             GetBitmaskInBetweenColumns(targetSquare.Column, attackerSquare.Column).Value;

            return(rayAttack == (rayAttack & freeSquares));
        }
Example #4
0
 private static bool IsBishopAttack(
     Bitmask64 targetBishopMovesBitmask,
     BoardSquare targetSquare,
     BoardSquare attackerSquare,
     long freeSquares)
 {
     if (!IsBishopDelta(new MoveDelta(targetSquare, attackerSquare)))
     {
         return(false);
     }
     return(IsAllSquaresEmptyAlongBishopAttack(
                targetBishopMovesBitmask,
                targetSquare,
                attackerSquare,
                freeSquares));
 }
Example #5
0
 private static bool IsRookAttack(
     Bitmask64 targetRookMovesBitmask,
     BoardSquare targetSquare,
     BoardSquare attackerSquare,
     long freeSquares)
 {
     if (targetSquare.Row == attackerSquare.Row)
     {
         return(IsAllSquaresEmptyAlongHorizontalAttack(
                    targetRookMovesBitmask,
                    targetSquare.Column,
                    attackerSquare.Column,
                    freeSquares));
     }
     else if (targetSquare.Column == attackerSquare.Column)
     {
         return(IsAllSquaresEmptyAlongVerticalAttack(
                    targetRookMovesBitmask,
                    targetSquare.Row,
                    attackerSquare.Row,
                    freeSquares));
     }
     return(false);
 }
Example #6
0
        public static bool IsBoardSquareAttacked(
            BoardSquare targetSquare,
            Board board,
            Player attacker)
        {
            if (IsAttackedByPawn(targetSquare, board, attacker))
            {
                return(true);
            }

            var targetBishopMovesBitmask = GetBishopBitmask(targetSquare);
            var targetRookMovesBitmask   = GetRookBitmask(targetSquare);

            long freeSquares = ~board.OccupiedSquares.Value;

            foreach (var attackingPiece in board.GetPieces(attacker))
            {
                switch (attackingPiece.piece)
                {
                case Piece.Pawn:
                    // Already checked
                    break;

                case Piece.Knight:
                {
                    if (IsKnightDelta(new MoveDelta(targetSquare, attackingPiece.square)))
                    {
                        return(true);
                    }
                }
                break;

                case Piece.Bishop:
                {
                    if (IsBishopAttack(
                            targetBishopMovesBitmask,
                            targetSquare,
                            attackingPiece.square,
                            freeSquares))
                    {
                        return(true);
                    }
                }
                break;

                case Piece.Rook:
                {
                    if (IsRookAttack(
                            targetRookMovesBitmask,
                            targetSquare,
                            attackingPiece.square,
                            freeSquares))
                    {
                        return(true);
                    }
                }
                break;

                case Piece.Queen:
                {
                    if (IsBishopAttack(
                            targetBishopMovesBitmask,
                            targetSquare,
                            attackingPiece.square,
                            freeSquares))
                    {
                        return(true);
                    }
                    if (IsRookAttack(
                            targetRookMovesBitmask,
                            targetSquare,
                            attackingPiece.square,
                            freeSquares))
                    {
                        return(true);
                    }
                }
                break;

                case Piece.King:
                {
                    if (IsKingDelta(new MoveDelta(targetSquare, attackingPiece.square)))
                    {
                        return(true);
                    }
                }
                break;
                }
            }
            return(false);
        }
Example #7
0
 private void MovePiece(BoardSquare originSquare, BoardSquare destSquare)
 {
     _pieceTable.MovePiece(originSquare.IntValue, destSquare.IntValue);
     _occupiedSquares.UnsetBit(originSquare.IntValue);
     _occupiedSquares.SetBit(destSquare.IntValue);
 }
Example #8
0
 public PieceOnBoard GetPieceAt(BoardSquare square)
 {
     return(_pieceTable.GetPieceAt(square.IntValue));
 }
Example #9
0
 public bool IsEmpty(BoardSquare square)
 {
     return(!_occupiedSquares.IsBitSet(square.IntValue));
 }