Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Mos\Dice;
- /**
- * Showing off a standard class with methods and properties.
- */
- class Dice
- {
- /**
- * Constructor to create a Dice.
- *
- * @var integer $arrayRand sets the array length.
- * @var integer $lastRoll Value of the last roll.
- */
- public $arrayRand;
- private $lastRoll;
- public function __construct($count=6){
- $this->arrayRand = $this->generateRandomArr($count);
- $this->lastRoll = end($this->arrayRand);
- }
- /**
- * Generate an array of random numbers.
- *
- * @param int $count Sets the length of an array of random numbers.
- *
- * @return array
- */
- public function generateRandomArr($count) {
- $random = [];
- for ($i = 0; $i < $count; $i++) {
- $random[] = rand(1, 6);
- }
- return $random;
- }
- /**
- * Returns an array of data
- *
- * @return array
- */
- public function getRand(){
- return $this->arrayRand;
- }
- /**
- * Output array to html code
- *
- * @return string ul html tag
- */
- public function viewRand() {
- $html = "<ol>\n";
- foreach ($this->getRand() as $value) {
- $html .= " <li>$value</li> \n";
- }
- $html .= "</ol> \n";
- return $html;
- }
- /**
- * Array sum
- *
- * @return integer
- */
- public function summRand() {
- return array_sum($this->getRand());
- }
- /**
- * Average value series random
- *
- * @return float
- */
- public function averageRand() {
- return round($this->summRand() / count($this->getRand()), 1);
- }
- /**
- * Roll die
- *
- * @return integer random num
- */
- public function roll(){
- $temp = array_pop ( $this->arrayRand );
- $this->lastRoll = $temp;
- return $temp;
- }
- /**
- * Get the last throw
- *
- * @return int last throw
- */
- public function getLastRoll() {
- return $this->lastRoll;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement