Example #1
0
        //Meが動くとする。「Meのスコア - Enemyのスコア」の最大値を返す。
        //NegaMaxではない
        private int NegaMax(int deepness, SearchState state, int alpha, int count, PointEvaluator.Base evaluator, Decision ngMove, Unsafe8Array <Way> nextways, int nowAgent)
        {
            var sw = System.Diagnostics.Stopwatch.StartNew();

            if (deepness == 0)
            {
                return(evaluator.Calculate(ScoreBoard, state.MeBoard, 0, state.Me, state.Enemy) - evaluator.Calculate(ScoreBoard, state.EnemyBoard, 0, state.Enemy, state.Me));
            }

            Ways ways = state.MakeMoves(AgentsCount, ScoreBoard);

            int i = 0;

            foreach (var way in ways.Data[nowAgent])
            {
                if (CancellationToken.IsCancellationRequested == true)
                {
                    return(alpha);
                }                                                                           //何を返しても良いのでとにかく返す
                if (way.Direction == new VelocityPoint())
                {
                    continue;
                }
                i++;

                int j = 0;
                for (j = 0; j < nowAgent; ++j)
                {
                    if (dp[0].Ways[j].Locate == way.Locate)
                    {
                        break;
                    }
                }
                if (j != nowAgent)
                {
                    continue;
                }

                Unsafe8Array <Way> newways = new Unsafe8Array <Way>();
                newways[nowAgent] = way;
                SearchState backup = state;
                state = state.GetNextState(AgentsCount, newways);

                int res = NegaMax(deepness - 1, state, alpha, count + 1, evaluator, ngMove, nextways, nowAgent);
                if (alpha < res)
                {
                    nextways[nowAgent] = way;
                    alpha = res;
                    dp[count].UpdateScore(alpha, nextways);
                }

                state = backup;
            }

            sw.Stop();
            //Log("NODES : {0} nodes, elasped {1} ", i, sw.Elapsed);
            ways.End();
            return(alpha);
        }
Example #2
0
        //Meが動くとする。「Meのスコア - Enemyのスコア」の最大値を返す。
        private int NegaMax(int deepness, SearchState state, int alpha, int beta, int count, PointEvaluator.Base evaluator)
        {
            var sw = System.Diagnostics.Stopwatch.StartNew();

            if (deepness == 0)
            {
                return(evaluator.Calculate(ScoreBoard, state.MeBoard, 0, state.Me, state.Enemy) - evaluator.Calculate(ScoreBoard, state.EnemyBoard, 0, state.Enemy, state.Me));
            }

            Ways ways = state.MakeMoves(AgentsCount, ScoreBoard);

            int i = 0;

            foreach (var way in ways.GetEnumerator(AgentsCount))
            {
                i++;
                if (CancellationToken.IsCancellationRequested == true)
                {
                    return(alpha);
                }                                                                                           //何を返しても良いのでとにかく返す
                SearchState backup = state;
                state = state.GetNextState(AgentsCount, way);
                state = state.ChangeTurn();
                int res = -NegaMax(deepness - 1, state, -beta, -alpha, count + 1, evaluator);
                if (alpha < res)
                {
                    alpha = res;
                    dp[count].UpdateScore(alpha, way);
                    if (alpha >= beta)
                    {
                        return(beta);                                   //βcut
                    }
                }
                state = backup;
            }
            sw.Stop();
            Log("NODES : {0} nodes, elasped {1} ", i, sw.Elapsed);
            ways.End();
            return(alpha);
        }