コード例 #1
0
ファイル: TootService.cs プロジェクト: commerble/decode2017
        public IEnumerable <Toot> Load(bool hasFederated, int take = int.MaxValue)
        {
            var accessors = new List <ITootAccessor>
            {
                new TootLocal()
            };

            if (hasFederated)
            {
                accessors.AddRange(_federates.Select(f => new TootRemote(f) as ITootAccessor));
            }
#if NET35
            var sync    = new object();
            var toots   = new List <Toot>();
            var counter = accessors.Count;
            var wait    = new System.Threading.AutoResetEvent(false);

            foreach (var accessor in accessors)
            {
                System.Threading.ThreadPool.QueueUserWorkItem(acs =>
                {
                    var loaded = (acs as ITootAccessor).Load(take);

                    System.Threading.Interlocked.Decrement(ref counter);
                    lock (sync)
                    {
                        if (loaded != null)
                        {
                            toots.AddRange(loaded);
                        }
                    }

                    if (counter <= 0)
                    {
                        wait.Set();
                    }
                }, accessor);
            }
            wait.WaitOne();
#else
            var toots = new System.Collections.Concurrent.ConcurrentBag <Toot>();
            var tasks = accessors.Select(acs => System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                var loaded = (acs as ITootAccessor).Load(take);
                if (loaded != null)
                {
                    foreach (var t in loaded)
                    {
                        toots.Add(t);
                    }
                }
            }));
            System.Threading.Tasks.Task.WaitAll(tasks.ToArray());
#endif
            return(toots.OrderByDescending(t => t.CreateAt).ThenBy(t => t.IsRemote));
        }
コード例 #2
0
        private static int WorkOutMove(Game game, Guid playerID)
        {
            const int MAXDEPTH = 3;

            // What colour are we?
            var weAreYellow = (game.YellowPlayerID == playerID);

            // Which move will give us the highest score?
            var counts = new System.Collections.Concurrent.ConcurrentBag <Tuple <int, int> >();   //Column, Score

            Parallel.For(0, Game.NUMBER_OF_COLUMNS, column =>
            {
                if (IsValidMove(game, column))
                {
                    var nextMove = EffectOfMakingMove(game, weAreYellow, column);
                    var ourScore = MiniMax(nextMove, false, !weAreYellow, MAXDEPTH, int.MinValue, int.MaxValue);
                    counts.Add(new Tuple <int, int>(column, ourScore));
                }
            });

            var bestColumn = counts.OrderByDescending(a => a.Item2).First().Item1;

            if (bestColumn == -1)
            {
                // Just play the first column
                for (int column = 0; column < COLUMNS; column++)
                {
                    if (IsValidMove(game, column))
                    {
                        bestColumn = column;
                        break;
                    }
                }
            }

            return(bestColumn);
        }