Esempio n. 1
0
        internal static void GetCastleMoves(byte color, Board board, Move[] moveList, ref int pos)
        {
            if (color == PieceColor.White)
            {
                if (board.WhiteCanCastleOO)
                {
                    if ((Constants.WhiteCastleMaskOO & board.OccupiedSquares) == 0)
                        moveList[pos++] = Constants.WhiteCastlingOO;

                }
                if (board.WhiteCanCastleOOO)
                {
                    if ((Constants.WhiteCastleMaskOOO & board.OccupiedSquares) == 0)
                        moveList[pos++] = Constants.WhiteCastlingOOO;
                }
            }

            else if (color == PieceColor.Black)
            {
                if (board.BlackCanCastleOO)
                {
                    if ((Constants.BlackCastleMaskOO & board.OccupiedSquares) == 0)
                        moveList[pos++] = Constants.BlackCastlingOO;
                }
                if (board.BlackCanCastleOOO)
                {
                    if ((Constants.BlackCastleMaskOOO & board.OccupiedSquares) == 0)
                        moveList[pos++] = Constants.BlackCastlingOOO;
                }
            }
        }
Esempio n. 2
0
 internal static void GetAllMoves(byte color, Move[] allMoves, ref int pos, Board board)
 {
     GetPawnMoves(color, board.GetPieceSet(color, PieceType.Pawn), board, allMoves, ref pos);
     GetKnightMoves(color, board.GetPieceSet(color, PieceType.Knight), board, allMoves, ref pos);
     GetKingMoves(color, board.GetPieceSet(color, PieceType.King), board, allMoves, ref pos);
     GetBishopMoves(color, board.GetPieceSet(color, PieceType.Bishop), board, allMoves, ref pos);
     GetRookMoves(color, board.GetPieceSet(color, PieceType.Rook), board, allMoves, ref pos);
     GetQueenMoves(color, board.GetPieceSet(color, PieceType.Queen), board, allMoves, ref pos);
     GetCastleMoves(color, board, allMoves, ref pos);
 }
Esempio n. 3
0
        internal static BitBoard GetAllTargets(byte pieceColor, BitBoard bishops, Board board)
        {
            BitBoard occupiedSquares = board.OccupiedSquares;
            BitBoard targets = Constants.Empty;
            int square;

            square = BitBoard.BitScanForward(bishops);

            targets |= MovePackHelper.GetA1H8DiagonalAttacks(occupiedSquares, square);
            targets |= MovePackHelper.GetH1A8DiagonalAttacks(occupiedSquares, square);

            return targets & ~board.GetPlayerPieces();
        }
Esempio n. 4
0
        internal static BitBoard GetAllTargets(byte pieceColor, BitBoard rooks, Board board)
        {
            BitBoard occupiedSquares = board.OccupiedSquares;
            BitBoard targets = Constants.Empty;
            int square;

            square = BitBoard.BitScanForward(rooks);

            targets |= MovePackHelper.GetRankAttacks(occupiedSquares, square);
            targets |= MovePackHelper.GetFileAttacks(occupiedSquares, square);

            return targets & ~board.GetPlayerPieces();
        }
Esempio n. 5
0
        internal static void GetKingMoves(byte color, BitBoard king, Board board, Move[] moveList, ref int pos)
        {
            BitBoard targets;
            byte fromIndex;
            byte toIndex;

            while (king != 0)
            {
                fromIndex = (byte)BitBoard.BitScanForwardReset(ref king); // search for LS1B and then reset it
                targets = King.GetAllTargets(color, Constants.SquareMask[fromIndex], board);

                while (targets != 0)
                {
                    toIndex = (byte)BitBoard.BitScanForwardReset(ref targets); // search for LS1B and then reset it
                    moveList[pos++] = new Move(fromIndex, toIndex, PieceType.King, board.pieceSet[toIndex].Type, PieceType.None);
                }
            }
        }
Esempio n. 6
0
        private static void Divide(byte color, int depth, Board board)
        {
            int total = 0;
            int temp = 0;
            int NumMoves = 0;

            MoveList moves = new MoveList();
            MoveGenerator.GetLegalMoves(color, moves.moves, ref moves.Length, board);

            Console.WriteLine("Move\tNodes");

            for (int i = 0; i < moves.Length; i++)
            {
                board.MakeMove(moves.moves[i]);
                temp = Perft(color.GetOpposite(), depth - 1, board);
                total += temp;
                Console.WriteLine("{0} {1} ", moves.moves[i].ToAlgebraic(), temp);
                board.UndoMove(moves.moves[i]);
                NumMoves++;
            }

            Console.WriteLine("Total Nodes: {0}", total);
            Console.WriteLine("Moves: {0}", NumMoves);
        }
Esempio n. 7
0
        internal static BitBoard GetAllTargets(byte pieceColor, BitBoard knights, Board board)
        {
            BitBoard targets = MovePackHelper.KnightAttacks[(BitBoard.BitScanForward(knights))];

            return targets & ~board.GetPlayerPieces();
        }
Esempio n. 8
0
 internal static BitBoard GetAllTargets(byte pieceColor, BitBoard king, Board board)
 {
     BitBoard kingMoves = MovePackHelper.KingAttacks[(BitBoard.BitScanForward(king))];
     return kingMoves & ~board.GetPlayerPieces();
 }
Esempio n. 9
0
 internal static BitBoard GetAnyAttack(byte color, BitBoard pawns, Board board)
 {
     return (GetEastAttacks(color, pawns) | GetWestAttacks(color, pawns)) & board.GetEnemyPieces();
 }
Esempio n. 10
0
        internal static BitBoard GetAllTargets(byte color, BitBoard pawns, Board board)
        {
            BitBoard empty = board.EmptySquares;

            return GetQuietTargets(color, pawns, empty) | GetAnyAttack(color, pawns, board);
        }
Esempio n. 11
0
        internal static void GetLegalMoves(byte color, Move[] allMoves, ref int pos, Board board)
        {
            BitBoard pinned = board.GetPinnedPieces();
            GetAllMoves(color, allMoves, ref pos, board);

            int last = pos;
            int cur = 0;
            while (cur != last)
            {
                if (!board.IsMoveLegal(allMoves[cur], pinned))
                {
                    allMoves[cur] = allMoves[--last];
                }
                else
                {
                    cur++;
                }
            }
            pos = last;
        }
Esempio n. 12
0
        //TESTER
        static void Main(string[] args)
        {
            Board board = new Board();
            board.Equip();

            //Move move1 = new Move(12, 28, PieceType.Pawn, board.pieceSet[28].Type, PieceType.None);
            //Move move2 = new Move(51, 35, PieceType.Pawn, board.pieceSet[35].Type, PieceType.None);
            //Move move3 = new Move(28, 35, PieceType.Pawn, board.pieceSet[35].Type, PieceType.None);

            //BitBoard.Display(board.GetPieceSet(board.SideToMove.GetOpposite(), PieceType.Pawn));

            //Console.ReadKey();
            //Console.Clear();

            //move1 = new Move(23, 38, PieceType.Knight, board.pieceSet[38].Type, PieceType.None);
            //board.MakeMove(move1);

            //BitBoard.Display(board.GetPieceSet(board.SideToMove, PieceType.Pawn));

            //Console.ReadKey();
            //Console.Clear();

            //board.Display();
            //Console.ReadKey();
            //Console.Clear();

            //board.UndoMove(move1);
            //board.Display();

            //Console.ReadKey();
            //Console.Clear();

            //move2 = new Move(51, 35, PieceType.Pawn, board.pieceSet[35].Type, PieceType.None);
            //board.MakeMove(move2);

            //board.Display();
            //Console.ReadKey();
            //Console.Clear();

            //move3 = new Move(28, 35, PieceType.Pawn, board.pieceSet[35].Type, PieceType.None);
            //board.MakeMove(move3);

            //board.Display();
            //Console.ReadKey();
            //Console.Clear();

            //board.UndoMove(move3);

            //board.Display();
            //Console.ReadKey();
            //Console.Clear();

            //board.UndoMove(move2);

            //board.Display();
            //Console.ReadKey();
            //Console.Clear();

            //board.UndoMove(move1);

            //board.Display();
            //Console.ReadKey();
            //Console.Clear();

            Stopwatch watch = Stopwatch.StartNew();

            //Divide(board.SideToMove, 1, board);
            Console.WriteLine(Perft(PieceColor.White, 5, board));

            watch.Stop();
            Console.WriteLine("Catture: {0}", cap);
            Console.WriteLine("Tempo Impiegato: {0}", watch.ElapsedMilliseconds);
            Console.ReadKey();

            //Console.ReadKey();
        }
Esempio n. 13
0
        private static void Test(Board board)
        {
            //MoveList moves = MoveGenerator.GetAllMoves(PieceColor.White, board);

            //short i, k, l, m, n, p;

            ////for (i = 0; i < moves.Lenght; i++)
            ////{
            ////    Console.WriteLine(moves.moves[i].ToAlgebraic());
            ////}
            ////Console.WriteLine(i);

            //for (i = 0; i < moves.Lenght; i++)
            //{
            //    for (k = 0; k < moves.Lenght; k++)
            //    {
            //        for (l = 0; l < moves.Lenght; l++)
            //        {
            //            for (m = 0; m < moves.Lenght; m++)
            //            {
            //                for (n = 0; n < moves.Lenght; n++)
            //                {
            //                    MoveGenerator.GetAllMoves(PieceColor.White, board);

            //                    for (p = 0; p < moves.Lenght; p++)
            //                    {
            //                        //if (BitBoard.IsAttacked(square, PieceColor.Black, board))
            //                        //{

            //                        //}

            //                    }
            //                }
            //            }
            //        }
            //    }
            //}
        }
Esempio n. 14
0
        private static int Perft(byte color, int depth, Board board)
        {
            MoveList moves = new MoveList();
            MoveGenerator.GetLegalMoves(color, moves.moves, ref moves.Length, board);

            int nodes = 0;

            if (depth == 1)
            {
                return moves.Length;
            }

            if (depth == 0)
                return 1;

            //if (moves.Length == 0)
            //    return 1;

            for (int i = 0; i < moves.Length; i++)
            {
                //if (moves.moves[i].IsCapture())
                //    cap++;
                board.MakeMove(moves.moves[i]);
                nodes += Perft(color.GetOpposite(), depth - 1, board);
                board.UndoMove(moves.moves[i]);
            }

            return nodes;
        }
Esempio n. 15
0
 internal static BitBoard GetAllTargets(byte pieceColor, BitBoard queens, Board board)
 {
     return Rook.GetAllTargets(pieceColor, queens, board) | Bishop.GetAllTargets(pieceColor, queens, board);
 }
Esempio n. 16
-1
        internal static void GetPawnMoves(byte color, BitBoard pawns, Board board, Move[] moveList, ref int pos)
        {
            BitBoard targets;
            BitBoard epTargets;
            byte fromIndex;
            byte toIndex;

            while (pawns != 0)
            {
                fromIndex = (byte)BitBoard.BitScanForwardReset(ref pawns); // search for LS1B and then reset it
                targets = Pawn.GetAllTargets(color, Constants.SquareMask[fromIndex], board);

                while (targets != 0)
                {
                    toIndex = (byte)BitBoard.BitScanForwardReset(ref targets); // search for LS1B and then reset it

                    // en passant
                    if (board.EnPassantSquare != Square.Invalid)
                    {
                        epTargets = color == PieceColor.White ? MovePackHelper.WhitePawnAttacks[fromIndex] : MovePackHelper.BlackPawnAttacks[fromIndex];

                        if ((epTargets & Constants.SquareMask[board.EnPassantSquare]) != 0)
                            moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, PieceType.Pawn, PieceType.Pawn);
                    }

                    // promotions
                    if (Square.GetRankIndex(toIndex) == 7 && color == PieceColor.White || Square.GetRankIndex(toIndex) == 0 && color == PieceColor.Black)
                    {
                        moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, board.pieceSet[toIndex].Type, PieceType.Queen);
                        moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, board.pieceSet[toIndex].Type, PieceType.Rook);
                        moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, board.pieceSet[toIndex].Type, PieceType.Bishop);
                        moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, board.pieceSet[toIndex].Type, PieceType.Knight);
                    }
                    else
                        moveList[pos++] = new Move(fromIndex, toIndex, PieceType.Pawn, board.pieceSet[toIndex].Type, PieceType.None); // no promotions
                }
            }
        }