Exemplo n.º 1
0
        public void Random(int w, int h, int BeadTypes, Random rnd = null)
        {
            Random rand = rnd ?? new Random(Guid.NewGuid().GetHashCode());

            Width   = w;
            Height  = h;
            Weights = new List <float>();
            Beads   = new List <int>();

            for (int i = 0; i < BeadTypes; i++)
            {
                Weights.Add(1);
            }

            IBoardEvaluator eval = new ComboEvaluator();

            do
            {
                Beads.Clear();
                for (int i = 0; i < Length; i++)
                {
                    Beads.Add(rand.Next(BeadTypes));
                }
            } while (eval.EvalBoard(Clone()) > 0);
        }
Exemplo n.º 2
0
        public Route GenTask(Board board, CancellationToken ct)
        {
            Random        rand       = new Random(Guid.NewGuid().GetHashCode());
            List <string> Directions = new List <string>();

            int   Score     = 0;
            Route Result    = new Route();
            Board TempBoard = null;

            IBoardEvaluator eval = new ComboEvaluator()
            {
                PerScore = 1000
            };

            int PickX = board.SelectStartX;
            int PickY = board.SelectStartY;

            try
            {
                do
                {
                    Interlocked.Increment(ref Attempts);

                    Directions.Clear();

                    Point CurrentPoint = new Point(PickX, PickY);
                    if (++PickX >= board.SelectEndX)
                    {
                        PickX = 0;
                        if (++PickY >= board.SelectEndY)
                        {
                            PickY = 0;
                        }
                    }

                    Result.StartX = CurrentPoint.X;
                    Result.StartY = CurrentPoint.Y;

                    TempBoard = board.Clone();

                    for (int i = 0; i < board.StepLimit; i++)
                    {
                        ct.ThrowIfCancellationRequested();
                        int DirRnd  = rand.Next(AvailableDir.Length);
                        var dir     = AvailableDir[DirRnd];
                        var BackDir = BackwardDir[DirRnd];
                        if (BackDir == Directions.LastOrDefault())
                        {
                            --i;
                            continue;//don't select backward path
                        }

                        var NextPoint = TempBoard.MoveBeads(CurrentPoint, dir);
                        if (CurrentPoint == NextPoint)//MoveFail
                        {
                            --i;
                            continue;
                        }
                        CurrentPoint = NextPoint;

                        Directions.Add(dir);

                        Score = eval.EvalBoard(TempBoard.Clone());

                        if (Score >= TargetScore)
                        {
                            break;
                        }
                    }
                } while (Score < TargetScore);
                Console.WriteLine($"Finish in {Attempts.ToString()}");
                Console.WriteLine($"TargetScore {TargetScore.ToString()},Final Score {Score.ToString()}");


                Result.Score      = Score;
                Result.Result     = TempBoard.Beads;
                Result.Directions = Directions;

                return(Result);
            }
            catch (OperationCanceledException)
            {
                //Console.WriteLine($"Cancelled Task Id:{Thread.CurrentThread.ManagedThreadId}");
            }

            return(null);
        }