Ejemplo n.º 1
0
        //Meが動くとする。「Meのスコア - Enemyのスコア」の最大値を返す。
        //NegaMaxではない
        private int NegaMax(int deepness, SearchState state, int alpha, int count, PointEvaluator.Base evaluator, Decision ngMove, int nowAgent)
        {
            if (deepness == 0)
            {
                return(evaluator.Calculate(ScoreBoard, state, 0));
            }
            if (CancellationToken.IsCancellationRequested == true)
            {
                return(alpha);
            }                                                                           //何を返しても良いのでとにかく返す
            SingleAgentWays ways = state.MakeMovesSingle(AgentsCount, nowAgent, ScoreBoard);

            for (int i = 0; i < ways.Count; ++i)
            {
                var way = ways.Data[i];
                if (count == 0 && !(ngMove is null))    //競合手とは違う手を指す
                {
                    if (way == ngMove.Agents[nowAgent])
                    {
                        continue;
                    }
                }

                SearchState newState = state.GetNextStateSingle(nowAgent, way, ScoreBoard, deepness);

                //自エージェントとの衝突を防ぐ
                if (count == 0)
                {
                    int j = 0;
                    for (j = 0; j < AgentsCount; ++j)
                    {
                        if (j == nowAgent)
                        {
                            continue;
                        }
                        if (ngMove is null && dp1[count][j] == way)
                        {
                            break;
                        }
                        if (!(ngMove is null) && dp2[count][j] == way)
                        {
                            break;
                        }
                    }
                    if (j != AgentsCount)
                    {
                        continue;
                    }
                }
                int res = NegaMax(deepness - 1, newState, alpha, count + 1, evaluator, ngMove, nowAgent);
                if (alpha < res)
                {
                    alpha = res;
                    if (ngMove is null)
                    {
                        dp1[count][nowAgent] = way;
                    }
                    else
                    {
                        dp2[count][nowAgent] = way;
                    }
                }
            }

            //Log("NODES : {0} nodes, elasped {1} ", i, sw.Elapsed);
            ways.End();
            return(alpha);
        }