Exemplo n.º 1
0
    private void ActuateTile(DigTile tile)
    {
        GameTile gameTile = Instantiate(TileObject, AnchorsWrapper);

        gameTile.gameObject.SetActive(true);
        gameTile.SetValue(tile.Value);

        Coord anchorPosition = tile.PreviousPosition ?? new Coord(tile.X, tile.Y);

        gameTile.transform.localPosition = anchors[anchorPosition.y, anchorPosition.x].localPosition;

        if (tile.PreviousPosition != null)
        {
            StartCoroutine(ActuateTileToPosition(gameTile.transform, anchorPosition, new Coord(tile.X, tile.Y)));
        }
        else if (tile.MergedFrom != null)
        {
            StartCoroutine(TileAddMerge(gameTile.transform));
            foreach (DigTile merged in tile.MergedFrom)
            {
                ActuateTile(merged);
            }
            gameTile.transform.localPosition += new Vector3(0f, 0.00015f, 0f);
        }
        else
        {
            StartCoroutine(TileAddNew(gameTile.transform));
        }

        ActuatedTiles.Add(gameTile);
    }
Exemplo n.º 2
0
    private bool TileMatchesAvailable()
    {
        for (int y = 0; y < Size; y++)
        {
            for (int x = 0; x < Size; x++)
            {
                DigTile tile = grid.CellContent(new Coord(x, y));

                if (tile != null)
                {
                    for (int direction = 0; direction < 4; direction++)
                    {
                        Coord vector = GetDirectionalOffset((Direction)direction);
                        Coord cell   = new Coord(x + vector.x, y + vector.y);

                        DigTile other = grid.CellContent(cell);

                        if (other != null && other.Value == tile.Value)
                        {
                            return(true);
                        }
                    }
                }
            }
        }
        return(false);
    }
Exemplo n.º 3
0
 public DigTile(DigTile tileToClone)
 {
     Value            = tileToClone.Value;
     X                = tileToClone.X;
     Y                = tileToClone.Y;
     PreviousPosition = tileToClone.PreviousPosition;
     MergedFrom       = tileToClone.MergedFrom;
 }
Exemplo n.º 4
0
 public GridLogTile(DigTile tile)
 {
     value            = tile.Value;
     x                = tile.X;
     y                = tile.Y;
     previousPosition = tile.PreviousPosition;
     mergedFrom       = tile.MergedFrom == null ? null : tile.MergedFrom.Select(t => new GridLogTile(t)).ToList();
 }
Exemplo n.º 5
0
 internal void AddRandomTile(int?valueOverride = null)
 {
     if (grid.CellsAvailable())
     {
         int     value = valueOverride == null ? Random.value < 0.9f ? 2 : 4 : (int)valueOverride;
         DigTile tile  = new DigTile(grid.RandomAvailableCell(), value);
         grid.InsertTile(tile);
     }
 }
Exemplo n.º 6
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));
    }
Exemplo n.º 7
0
 public void RemoveTile(DigTile tile)
 {
     Cells[tile.Y, tile.X] = null;
 }
Exemplo n.º 8
0
 public void InsertTile(DigTile tile)
 {
     Cells[tile.Y, tile.X] = tile;
 }
Exemplo n.º 9
0
 private static bool PositionsEqual(Coord first, DigTile second)
 {
     return(PositionsEqual(first, new Coord(second.X, second.Y)));
 }
Exemplo n.º 10
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();
            }
        }
    }
Exemplo n.º 11
0
 private static void MoveTile(Grid2048 grid, DigTile tile, Coord cell)
 {
     grid.Cells[tile.Y, tile.X] = null;
     grid.Cells[cell.y, cell.x] = tile;
     tile.UpdatePosition(cell);
 }
Exemplo n.º 12
0
 private void MoveTile(DigTile tile, Coord cell)
 {
     MoveTile(grid, tile, cell);
 }