コード例 #1
0
ファイル: Solver1.cs プロジェクト: marktanner1331/2048-Solver
        public unsafe Direction Solve(byte *grid)
        {
            byte *[] grids = new byte *[4];
            for (int i = 0; i < 4; i++)
            {
                grids[i] = GridFunctions.CloneGrid(grid);
            }

            Dictionary <Direction, int> scores = new Dictionary <Direction, int>();

            int j = 0;

            foreach (Direction direction in Enum.GetValues(typeof(Direction)))
            {
                if (GridFunctions.GridCanCollapse(grid, direction) == false)
                {
                    scores[direction] = -1;
                    continue;
                }

                GridFunctions.CollapseGridInPlace(grids[j], direction);
                scores[direction] = GridFunctions.CountEmptySquares(grids[j]);
            }

            return(scores.OrderByDescending(x => x.Value).First().Key);
        }
コード例 #2
0
ファイル: Solver2.cs プロジェクト: marktanner1331/2048-Solver
        public unsafe Direction Solve(byte *grid)
        {
            GridStack stack = new GridStack(128);

            GridFunctions.CloneGrid(grid, stack.current);

            Dictionary <Direction, int> scores = new Dictionary <Direction, int>();

            foreach (Direction direction in Enum.GetValues(typeof(Direction)))
            {
                //Console.WriteLine(direction);
                if (GridFunctions.GridCanCollapse(grid, direction) == false)
                {
                    //Console.WriteLine("\tCant Collapse");
                    continue;
                }

                stack.pushCurrent();
                GridFunctions.CollapseGridInPlace(stack.current, direction);

                scores[direction] = ScoreForGrid(stack, 1);

                stack.pop();
                //Console.WriteLine("\tScore: " + scores[direction]);
            }

            return(scores.OrderByDescending(x => x.Value).First().Key);
        }
コード例 #3
0
        public unsafe Direction Solve(byte *grid)
        {
            this.current = data;

            int[] scores   = new int[4];
            byte *tempGrid = GridFunctions.CreateGrid();

            for (int direction = 0; direction < 4; direction++)
            {
                //Console.WriteLine(direction);
                if (GridFunctions.GridCanCollapse(grid, (Direction)direction) == false)
                {
                    //Console.WriteLine("\tCant Collapse");
                    //GridFunctions.printGrid(grid);
                    scores[(int)direction] = 0;
                    continue;
                }

                GridFunctions.CloneGrid(grid, tempGrid);
                GridFunctions.CollapseGridInPlace(tempGrid, (Direction)direction);

                int       permutations = GridFunctions.CountEmptySquares(grid);
                const int depth        = 2;

                if (permutations > 5)
                {
                    scores[direction] = ScoreForGrid(tempGrid, depth);
                }
                else if (permutations < 3)
                {
                    scores[direction] = ScoreForGrid(tempGrid, depth + 2);
                }
                else
                {
                    scores[direction] = ScoreForGrid(tempGrid, depth + 1);
                }

                //Console.WriteLine("\tScore: " + scores[direction]);
            }

            GridFunctions.FreeGrid(tempGrid);

            int       max    = 0;
            Direction maxDir = Direction.left;

            for (int i = 0; i < 4; i++)
            {
                if (scores[i] > max)
                {
                    max    = scores[i];
                    maxDir = (Direction)i;
                }
            }

            return(maxDir);
            //return scores.OrderByDescending(x => x.Value).First().Key;
        }
コード例 #4
0
ファイル: Solver2.cs プロジェクト: marktanner1331/2048-Solver
        private unsafe int ScoreForGrid(GridStack stack, int depth)
        {
            int permutations = GridFunctions.CountEmptySquares(stack.current);

            if (permutations == 0)
            {
                return(0);
            }

            int startIndex = 0;
            int score      = int.MaxValue;

            for (int i = 0; i < permutations; i++)
            {
                stack.pushCurrent();
                GridFunctions.TryAddPermutation(stack.current, ref startIndex);
                //if(!success)
                //{
                //    //GridFunctions.printGrid(grid);
                //    //GridFunctions.printGrid((grids + i * 16));
                //    //GridFunctions.TryAddPermutation((grids + i * 16), startIndex, out startIndex);
                //    throw new Exception();
                //}

                startIndex++;

                int subScore = -1;
                foreach (Direction subDirection in Enum.GetValues(typeof(Direction)))
                {
                    stack.pushCurrent();
                    GridFunctions.CollapseGridInPlace(stack.current, subDirection);

                    if (depth == 1)
                    {
                        subScore = Math.Max(subScore, FinalScoreForGrid(stack.current));
                    }
                    else
                    {
                        subScore = Math.Max(subScore, ScoreForGrid(stack, depth - 1));
                    }

                    stack.pop();
                }

                stack.pop();
                score = Math.Min(score, subScore);
            }

            return(score);
        }
コード例 #5
0
        public Permutator(byte *grid, Direction direction)
        {
            this.grid = GridFunctions.CreateGrid();
            GridFunctions.CloneGrid(grid, this.grid);
            this.direction = direction;

            collapsedGrid = GridFunctions.CreateGrid();
            GridFunctions.CloneGrid(grid, this.collapsedGrid);
            GridFunctions.CollapseGridInPlace(collapsedGrid, direction);

            currentRow    = 0;
            currentColumn = 0;

            GridFunctions.GetDeltas(direction, out offset, out rowDelta, out columnDelta);
        }