Example #1
0
        int AlphaBeta(int depth, int alpha, int beta)
        {
            int    score    = 0;
            int    ttMove   = 0;
            int    bestMove = 0;
            TTType ttFlag   = TTType.Alpha;

            if (clk.GetElapsedMilliseconds() > totalMillisec)
            {
                return(0); // timeout
            }
            if (depth == 0)
            {
                return(Quiesce(alpha, beta, 0));
            }
            else
            {
                if (ttable.Probe(board.ZKey, depth, alpha, beta, ref ttMove, ref score))
                {
                    return(score);
                }
                bestMove = ttMove;
                var moves = GetMoves(bestMove, depth);

                if (moves.Count() == 0)
                {
                    score = board.InCheck(board.ToMove)?-INFINITE:0;
                    ttable.Save(board.ZKey, depth, score, TTType.Exact, 0);
                    return(score);
                }

                foreach (var move in moves)
                {
                    board.Move(move);
                    score = -AlphaBeta(depth - 1, -beta, -alpha);
                    board.UndoMove();
                    if (score > alpha)
                    {
                        bestMove = move;
                        alpha    = score;
                        ttFlag   = TTType.Exact;
                    }
                    if (alpha >= beta) // current plaier move is better than the other one better, no reason to search further
                    {
                        //beta cutoff !!!
                        ttable.Save(board.ZKey, depth, beta, TTType.Beta, bestMove);
                        if (0 != (MovePackHelper.Capture & move))
                        {
                            StoreKiller(depth, move);
                        }
                        return(beta);
                    }
                }
                ttable.Save(board.ZKey, depth, alpha, ttFlag, bestMove);
                return(alpha);
            }
        }