Example #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();
        }
Example #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);
        }
Example #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;
        }
        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;
            }
        }
Example #5
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;
        }
Example #6
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;
        }
Example #7
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;
        }
 public static int GetIndexFromSquare(Square s)
 {
     int index = 8 * (int)s.Rank + (int)s.File;
     return index;
 }
Example #9
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;
        }
Example #10
0
        //Test if a Square is being attacked/is in Check
        private bool TestForCheck(Square s, bool isWhite)
        {
            /*if all pieces attacking the kings position(or square) minus pieces of his own colour != 0
             * then the king is in check(square is attacked)*/

            if (isWhite)
                return ((FindAttacksToSquare(s) & ~WhitePieces) != 0);

            return (FindAttacksToSquare(s) & ~BlackPieces) != 0;
        }
Example #11
0
        public Board(bool isAnimated)
        {
            #region Init Squares

            Squares = new Square[8, 8];

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (i % 2 == 0)
                    {
                        if (j % 2 == 0)
                        {
                            // The dark coloured.. pass false for black
                            Squares[i, j] = new Square("square" + i + j, new Vector3(-24 + (7 * i), 0, 24 - (7 * j)), i, j, false);
                        }
                        else if (j % 2 == 1)
                        {
                            // ..and the light coloured.. pass true for white
                            Squares[i, j] = new Square("square" + i + j, new Vector3(-24 + (7 * i), 0, 24 - (7 * j)), i, j, true);
                        }
                    }
                    else if (i % 2 == 1)
                    {
                        if (j % 2 == 1)
                        {
                            Squares[i, j] = new Square("square" + i + j, new Vector3(-24 + (7 * i), 0, 24 - (7 * j)), i, j, false);
                        }
                        else if (j % 2 == 0)
                        {
                            Squares[i, j] = new Square("square" + i + j, new Vector3(-24 + (7 * i), 0, 24 - (7 * j)), i, j, true);
                        }
                    }
                }
            }
            #endregion

            #region Standard Piece Init
            Pieces = new List<Piece>
            {
                new Piece("PawnA2", "White Pawn", GetStartPos("a2"), 1, Piece.PieceType.Pawn),
                new Piece("PawnB2", "White Pawn", GetStartPos("b2"), 1, Piece.PieceType.Pawn),
                new Piece("PawnC2", "White Pawn", GetStartPos("c2"), 1, Piece.PieceType.Pawn),
                new Piece("PawnD2", "White Pawn", GetStartPos("d2"), 1, Piece.PieceType.Pawn),
                new Piece("PawnE2", "White Pawn", GetStartPos("e2"), 1, Piece.PieceType.Pawn),
                new Piece("PawnF2", "White Pawn", GetStartPos("f2"), 1, Piece.PieceType.Pawn),
                new Piece("PawnG2", "White Pawn", GetStartPos("g2"), 1, Piece.PieceType.Pawn),
                new Piece("PawnH2", "White Pawn", GetStartPos("h2"), 1, Piece.PieceType.Pawn),
                new Piece("RookA1", "White Rook", GetStartPos("a1"), 1, Piece.PieceType.Rook),
                new Piece("RookH1", "White Rook", GetStartPos("h1"), 1, Piece.PieceType.Rook),
                new Piece("King", "White King", GetStartPos("e1"), 1, Piece.PieceType.King),
                new Piece("Queen", "White Queen", GetStartPos("d1"), 1, Piece.PieceType.Queen),
                new Piece("KnightB1", "White Knight", GetStartPos("b1"), 1, Piece.PieceType.Knight),
                new Piece("KnightG1", "White Knight", GetStartPos("g1"), 1, Piece.PieceType.Knight),
                new Piece("BishopC1", "White Bishop", GetStartPos("c1"), 1, Piece.PieceType.Bishop),
                new Piece("BishopF1", "White Bishop", GetStartPos("f1"), 1, Piece.PieceType.Bishop),
                new Piece("pawnA7", "Black Pawn", GetStartPos("a7"), 0, Piece.PieceType.Pawn),
                new Piece("pawnB7", "Black Pawn", GetStartPos("b7"), 0, Piece.PieceType.Pawn),
                new Piece("pawnC7", "Black Pawn", GetStartPos("c7"), 0, Piece.PieceType.Pawn),
                new Piece("pawnD7", "Black Pawn", GetStartPos("d7"), 0, Piece.PieceType.Pawn),
                new Piece("pawnE7", "Black Pawn", GetStartPos("e7"), 0, Piece.PieceType.Pawn),
                new Piece("pawnF7", "Black Pawn", GetStartPos("f7"), 0, Piece.PieceType.Pawn),
                new Piece("pawnG7", "Black Pawn", GetStartPos("g7"), 0, Piece.PieceType.Pawn),
                new Piece("pawnH7", "Black Pawn", GetStartPos("h7"), 0, Piece.PieceType.Pawn),
                new Piece("rookA8", "Black Rook", GetStartPos("a8"), 0, Piece.PieceType.Rook),
                new Piece("rookH8", "Black Rook", GetStartPos("h8"), 0, Piece.PieceType.Rook),
                new Piece("king", "Black King", GetStartPos("e8"), 0, Piece.PieceType.King),
                new Piece("queen", "Black Queen", GetStartPos("d8"), 0, Piece.PieceType.Queen),
                new Piece("knightB8", "Black Knight", GetStartPos("b8"), 0, Piece.PieceType.Knight),
                new Piece("knightG8", "Black Knight", GetStartPos("g8"), 0, Piece.PieceType.Knight),
                new Piece("bishopC8", "Black Bishop", GetStartPos("c8"), 0, Piece.PieceType.Bishop),
                new Piece("bishopF8", "Black Bishop", GetStartPos("f8"), 0, Piece.PieceType.Bishop)
            };
            #endregion
        }
Example #12
0
 public Piece GetPiece(Square sq)
 {
     return Pieces.FirstOrDefault(t => t.World.Translation == sq.World.Translation + Adjust);
 }
Example #13
0
        // returns bitboard with all pieces attacking the specified Square
        public UInt64 FindAttacksToSquare(Square s)
        {
            int sqIndex = BitboardHelper.GetIndexFromSquare(s);

            ulong attackersBB = (BitboardHelper.KnightAttacks[sqIndex] & WhiteKnights & BlackKnights);
            attackersBB |= (BitboardHelper.WhitePawnAttacks[sqIndex] & WhitePawns);
            attackersBB |= (BitboardHelper.BlackPawnAttacks[sqIndex] & BlackPawns);
            attackersBB |= (BitboardHelper.KingAttacks[sqIndex] & (WhiteKings | BlackKings));
            attackersBB |= (MoveGen.GenerateBishopMoves(s, Piece.Colour.None) & (BlackBishops | WhiteBishops | BlackQueens | WhiteQueens));
            attackersBB |= (MoveGen.GenerateRookMoves(s, Piece.Colour.None) & (BlackRooks | WhiteRooks | BlackQueens | WhiteQueens));

            return attackersBB;
        }
Example #14
0
        protected virtual void HandleInput()
        {
            if (InputEngine.IsKeyPressed(Keys.N))
            {
                MediaPlayer.Pause();
            }

            if (InputEngine.IsKeyPressed(Keys.M))
            {
                MediaPlayer.Resume();
            }

            #region Arrow move/selection

            GameBoard.Squares[CurrentI, CurrentJ].IsHover = false;

            if (InputEngine.IsKeyPressed(Keys.Right))
            {
                _isMouseClick = false;
                if (PreviousSquare != null)
                    PreviousSquare.IsSelected = false;

                if (Turn == TurnState.White)
                {
                    CurrentI++;
                    if (CurrentI > 7)
                        CurrentI = 0;
                }
                else
                {
                    CurrentI--;
                    if (CurrentI < 0)
                        CurrentI = 7;
                }
            }

            if (InputEngine.IsKeyPressed(Keys.Left))
            {
                _isMouseClick = false;
                if (PreviousSquare != null)
                    PreviousSquare.IsSelected = false;

                if (Turn == TurnState.White)
                {
                    CurrentI--;
                    if (CurrentI < 0)
                        CurrentI = 7;
                }
                else
                {
                    CurrentI++;
                    if (CurrentI > 7)
                        CurrentI = 0;
                }
            }

            if (InputEngine.IsKeyPressed(Keys.Up))
            {
                _isMouseClick = false;
                if (PreviousSquare != null)
                    PreviousSquare.IsSelected = false;

                if (Turn == TurnState.White)
                {
                    CurrentJ++;
                    if (CurrentJ > 7)
                        CurrentJ = 0;
                }
                else
                {
                    CurrentJ--;
                    if (CurrentJ < 0)
                        CurrentJ = 7;
                }
            }

            if (InputEngine.IsKeyPressed(Keys.Down))
            {
                _isMouseClick = false;
                if (PreviousSquare != null)
                    PreviousSquare.IsSelected = false;

                if (Turn == TurnState.White)
                {
                    CurrentJ--;
                    if (CurrentJ < 0)
                        CurrentJ = 7;
                }
                else
                {
                    CurrentJ++;
                    if (CurrentJ > 7)
                        CurrentJ = 0;
                }
            }
            if (!_isMouseClick)
            {
                GameBoard.Squares[CurrentI, CurrentJ].IsHover = true;
            }

            if (InputEngine.IsKeyPressed(Keys.Enter))
            {
                if (PreviousSquare != null)
                    PreviousSquare.IsSelected = false;

                CurrentSquare = GameBoard.Squares[CurrentI, CurrentJ];

                if (SelectState == SelectionState.SelectPiece) //if a piece hasnt been selected, highlight
                {
                    CurrentSquare.IsSelected = true;
                }
                else
                    PreviousSquare = null;

                PreviousSquare = CurrentSquare;
            }

            if (InputEngine.IsMouseLeftClick())
            {
                if (PreviousSquare != null)
                    PreviousSquare.IsSelected = false;

                _isMouseClick = true;

                CurrentSquare = SquareSelectWithMouse();

                if (SelectState == SelectionState.SelectPiece && CurrentSquare != null) //if a piece hasnt been selected, highlight
                {
                        CurrentSquare.IsSelected = true;
                }
                else
                    PreviousSquare = null;

                PreviousSquare = CurrentSquare;
            }

            #endregion
        }
Example #15
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;
            }
        }
Example #16
0
 private Vector3 GetNewPos(Square destination)
 {
     return destination.World.Translation + new Vector3(0, 2, 0);
 }
Example #17
0
 public static ulong GetBitboardFromSquare(Square s)
 {
     return ((ulong)1) << GetIndexFromSquare(s);
 }