Beispiel #1
0
        public Move GetMove(ProcessData data, TimeSpan timeout)
        {
            var turn = this.Turn + 1;

            Run(data, timeout, turn);
            this.Stopwatch.Stop();
            Console.WriteLine();

            return(new Move(this.BestMove, LogResult()));
        }
Beispiel #2
0
        public ScoreCollection Process(ProcessData data, int turn, ScoreCollection alphas)
        {
            var map          = data.Map;
            var resultAlphas = PotentialScore.EmptyCollection;

            if (this.Turn >= 1199 || turn <= this.Turn)
            {
                this.Score = data.Evalutor.Evaluate(this.State);
                alphas.ContinueProccesingAlphas(this.Score, this.PlayerToMove, out resultAlphas);
                return(resultAlphas);
            }

            if (this.Children == null)
            {
                this.Children = new List <Node>();

                foreach (var dir in data.MoveGenerator.Generate(map, this.State))
                {
                    var state = this.State.Move(map, dir, this.PlayerToMove);
                    var child = data.Lookup.Get(this.Turn + 1, state, dir);
                    this.Children.Add(child);
                }
            }

            var test = PotentialScore.EmptyCollection;

            for (int i = 0; i < this.Children.Count; i++)
            {
                var child = this.Children[i];
                switch (i)
                {
                case 0: test = child.Process(data, turn - 0, alphas); break;

                case 1: test = child.Process(data, turn - 1, alphas); break;

                case 2: test = child.Process(data, turn - 2, alphas); break;

                case 3: test = child.Process(data, turn - 3, alphas); break;

                default:
                case 4: test = child.Process(data, turn - 5, alphas); break;
                }

                if (!alphas.ContinueProccesingAlphas(test, this.PlayerToMove, out resultAlphas))
                {
                    break;
                }
            }
            var comparer = NodeComparer.Get(this.PlayerToMove);

            Children.Sort(comparer);
            this.Score = this.Children[0].Score;

            return(resultAlphas);
        }
Beispiel #3
0
        /// <summary>Runs the calculations.</summary>
        /// <remarks>
        /// Runs in a different thread.
        /// </remarks>
        private void Run(ProcessData data, TimeSpan timeout, int turn)
        {
            try
            {
                var source = new CancellationTokenSource((int)timeout.TotalMilliseconds);

                var task = Task.Factory.StartNew(() =>
                {
                    while (turn < 1200)
                    {
                        this.Process(data, turn++, PotentialScore.EmptyCollection);
                        LogResult();

                        // Was cancellation already requested?
                        if (source.IsCancellationRequested == true)
                        {
                            source.Token.ThrowIfCancellationRequested();
                        }
                    }
                }, source.Token);
                if (task.Wait((int)timeout.TotalMilliseconds))
                {
                    source.Cancel();
                }
            }
            catch (OperationCanceledException) { }
            catch (AggregateException) { }

            catch (Exception x)
            {
                Console.WriteLine(x);
#if DEBUG
                throw;
#endif
            }
        }