예제 #1
0
        private Result Run(Evaluator evaluator, Board board, Player player, int depth, int alpha, int beta)
        {
            hits++;

            if (depth == 0 || board.Winner != Player.None)
            {
                return(new Result(null, evaluator.Run(board, player)));
            }

            Result best = new Result(null, -inf);

            List <Move> moves = board.GetPossibleMovesSorted(player);

            if (depth == start)
            {
                total = moves.Count;
            }

            foreach (Move move in moves)
            {
                Transition transition = board.PerformMove(move);
                Result     candidate  = Run(evaluator, board, BackendHelper.SwapPlayer(player), depth - 1, -beta, -alpha);

                board.ReverseTransition(transition);

                if (-candidate.Value > best.Value)
                {
                    best = new Result(move, -candidate.Value);
                }

                if (best.Value >= beta)
                {
                    break;
                }

                if (best.Value > alpha)
                {
                    alpha = best.Value;
                }

                if (depth == start)
                {
                    done++;
                }
            }

            return(best);
        }
예제 #2
0
        private Result RunInternal(Evaluator evaluator, Board board, Player player, int depth)
        {
            hits++;

            if (depth == 0 || board.Winner != Player.None)
            {
                return(new Result(null, evaluator.Run(board, player)));
            }

            Result winner = new Result(null, int.MinValue);

            List <Move> moves = board.GetPossibleMovesSorted(player);

            if (depth == start)
            {
                total = moves.Count;
            }

            foreach (Move move in moves)
            {
                Transition transition = board.PerformMove(move);

                Result candidate = RunInternal(evaluator, board, BackendHelper.SwapPlayer(player), depth - 1);

                if (-candidate.Value > winner.Value)
                {
                    winner = new Result(move, -candidate.Value);
                }

                board.ReverseTransition(transition);

                if (depth == start)
                {
                    done++;
                }
            }

            return(winner);
        }
        private Result Run(Evaluator evaluator, Board board, Player player, int depth, int alpha, int beta)
        {
            hits++;

            if (depth == 0 || board.Winner != Player.None)
            {
                return(new Result(null, evaluator.Run(board, player)));
            }

            int prevalpha = alpha;

            Transposition transposition = transTable.Lookup(board, player);

            if (transposition != null && transposition.Depth >= depth)
            {
                switch (transposition.Bound)
                {
                case EvaluationBound.Lower:
                    alpha = Math.Max(alpha, transposition.BestMove.Value);
                    break;

                case EvaluationBound.Upper:
                    beta = Math.Min(beta, transposition.BestMove.Value);
                    break;

                case EvaluationBound.Accurate:
                    alpha = beta = transposition.BestMove.Value;
                    break;
                }

                if (alpha >= beta)
                {
                    return(transposition.BestMove);
                }
            }

            List <Move> possibleMoves = board.GetPossibleMovesSorted(player);

            if (depth == start)
            {
                total = possibleMoves.Count;
            }

            if (transposition != null)
            {
                int index = possibleMoves.IndexOf(transposition.BestMove.Move);
                if (index == -1)
                {
                    throw new Exception(String.Format("Move \"{0}\" not found.", transposition.BestMove.Move));
                }
                possibleMoves[index] = possibleMoves[0];
                possibleMoves[0]     = transposition.BestMove.Move;
            }

            Result best = new Result(null, -inf);

            foreach (Move move in possibleMoves)
            {
                Transition transition = board.PerformMove(move);

                Result candidate = Run(evaluator, board, BackendHelper.SwapPlayer(player), depth - 1, -beta, -alpha);

                board.ReverseTransition(transition);

                if (-candidate.Value > best.Value)
                {
                    best = new Result(move, -candidate.Value);
                }

                if (best.Value >= beta)
                {
                    break;
                }

                if (best.Value > alpha)
                {
                    alpha = best.Value;
                }

                if (depth == start)
                {
                    done++;
                }
            }

            transTable.Save(board, best, prevalpha, beta, depth);

            return(best);
        }