Beispiel #1
0
        public Tuple <int, int> NextMove(int side)
        {
            Move[] myMove = new Move[256];
            int    index  = 0;

            int[,] newWhiteScores = new int[15, 15];
            int[,] newBlackScores = new int[15, 15];
            for (int I = 0; I < 15; I++)
            {
                for (int J = 0; J < 15; J++)
                {
                    if (MyTable[J, I] == 0)
                    {
                        int selfScore  = 0;
                        int enemyScore = 0;
                        MyTable[J, I]        = side;
                        selfScore            = Evaluate(J, I, side, MyTable); //yx
                        MyTable[J, I]        = (side == 1) ? -1 : 1;
                        enemyScore           = Evaluate(J, I, (side == 1) ? -1 : 1, MyTable);
                        MyTable[J, I]        = 0;
                        myMove[index].x      = I;
                        myMove[index].y      = J;
                        myMove[index].score  = Math.Abs(selfScore) + Math.Abs(enemyScore);
                        newWhiteScores[J, I] = (side == 1) ? selfScore : enemyScore;
                        newBlackScores[J, I] = (side == 1) ? enemyScore : selfScore;
                        if (Math.Abs(selfScore) >= int.Parse(weightTable["____wwwww"][1]))
                        {
                            return(new Tuple <int, int>(myMove[index].y, myMove[index].x));
                        }
                        index++;
                    }
                }
            }
            Array.Sort(myMove, (side == 1) ? (IComparer <Move>)(new MaxScoreFirstComparer()) : (IComparer <Move>)(new MinScoreFirstComparer()));
            int max = int.MinValue;
            int min = int.MaxValue;
            MinMaxFunctionDelegate minMaxFunctionDelegate            = new MinMaxFunctionDelegate(MinMax);
            List <KeyValuePair <int, IAsyncResult> > asyncResultList = new List <KeyValuePair <int, IAsyncResult> >();

            for (int I = 0; I < index && I < 8; I++)
            {
                int[,] nextBoard = (int[, ])MyTable.Clone();
                nextBoard[(myMove[I].y), (myMove[I].x)] = side;
                asyncResultList.Add(new KeyValuePair <int, IAsyncResult>(I, minMaxFunctionDelegate.BeginInvoke(myMove[I].y, myMove[I].x, nextBoard, (side == 1 ? -1 : 1), 0, max, min, newWhiteScores, newBlackScores, null, null)));
                if ((I + 1) % (Environment.ProcessorCount) == 0)
                {
                    WaitAllThreads(asyncResultList, minMaxFunctionDelegate, ref myMove, side, ref max, ref min);
                }
            }
            if (asyncResultList.Any())
            {
                WaitAllThreads(asyncResultList, minMaxFunctionDelegate, ref myMove, side, ref max, ref min);
            }
            Array.Sort(myMove, 0, 8, (side == 1) ? (IComparer <Move>)(new MaxScoreFirstComparer()) : (IComparer <Move>)(new MinScoreFirstComparer()));
            return(new Tuple <int, int>(myMove[0].y, myMove[0].x));
        }
Beispiel #2
0
 private void WaitAllThreads(List <KeyValuePair <int, IAsyncResult> > asyncResultList, MinMaxFunctionDelegate minMaxFunctionDelegate, ref Move[] myMove, int side, ref int max, ref int min)
 {
     WaitHandle.WaitAll(asyncResultList.Select(r => r.Value.AsyncWaitHandle).ToArray());
     foreach (var result in asyncResultList)
     {
         int selfScore = minMaxFunctionDelegate.EndInvoke(result.Value);
         myMove[result.Key].score = selfScore;
         if (side == 1)
         {
             if (selfScore > max)
             {
                 max = selfScore;
             }
         }
         else
         {
             if (selfScore < min)
             {
                 min = selfScore;
             }
         }
     }
     asyncResultList.Clear();
 }