Advertisement
repente

Untitled

May 5th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Mos\Dice;
  4.  
  5. /**
  6. * Showing off a standard class with methods and properties.
  7. */
  8. class Dice
  9. {
  10. /**
  11. * Constructor to create a Dice.
  12. *
  13. * @var integer $arrayRand sets the array length.
  14. * @var integer $lastRoll Value of the last roll.
  15. */
  16. public $arrayRand;
  17. private $lastRoll;
  18.  
  19.  
  20. public function __construct($count=6){
  21. $this->arrayRand = $this->generateRandomArr($count);
  22. $this->lastRoll = end($this->arrayRand);
  23. }
  24.  
  25. /**
  26. * Generate an array of random numbers.
  27. *
  28. * @param int $count Sets the length of an array of random numbers.
  29. *
  30. * @return array
  31. */
  32. public function generateRandomArr($count) {
  33. $random = [];
  34. for ($i = 0; $i < $count; $i++) {
  35. $random[] = rand(1, 6);
  36. }
  37. return $random;
  38. }
  39.  
  40.  
  41. /**
  42. * Returns an array of data
  43. *
  44. * @return array
  45. */
  46.  
  47. public function getRand(){
  48. return $this->arrayRand;
  49. }
  50.  
  51. /**
  52. * Output array to html code
  53. *
  54. * @return string ul html tag
  55. */
  56.  
  57. public function viewRand() {
  58. $html = "<ol>\n";
  59. foreach ($this->getRand() as $value) {
  60. $html .= " <li>$value</li> \n";
  61. }
  62. $html .= "</ol> \n";
  63. return $html;
  64. }
  65.  
  66. /**
  67. * Array sum
  68. *
  69. * @return integer
  70. */
  71.  
  72. public function summRand() {
  73. return array_sum($this->getRand());
  74. }
  75.  
  76. /**
  77. * Average value series random
  78. *
  79. * @return float
  80. */
  81.  
  82. public function averageRand() {
  83. return round($this->summRand() / count($this->getRand()), 1);
  84. }
  85.  
  86. /**
  87. * Roll die
  88. *
  89. * @return integer random num
  90. */
  91. public function roll(){
  92. $temp = array_pop ( $this->arrayRand );
  93. $this->lastRoll = $temp;
  94. return $temp;
  95. }
  96.  
  97. /**
  98. * Get the last throw
  99. *
  100. * @return int last throw
  101. */
  102. public function getLastRoll() {
  103. return $this->lastRoll;
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement