Ejemplo n.º 1
0
    // Clears all active tiles on the grid and re-initializes it with fresh randomized tiles from the tile pool.
    public void ResetGrid()
    {
        _grid.Clear(_tilePool);
        GameSettings.LevelSettings settings = GameSettings.currentLevelSettings;

        _grid.ResizeGrid(settings.gridSize.x + 2, settings.gridSize.y + 2);
        int tileCount = settings.gridSize.x * settings.gridSize.y;

        SpriteTileInfo[] tiles = GetRandomTilePairs(settings.uniqueTileCount, tileCount);

        // Fill the grid leaving cells around the border empty
        for (int x = 1; x < _grid.columns - 1; x++)
        {
            for (int y = 1; y < _grid.rows - 1; y++)
            {
                int i = (y - 1) * (_grid.columns - 2) + (x - 1);

                if (tiles[i] != null)
                {
                    // Grab a new tile from the pool and initalize it
                    SpriteTile tile = _tilePool.Pop();
                    tile.SetSpriteTileInfo(tiles[i]);
                    tile.SetGridNode(_grid[x, y]);
                    tile.SetHighlighted(false);
                    tile.SetColliderEnabled(true);
                }
            }
        }

        // Make sure we haven't spawned a grid with an unwinnable pattern of tiles
        FixUnwinnables();
    }
Ejemplo n.º 2
0
    // Looks for an 'x' pattern of tiles in the grid and swaps the tiles in any it finds so that it can be completed.
    void FixUnwinnables()
    {
        bool done = false;

        // Repeat until no cross patterns are detected
        do
        {
            done = true;

            for (int x = 1; x < _grid.columns - 2; x++)
            {
                for (int y = 1; y < _grid.rows - 2; y++)
                {
                    // Check for 'crosses' of tiles that result in an unwinnable puzzle.
                    // [ n3, n4 ]
                    // [ n1, n2 ]
                    GridNode n1 = _grid[x, y];
                    GridNode n2 = _grid[x + 1, y];
                    GridNode n3 = _grid[x, y + 1];
                    GridNode n4 = _grid[x + 1, y + 1];

                    // Pattern is unwinnable if n1 == n4 and n2 == n3
                    if (n1.tile != null && n2.tile != null && n3.tile != null && n4.tile != null && n1.tile.Matches(n4.tile) && n2.tile.Matches(n3.tile))
                    {
                        // Swap n1 and n2 to move the pattern
                        SpriteTile t = n2.tile;
                        n1.tile.SetGridNode(n2);
                        t.SetGridNode(n1);
                        done = false;
                    }
                }
            }
        } while(!done);
    }