Ejemplo n.º 1
0
        public Move(GameEngine engine, Board board, Square fromSquare, Square toSquare, Piece pieceToTest)
        {
            _engine = engine;
            _board = board;
            FromSquare = fromSquare;
            ToSquare = toSquare;
            PieceMoved = pieceToTest;

            IsValidMove = TestMove();
        }
Ejemplo n.º 2
0
        public Move(GameEngine engine, Board board, Square fromSquare, Square toSquare, Piece pieceMoved, Piece pieceCaptured, bool isPacketMove, Piece.PieceType piecePromoted)
        {
            _engine = engine;
            _board = board;
            FromSquare = fromSquare;
            ToSquare = toSquare;
            PieceMoved = pieceMoved;
            PiecePromoted = piecePromoted;
            PieceCaptured = pieceCaptured;

            ProcessMove(isPacketMove);
        }
Ejemplo n.º 3
0
        public static UInt64 GenerateBishopMoves(Square s, Piece.Colour c)
        {
            UInt64 validSquares;
            int sqIndex = BitboardHelper.GetIndexFromSquare(s);

            UInt64 bbBlockers = _board.AllPieces & BitboardHelper.OccupancyMaskBishop[sqIndex];

            int databaseIndex = (int)((bbBlockers * BitboardHelper.MagicNumberBishop[sqIndex]) >> BitboardHelper.MagicNumberShiftsBishop[sqIndex]);

            if (c == Piece.Colour.White)
                validSquares = BitboardHelper.MagicMovesBishop[sqIndex][databaseIndex] & ~_board.WhitePieces;
            else if (c == Piece.Colour.Black)
                validSquares = BitboardHelper.MagicMovesBishop[sqIndex][databaseIndex] & ~_board.BlackPieces;
            else
                validSquares = BitboardHelper.MagicMovesBishop[sqIndex][databaseIndex];

            return validSquares;
        }
Ejemplo n.º 4
0
 private static string GetInitial(Piece.PieceType type)
 {
     switch (type)
     {
         case Piece.PieceType.Bishop:
             return "B";
         case Piece.PieceType.Knight:
             return "N";
         case Piece.PieceType.Queen:
             return "Q";
         case Piece.PieceType.Rook:
             return "R";
             case Piece.PieceType.King:
             return "K";
         default:
             return "initial";
     }
 }
Ejemplo n.º 5
0
        private List<Square> GenerateMoves(Piece p, Square s)
        {
            //Call method based on Type of Piece passed in
            switch (p.Piece_Type)
            {
                case Piece.PieceType.King:
                    return GetSquareListFromBB(MoveGen.GenerateKingMoves(s, p.ColorType));

                case Piece.PieceType.Pawn:
                    return GetSquareListFromBB(MoveGen.GeneratePawnMoves(s, p.ColorType));

                case Piece.PieceType.Knight:
                    return GetSquareListFromBB(MoveGen.GenerateKnightMoves(s, p.ColorType));

                case Piece.PieceType.Bishop:
                    return GetSquareListFromBB(MoveGen.GenerateBishopMoves(s, p.ColorType));

                case Piece.PieceType.Rook:
                    return GetSquareListFromBB(MoveGen.GenerateRookMoves(s, p.ColorType));

                case Piece.PieceType.Queen:
                    return GetSquareListFromBB(MoveGen.GenerateQueenMoves(s, p.ColorType));
                default:
                    return null;
            }
        }
Ejemplo n.º 6
0
        public static UInt64 GenerateKingMoves(Square s, Piece.Colour c)
        {
            UInt64 myPieceBB = BitboardHelper.GetBitboardFromSquare(s);

            UInt64 myPieceBB_H_Clip = (myPieceBB & BitboardHelper.ClearFile[7]);
            UInt64 myPieceBB_A_Clip = (myPieceBB & BitboardHelper.ClearFile[0]);

            UInt64 validMovesBB = (myPieceBB_A_Clip << 7) | (myPieceBB << 8) | (myPieceBB_H_Clip << 9) | (myPieceBB_H_Clip << 1) | (myPieceBB_H_Clip >> 7) | (myPieceBB >> 8) | (myPieceBB_A_Clip >> 9) | (myPieceBB_A_Clip >> 1);

            if (c == Piece.Colour.White)
            {
                //remove move options occupied by own side
                validMovesBB = validMovesBB ^ (validMovesBB & _board.WhitePieces);

                //Add Castling moves if available
                if (WhiteCanCastleShort && ((BitboardHelper.MaskWhiteCastleShort & _board.AllPieces) == 0))
                    validMovesBB |= myPieceBB << 2;
                if (WhiteCanCastleLong && ((BitboardHelper.MaskWhiteCastleLong & _board.AllPieces) == 0))
                    validMovesBB |= myPieceBB >> 2;
            }
            else
            {
                //remove move options occupied by own side
                validMovesBB = validMovesBB ^ (validMovesBB & _board.BlackPieces);

                //Add Castling moves if available
                if (BlackCanCastleShort && ((BitboardHelper.MaskBlackCastleShort & _board.AllPieces) == 0))
                    validMovesBB |= myPieceBB << 2;
                if (BlackCanCastleLong && ((BitboardHelper.MaskBlackCastleLong & _board.AllPieces) == 0))
                    validMovesBB |= myPieceBB >> 2;
            }

            return validMovesBB;
        }
Ejemplo n.º 7
0
        public static UInt64 GenerateQueenMoves(Square s, Piece.Colour c)
        {
            //first calulate Rook movements for queen
            ulong validSquares = GenerateRookMoves(s, c);

            //then calculate Bishop moves for queen and OR with rook movements
            validSquares |= GenerateBishopMoves(s, c);

            return validSquares;
        }
Ejemplo n.º 8
0
        public static List<Square> GenerateMoves(Piece pieceToMove, Square fromSq)
        {
            //Call method based on Type of Piece passed in
            List<Square> list = new List<Square>();
            List<Square> result;

            switch (pieceToMove.Piece_Type)
            {
                case Piece.PieceType.King:
                   result = new List<Square>();
                   list = _board.GetSquareListFromBB(GenerateKingMoves(fromSq, pieceToMove.ColorType));
                    if(list != null)
                    {
                        foreach (Square s in list)
                        {
                        var m = new Move(null, _board, fromSq, s, pieceToMove);
                        if(m.IsValidMove)
                            result.Add(m.ToSquare);
                    }
                    }
                    return result;
                case Piece.PieceType.Pawn:
                    result = new List<Square>();
                    list = _board.GetSquareListFromBB(GeneratePawnMoves(fromSq, pieceToMove.ColorType));
                    if(list != null)
                    {
                        foreach (Square s in list)
                        {
                        var m = new Move(null, _board, fromSq, s, pieceToMove);
                        if(m.IsValidMove)
                            result.Add(m.ToSquare);
                    }
                    }
                    return result;
                case Piece.PieceType.Knight:
                    result = new List<Square>();
                    list = _board.GetSquareListFromBB(GenerateKnightMoves(fromSq, pieceToMove.ColorType));
                    if(list != null)
                    {
                        foreach (Square s in list)
                        {
                        var m = new Move(null, _board, fromSq, s, pieceToMove);
                        if(m.IsValidMove)
                            result.Add(m.ToSquare);
                    }
                    }
                    return result;
                case Piece.PieceType.Bishop:
                    result = new List<Square>();
                    list = _board.GetSquareListFromBB(GenerateBishopMoves(fromSq, pieceToMove.ColorType));
                    if(list != null)
                    {
                        foreach (Square s in list)
                        {
                        var m = new Move(null, _board, fromSq, s, pieceToMove);
                        if(m.IsValidMove)
                            result.Add(m.ToSquare);
                    }
                    }
                    return result;
                case Piece.PieceType.Rook:
                    result = new List<Square>();
                    list = _board.GetSquareListFromBB(GenerateRookMoves(fromSq, pieceToMove.ColorType));
                    if(list != null)
                    {
                        foreach (Square s in list)
                        {
                        var m = new Move(null, _board, fromSq, s, pieceToMove);
                        if(m.IsValidMove)
                            result.Add(m.ToSquare);
                    }
                    }
                    return result;
                case Piece.PieceType.Queen:
                   result = new List<Square>();
                   list = _board.GetSquareListFromBB(GenerateQueenMoves(fromSq, pieceToMove.ColorType));
                    if(list != null)
                    {
                        foreach (Square s in list)
                        {
                        var m = new Move(null, _board, fromSq, s, pieceToMove);
                        if(m.IsValidMove)
                            result.Add(m.ToSquare);
                    }
                    }
                    return result;

                default:
                    return null;
            }
        }
Ejemplo n.º 9
0
        public static UInt64 GeneratePawnMoves(Square s, Piece.Colour c)
        {
            UInt64 validMovesBB;
            UInt64 myPieceBB = BitboardHelper.GetBitboardFromSquare(s); //bitboard representation of the pawns position
            UInt64 myPieceBB_H_Clip = (myPieceBB & BitboardHelper.ClearFile[7]);
            UInt64 myPieceBB_A_Clip = (myPieceBB & BitboardHelper.ClearFile[0]);

            if (c == Piece.Colour.White)
            {
                validMovesBB = (myPieceBB_A_Clip << 7 | myPieceBB_H_Clip << 9) & _board.BlackPieces;

                if (((myPieceBB << 8) & _board.AllPieces) == 0)
                {
                    validMovesBB = validMovesBB | (myPieceBB << 8);

                    if (((myPieceBB & BitboardHelper.MaskRank[(int)Ranks.Two]) != 0) && ((myPieceBB << 16) & _board.AllPieces) == 0)
                        validMovesBB = validMovesBB | (myPieceBB << 16);
                }

                #region enPassant?
                if (_board.AllMoves.Count != 0 && _board.AllMoves.Last().IsDoublePawnPush) //is lastmove was a double pawn push, need to check is en passant is possible
                {
                    var lastPieceMovedBB = BitboardHelper.GetBitboardFromSquare(_board.AllMoves.Last().ToSquare);

                    if ((myPieceBB >> 1) == lastPieceMovedBB) // if lastPieceMoved is one square to the left, add en passant capture to validMoves
                        validMovesBB |= myPieceBB << 7;
                    else if ((myPieceBB << 1) == lastPieceMovedBB)// if lastPieceMoved is one square to the right, add en passant capture to validMoves
                        validMovesBB |= myPieceBB << 9;
                }
                #endregion
            }
            else
            {
                validMovesBB = (myPieceBB_H_Clip >> 7 | myPieceBB_A_Clip >> 9) & _board.WhitePieces;

                if (((myPieceBB >> 8) & _board.AllPieces) == 0)
                {
                    validMovesBB = validMovesBB | (myPieceBB >> 8);

                    if (((myPieceBB & BitboardHelper.MaskRank[(int)Ranks.Seven]) != 0) && ((myPieceBB >> 16) & _board.AllPieces) == 0)
                        validMovesBB = validMovesBB | myPieceBB >> 16;
                }

                #region EnPassant?
                if (_board.AllMoves.Count > 0 & _board.AllMoves.Last().IsDoublePawnPush) //is lastmove was a double pawn push, need to check is en passant is possible
                {
                    var lastPieceMovedBB = BitboardHelper.GetBitboardFromSquare(_board.AllMoves.Last().ToSquare);

                    if ((myPieceBB >> 1) == lastPieceMovedBB) // if lastPieceMoved is one square to the left, add en passant capture to validMoves
                        validMovesBB |= myPieceBB >> 9;
                    else if ((myPieceBB << 1) == lastPieceMovedBB)// if lastPieceMoved is one square to the right, add en passant capture to validMoves
                        validMovesBB |= myPieceBB >> 7;
                }
                #endregion
            }

            return validMovesBB;
        }
Ejemplo n.º 10
0
 public void UpdateRelevantbb(Piece.PieceType type, Piece.Colour c, ulong bbFrom, ulong bbTo)
 {
     if (c == Piece.Colour.White)
     {
         switch (type)
         {
             case Piece.PieceType.Bishop:
                 WhiteBishops ^= bbFrom;
                 WhiteBishops ^= bbTo;
                 break;
             case Piece.PieceType.King:
                 WhiteKings ^= bbFrom;
                 WhiteKings ^= bbTo;
                 break;
             case Piece.PieceType.Knight:
                 WhiteKnights ^= bbFrom;
                 WhiteKnights ^= bbTo;
                 break;
             case Piece.PieceType.Queen:
                 WhiteQueens ^= bbFrom;
                 WhiteQueens ^= bbTo;
                 break;
             case Piece.PieceType.Rook:
                 WhiteRooks ^= bbFrom;
                 WhiteRooks ^= bbTo;
                 break;
             case Piece.PieceType.Pawn:
                 WhitePawns ^= bbFrom;
                 WhitePawns ^= bbTo;
                 break;
         }
     }
     else
     {
         switch (type)
         {
             case Piece.PieceType.Bishop:
                 BlackBishops ^= bbFrom;
                 BlackBishops ^= bbTo;
                 break;
             case Piece.PieceType.King:
                 BlackKings ^= bbFrom;
                 BlackKings ^= bbTo;
                 break;
             case Piece.PieceType.Knight:
                 BlackKnights ^= bbFrom;
                 BlackKnights ^= bbTo;
                 break;
             case Piece.PieceType.Queen:
                 BlackQueens ^= bbFrom;
                 BlackQueens ^= bbTo;
                 break;
             case Piece.PieceType.Rook:
                 BlackRooks ^= bbFrom;
                 BlackRooks ^= bbTo;
                 break;
             case Piece.PieceType.Pawn:
                 BlackPawns ^= bbFrom;
                 BlackPawns ^= bbTo;
                 break;
         }
     }
 }
Ejemplo n.º 11
0
        public static UInt64 GenerateKnightMoves(Square s, Piece.Colour c)
        {
            UInt64 validMovesBB;
            int sqIndex = BitboardHelper.GetIndexFromSquare(s);

            if (c == Piece.Colour.White)
                validMovesBB = BitboardHelper.KnightAttacks[sqIndex] ^ (BitboardHelper.KnightAttacks[sqIndex]) & _board.WhitePieces;

            else if (c == Piece.Colour.Black)
                validMovesBB = BitboardHelper.KnightAttacks[sqIndex] ^ (BitboardHelper.KnightAttacks[sqIndex]) & _board.BlackPieces;

            else
                validMovesBB = BitboardHelper.KnightAttacks[sqIndex];

            return validMovesBB;
        }
Ejemplo n.º 12
0
 // Tests if king is in check after a piece is moved
 public bool TestMoveForCheck(Piece.Colour color)
 {
     switch (color)
     {
         case Piece.Colour.White:
             return TestForCheck(GetSquareFromBB(WhiteKings), true);
         case Piece.Colour.Black:
             return TestForCheck(GetSquareFromBB(BlackKings), false);
     }
     return false;
 }
Ejemplo n.º 13
0
 public bool TestForPromotion(Piece.Colour c, Ranks rank)
 {
     switch (c)
     {
         case Piece.Colour.White:
             if (rank == Ranks.Eight)
                 return true;
             break;
         case Piece.Colour.Black:
             if (rank == Ranks.One)
                 return true;
             break;
     }
     return false;
 }
Ejemplo n.º 14
0
        public bool TestForCheckmate(Piece.Colour c)
        {
            List<Square> list = new List<Square>();

            switch (c)
            {
                case Piece.Colour.White:
                    foreach (Piece p in White_Pieces)
                    {
                        list.AddRange(MoveGen.GenerateMoves(p, GetSquare(p)));
                    }
                    break;
                case Piece.Colour.Black:
                    foreach (Piece p in Black_Pieces)
                    {
                        list.AddRange(MoveGen.GenerateMoves(p, GetSquare(p)));
                    }
                    break;
            }
            return list.Count == 0;
        }
Ejemplo n.º 15
0
 public Square GetSquare(Piece p)
 {
     for (int i = 0; i < 8; i++)
     {
         for(int j = 0; j < 8; j++)
         {
             if (Squares[i, j].World.Translation == p.World.Translation - Adjust)
                 return Squares[i, j];
         }
     }
     return null;
 }
Ejemplo n.º 16
0
 public void SetPromotion(Piece.PieceType promoteType)
 {
     PromoteTo = promoteType;
     SelectState = SelectionState.MoveSelected;
 }