Beispiel #1
0
    internal static PredictedMove CalculateMove(int size, PredictedMove previousMove, Direction direction)
    {
        int      score        = (int)previousMove.NewScore;
        Grid2048 previousGrid = new Grid2048(previousMove.Grid);

        bool moved = false;

        Coord   cell;
        DigTile tile;

        Coord      vector     = GetDirectionalOffset(direction);
        Traversals traversals = Traversals.BuildTraversals(size, vector);

        foreach (int x in traversals.x)
        {
            foreach (int y in traversals.y)
            {
                cell = new Coord(x, y);
                tile = previousGrid.CellContent(cell);

                if (tile != null)
                {
                    FarthestPosition positions = FindFarthestPosition(previousGrid, cell, vector);
                    DigTile          next      = previousGrid.CellContent(positions.Next);

                    if (next != null && next.Value == tile.Value && next.MergedFrom == null)
                    {
                        DigTile merged = new DigTile(positions.Next, tile.Value * 2)
                        {
                            MergedFrom = new List <DigTile> {
                                tile, next
                            }
                        };

                        previousGrid.InsertTile(merged);
                        previousGrid.RemoveTile(tile);

                        tile.UpdatePosition(positions.Next);

                        score += merged.Value;
                    }
                    else
                    {
                        MoveTile(previousGrid, tile, positions.Farthest);
                    }

                    if (!PositionsEqual(cell, tile))
                    {
                        moved = true;
                    }
                }
            }
        }

        return(new PredictedMove(moved ? score : -1, previousGrid, direction));
    }
Beispiel #2
0
    private void Move(Direction direction)
    {
        Coord   cell;
        DigTile tile;

        Coord      vector     = GetDirectionalOffset(direction);
        Traversals traversals = Traversals.BuildTraversals(Size, vector);
        bool       moved      = false;

        PrepareTiles();

        foreach (int x in traversals.x)
        {
            foreach (int y in traversals.y)
            {
                cell = new Coord(x, y);
                tile = grid.CellContent(cell);

                if (tile != null)
                {
                    FarthestPosition positions = FindFarthestPosition(cell, vector);
                    DigTile          next      = grid.CellContent(positions.Next);

                    if (next != null && next.Value == tile.Value && next.MergedFrom == null)
                    {
                        DigTile merged = new DigTile(positions.Next, tile.Value * 2)
                        {
                            MergedFrom = new List <DigTile> {
                                tile, next
                            }
                        };

                        grid.InsertTile(merged);
                        grid.RemoveTile(tile);

                        tile.UpdatePosition(positions.Next);

                        CurrentScore += merged.Value;

                        if (merged.Value == Goal)
                        {
                            Solve();
                        }
                        if (merged.Value == 2048 && !hasWon2048)
                        {
                            Win2048.SetActive(true);
                            hasWon2048 = justWon2048 = true;
                            HideDirectionHighlights(true);
                        }
                    }
                    else
                    {
                        MoveTile(tile, positions.Farthest);
                    }

                    if (!PositionsEqual(cell, tile))
                    {
                        moved = true;
                    }
                }
            }
        }

        if (moved)
        {
            AddRandomTile();

            Actuate();
            LogGrid(direction);

            if (!MovesAvailable())
            {
                GameOver();
            }
        }
    }