Exemple #1
0
        private unsafe int ScoreForGrid(byte *grid, int depth)
        {
            if (GridFunctions.HasEmptySquares(grid) == false)
            {
                return(GridFunctions.SumValuesInGrid(grid));
            }

            int permutations = GridFunctions.CountEmptySquares(grid);

            int[] scores = new int[permutations];
            current += 16;

            foreach (var subDirection in new[] { (Direction.left, Direction.right), (Direction.up, Direction.down) })
Exemple #2
0
        private unsafe int ScoreForGrid(byte *grid, int depth)
        {
            if (GridFunctions.HasEmptySquares(grid) == false)
            {
                return(GridFunctions.SumValuesInGrid(grid));
            }

            int permutations = GridFunctions.CountEmptySquares(grid);

            int[] scores = new int[permutations];
            current += 16;

            foreach (Direction subDirection in Enum.GetValues(typeof(Direction)))
            {
                Permutator             permutator = new Permutator(grid, subDirection);
                Dictionary <uint, int> scoreCache = new Dictionary <uint, int>();

                int k = 0;
                while (permutator.TryGetPermutation(current, out uint permutationHash))
                {
                    int score;
                    if (scoreCache.ContainsKey(permutationHash))
                    {
                        score = scoreCache[permutationHash];
                    }
                    else
                    {
                        if (depth == 1)
                        {
                            score = FinalScoreForGrid(current);
                        }
                        else
                        {
                            score = ScoreForGrid(current, depth - 1);
                        }

                        scoreCache.Add(permutationHash, score);
                    }

                    scores[k] = Math.Max(scores[k], score);

                    k++;
                }
            }

            current -= 16;
            //GridFunctions.FreeGrid(tempGrid);

            return((int)scores.Average());
        }