src/RandomFiller.php
public static function set(Grid $grid, Tile $tile)
    {
        // Boundaries of the grid within which the tile would fit
        $rows = $grid->rows - $tile->height + 1;
        $columns = $grid->columns - $tile->width + 1;

        // Start at a random position
        $row = rand(0, $rows - 1);
        $column = rand(0, $columns - 1);

        // Record the initial position to prevent infinite loop
        $initialRow = $row;
        $initialColumn = $column;

        // Iterate over grid until an empty spot is found
        while (!$grid->isEmptyForTile($row, $column, $tile)) {
            $column = ($column + 1) % $columns;
            if ($column === 0) {
                $row = ($row + 1) % $rows;
            }
            if ($row === $initialRow && $column === $initialColumn) {
                throw new InvalidArgumentException("No empty slot for {$tile} tile in grid: {$grid}");
            }
        }

        $grid->set($row, $column, $tile);
        return [$row, $column];
    }
public static function set(Grid $grid, Tile $tile)
    {
        // Boundaries of the grid within which the tile would fit
        $rows = $grid->rows - $tile->height + 1;
        $columns = $grid->columns - $tile->width + 1;

        // Start at a random position
        $row = rand(0, $rows - 1);
        $column = rand(0, $columns - 1);

        // Record the initial position to prevent infinite loop
        $initialRow = $row;
        $initialColumn = $column;

        // Iterate over grid until an empty spot is found
        while (!$grid->isEmptyForTile($row, $column, $tile)) {
            $column = ($column + 1) % $columns;
            if ($column === 0) {
                $row = ($row + 1) % $rows;
            }
            if ($row === $initialRow && $column === $initialColumn) {
                throw new InvalidArgumentException("No empty slot for {$tile} tile in grid: {$grid}");
            }
        }

        $grid->set($row, $column, $tile);
        return [$row, $column];
    }
public static function set ( Grid $ grid , Tile $ tile ) { // Boundaries of the grid within which the tile would fit $ rows = $ grid -> rows - $ tile -> height + 1 ; $ columns = $ grid -> columns - $ tile -> width + 1 ; // Start at a random position $ row = rand ( 0 , $ rows - 1 ) ; $ column = rand ( 0 , $ columns - 1 ) ; // Record the initial position to prevent infinite loop $ initialRow = $ row ; $ initialColumn = $ column ; // Iterate over grid until an empty spot is found while ( ! $ grid -> isEmptyForTile ( $ row , $ column , $ tile ) ) { $ column = ( $ column + 1 ) % $ columns ; if ( $ column === 0 ) { $ row = ( $ row + 1 ) % $ rows ; } if ( $ row === $ initialRow && $ column === $ initialColumn ) { throw new InvalidArgumentException ( "No empty slot for {$tile} tile in grid: {$grid}" ) ; } } $ grid -> set ( $ row , $ column , $ tile ) ; return [ $ row , $ column ] ; }
Set a tile randomly within a grid.

@param Grid $grid Grid to fill the tile in.
@param Tile $tile Tile to set.
@return int[] Row and column where the tile was set.
@throws InvalidArgumentException If there is no spot available for the given tile in the grid.
Set a tile randomly within a grid .