Пример #1
0
        /* Evaluation heurisitc:
         *      Consider each potential winning line.  If there is a black piece
         *      in the line, then it cannot be won by white, so score that line as zero.
         *      Otherwise, score the line as the # of white pieces residing in the line.
         *      Keep track of the highest scoring chain, and how many were found with
         *      that score.  Do the same for black.
         *
         *      Give most favor to longer chains, then secondary favor to the number of
         *      such chains present.  Create a positive white score this way, then
         *      subtract off the corresponding black score using the same logic.
         *
         *      Always gives positive score to white, negative to black.  Up to the
         *      caller to multiply by -1 if needed.
         */
        private static int EvaluateChains(ref Board b)
        {
            int    longestWhiteChain = 0;
            int    numLongestWhiteChain = 0;
            int    longestBlackChain = 0;
            int    numLongestBlackChain = 0;
            UInt64 whiteTemp, blackTemp;
            int    temp;

            for (int i = 0; i < BoardHelper.WinningLines.Length; i++)
            {
                // how many white pieces are in this line
                whiteTemp = b.WhiteBitBoard & BoardHelper.WinningLines[i];
                // how many black pieces are in this line
                blackTemp = b.BlackBitBoard & BoardHelper.WinningLines[i];

                if (blackTemp == 0 && whiteTemp != 0)
                {
                    temp = BoardHelper.NumCommonOneBits(b.WhiteBitBoard, BoardHelper.WinningLines[i]);
                    if (temp >= longestWhiteChain)
                    {
                        if (temp == longestWhiteChain)
                        {
                            numLongestWhiteChain++;
                        }
                        else
                        {
                            longestWhiteChain    = temp;
                            numLongestWhiteChain = 1;
                        }
                    }
                    continue;  // no need to evaluate black
                }
                if (whiteTemp == 0 && blackTemp != 0)
                {
                    temp = BoardHelper.NumCommonOneBits(b.BlackBitBoard, BoardHelper.WinningLines[i]);
                    if (temp >= longestBlackChain)
                    {
                        if (temp == longestBlackChain)
                        {
                            numLongestBlackChain++;
                        }
                        else
                        {
                            longestBlackChain    = temp;
                            numLongestBlackChain = 1;
                        }
                    }
                }
            }

            return((100 * longestWhiteChain + numLongestWhiteChain) - (100 * longestBlackChain + numLongestBlackChain));
        }
Пример #2
0
        public static bool HasCheckmate(Board b, PieceColor pieceColor)
        {
            int           whiteTemp = 0;
            int           blackTemp = 0;
            UInt64        whiteLine = 0;
            UInt64        blackLine = 0;
            List <UInt64> lines     = new List <UInt64>();

            for (int i = 0; i < BoardHelper.WinningLines.Length; i++)
            {
                whiteLine = b.WhiteBitBoard & BoardHelper.WinningLines[i];
                whiteTemp = BoardHelper.NumOneBits(whiteLine);
                blackLine = b.BlackBitBoard & BoardHelper.WinningLines[i];
                blackTemp = BoardHelper.NumOneBits(blackLine);

                if (pieceColor == PieceColor.White && blackTemp == 0 && whiteTemp == 4)
                {
                    foreach (UInt64 line in lines)
                    {
                        if (line == whiteLine)
                        {
                            return(true);
                        }
                    }
                    lines.Add(whiteLine);
                }
                else if (pieceColor == PieceColor.Black && whiteTemp == 0 && blackTemp == 4)
                {
                    foreach (UInt64 line in lines)
                    {
                        if (line == blackLine)
                        {
                            return(true);
                        }
                    }
                    lines.Add(blackLine);
                }
            }
            return(false);
        }