Dice.class.php 796 B

12345678910111213141516171819202122232425262728
  1. <?php
  2. class Dice
  3. {
  4. protected $figures = [1, 2, 3, 4, 5, 6];
  5. public function play($number = 2, $total = false)
  6. {
  7. // array_rand($a)
  8. $dices = [];
  9. $min = min($this->figures);
  10. if ($total === false || ($min * $number > $total)) {
  11. for ($i = 0; $i < $number; $i++) {
  12. $dices[] = $this->figures[array_rand($this->figures)];
  13. }
  14. } else {
  15. $max = max($this->figures);
  16. for ($i = 0; $i < $number; $i++) {
  17. $dices[] = $min;
  18. }
  19. while (array_sum($dices) < $total) {
  20. $key = array_rand($dices);
  21. if ($dices[$key] < $max) {
  22. $dices[$key]++;
  23. }
  24. }
  25. }
  26. return $dices;
  27. }
  28. }