示例#1
0
文件: Search.cs 项目: nymann/KCE
        public int Quiescene(int alpha, int beta, BoardState bs, SearchInfo sInfo)
        {
            if (sInfo.IsTimeUp())
            {
                sInfo.Stopped = true;
            }

            //sInfo.Nodes++;

            /*if (_helper.IsRepetition(bs) || bs.FiftyMoveRule >= 100)
             * {
             *  //if (bs.BestPly != null) bs.BestPly.Score = 0;
             *  return 0;
             * }*/

            var score = _eval.EvalPosition(bs);

            if (score >= beta)
            {
                return(beta);
            }

            if (score > alpha)
            {
                alpha = score;
            }

            var mg = new MoveGenerator(bs);

            var capMoves = mg.AllCapMoves();

            Ply bestMove = null;
            var nMoves   = 0;

            if (capMoves != null)
            {
                nMoves = capMoves.Length;
            }
            var oldAlpha = alpha;
            var moveNum  = 0;

            if (bs.BestPlyAtLowerDepth != null && !bs.HaveSearched)
            {
                mg.MakeMove(bs.BestPlyAtLowerDepth);
                bs.BestPlyAtLowerDepth.Score = -Quiescene(-beta, -alpha, bs, sInfo);
                mg.UndoMove(bs.BestPlyAtLowerDepth);
                bs.HaveSearched = true;
                bestMove        = bs.BestPlyAtLowerDepth;
                alpha           = bestMove.Score;
                //Console.WriteLine("This never happens.");
            }

            for (moveNum = 0; moveNum < nMoves; moveNum++)
            {
                mg.MakeMove(capMoves[moveNum]);
                capMoves[moveNum].Score = -Quiescene(-beta, -alpha, bs, sInfo);
                mg.UndoMove(capMoves[moveNum]);

                if (sInfo.Stopped)
                {
                    return(Definitions.Stopped);
                }

                if (capMoves[moveNum].Score <= alpha)
                {
                    continue;
                }

                if (capMoves[moveNum].Score >= beta)
                {
                    if (nMoves == 1)
                    {
                        sInfo.Fhf++;
                    }
                    sInfo.Fh++;

                    return(beta); // Fail hard beta-cutoff.
                }

                alpha    = capMoves[moveNum].Score; // alpha acts like max in minimax.
                bestMove = capMoves[moveNum];
            }

            if (alpha != oldAlpha)
            {
                //Console.WriteLine("New best ply!");
                bs.BestPly = bestMove;
            }
            return(alpha);
        }