Ejemplo n.º 1
0
    private void Start()
    {
        NonError.SetActive(true);
        ErrorDefault.SetActive(false);

        Log("Setup [{0}]", Size);
        HideDirectionHighlights(false);

        grid = new Grid2048(Size);

        Win2048.SetActive(false);

        if (!IsEditor)
        {
            VersionLabel.text = Version;
        }

        tileScale = TileObject.transform.localScale.x;
        TileObject.gameObject.SetActive(false);
        CreateAnchors();

        Reset();

        for (int i = 0; i < DirectionButtons.Length; i++)
        {
            Direction direction = (Direction)i;
            DirectionButtons[i].OnInteract += delegate
            {
                MoveDirection(direction);
                return(false);
            };
        }

        Get <KMSelectable>().Assign(onInteract: OnInteract, onDefocus: OnDefocus);
    }
Ejemplo n.º 2
0
    public Snapshot(Grid2048 grid, int score)
    {
        this.tiles       = new Dictionary <Vector2Int, Tile> (grid.GridLength);
        this.freeSquares = new List <Vector2Int> (grid.FreeSquares);
        this.score       = score;

        DestroySnapshotTiles();
        SnapshotTileGameObjects(grid);
    }
Ejemplo n.º 3
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));
    }
Ejemplo n.º 4
0
    private void InitializeGrid()
    {
        Camera mainCamera = Camera.main;

        Vector2 gridOrigin = mainCamera.transform.position;

        mainCamera.orthographicSize = gridProperties.gridLength + 1; // Camera size is adjusted so tiles stay in frame.
        grid = new Grid2048(gridProperties, gridOrigin);

        SpawnTiles();
    }
Ejemplo n.º 5
0
    private void SnapshotTileGameObjects(Grid2048 grid)
    {
        foreach (KeyValuePair <Vector2Int, Tile> entry in grid.Tiles)
        {
            GameObject clonedGameObject = entry.Value.Clone();

            clonedGameObject.SetActive(false);
            clonedGameObject.transform.parent = snapshotContainer.transform;
            Tile tileComponent = clonedGameObject.GetComponent <Tile> ();
            tiles.Add(entry.Key, tileComponent);
        }
    }
Ejemplo n.º 6
0
    private static FarthestPosition FindFarthestPosition(Grid2048 grid, Coord cell, Coord vector)
    {
        Coord previous;

        do
        {
            previous = cell;
            cell     = new Coord(previous.x + vector.x, previous.y + vector.y);
        } while (grid.WithinBounds(cell) && grid.CellAvailable(cell));

        return(new FarthestPosition(previous, cell));
    }
Ejemplo n.º 7
0
 public Grid2048(Grid2048 grid)
 {
     Size  = grid.Size;
     Cells = new DigTile[Size, Size];
     grid.EachCell((x, y, tile) => Cells[y, x] = tile == null ? null : new DigTile(tile));
 }
Ejemplo n.º 8
0
 public PredictedMove(int score, Grid2048 grid, Direction direction)
 {
     NewScore      = score;
     Grid          = grid;
     MoveDirection = direction;
 }
Ejemplo n.º 9
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);
 }