src/GameOfLife/Inputs/RandomInput.php
/**
     * Fills the board with random cells until a specific percentage of the field is filled.
     *
     * @param Board $_board The Board which will be filled
     * @param Getopt $_options The option list
     *
     * @throws \Exception The exception when the fill percentage is invalid
     */
Fills the board with random cells until a specific percentage of the field is filled.
@param Board $_board The Board which will be filled
@param Getopt $_options The option list

@throws \Exception The exception when the fill percentage is invalid
Fills the board with random cells until a specific percentage of the field is filled . @param Board $_board The Board which will be filled @param Getopt $_options The option list @throws \ Exception The exception when the fill percentage is invalid
public function fillBoard(Board $_board, Getopt $_options)
    {
        if ($_options->getOption("fillPercent") !== null) $fillPercent = (float)$_options->getOption("fillPercent");
        else $fillPercent = (float)(rand(15000, 70000) / 1000);
        if ($fillPercent > 100)
        {
            throw new \Exception("There can't be more living cells than 100% of the fields.");
        }
        elseif ($fillPercent < 0)
        {
            throw new \Exception("There can't be less living cells than 0% of the fields.");
        }
        $numberOfSetCells = 0;
        $numberOfFields = $_board->width() * $_board->height();
        while (($numberOfSetCells / $numberOfFields) * 100 < $fillPercent)
        {
            $x = rand(0, $_board->width() - 1);
            $y = rand(0, $_board->height() - 1);
            if ($_board->getFieldStatus($x, $y) == false)
            {
                $_board->setField($x, $y, true);
                $numberOfSetCells++;
            }
        }
    }
public function fillBoard ( Board $ _board , Getopt $ _options ) { if ( $ _options -> getOption ( " fillPercent " ) !== null ) $ fillPercent = ( float ) $ _options -> getOption ( " fillPercent " ) ; else $ fillPercent = ( float ) ( rand ( 15000 , 70000 ) / 1000 ) ; if ( $ fillPercent > 100 ) { throw new \ Exception ( " There can't be more living cells than 100% of the fields. " ) ; } elseif ( $ fillPercent < 0 ) { throw new \ Exception ( " There can't be less living cells than 0% of the fields. " ) ; } $ numberOfSetCells = 0 ; $ numberOfFields = $ _board -> width ( ) * $ _board -> height ( ) ; while ( ( $ numberOfSetCells / $ numberOfFields ) * 100 < $ fillPercent ) { $ x = rand ( 0 , $ _board -> width ( ) - 1 ) ; $ y = rand ( 0 , $ _board -> height ( ) - 1 ) ; if ( $ _board -> getFieldStatus ( $ x , $ y ) == false ) { $ _board -> setField ( $ x , $ y , true ) ; $ numberOfSetCells ++ ; } } }
Fills the board with random cells until a specific percentage of the field is filled.
Fills the board with random cells until a specific percentage of the field is filled .
// Fill the board with random set cells