Advertisement
repente

Untitled

Jun 19th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Mos\Dice;
  4.  
  5. /**
  6. * Generating histogram data.
  7. */
  8. class Histogram
  9. {
  10. /**
  11. * @var array $serie The numbers stored in sequence.
  12. * @var int $min The lowest possible number.
  13. * @var int $max The highest possible number.
  14. */
  15. private $serie = [];
  16. private $min;
  17. private $max;
  18.  
  19.  
  20.  
  21. /**
  22. * Get the serie.
  23. *
  24. * @return array with the serie.
  25. */
  26. public function getSerie()
  27. {
  28. return $this->serie;
  29. }
  30.  
  31. /**
  32. * Get the min.
  33. *
  34. * @return array with the serie.
  35. */
  36. public function getMin()
  37. {
  38. return $this->min;
  39. }
  40.  
  41. /**
  42. * Get the max.
  43. *
  44. * @return array with the serie.
  45. */
  46. public function getMax()
  47. {
  48. return $this->max;
  49. }
  50.  
  51. /**
  52. * Return a string with a textual representation of the histogram.
  53. *
  54. * @return string representing the histogram.
  55. */
  56. public function getAsText()
  57. {
  58. $min = $this->getMin();
  59. $max = $this->getMax();
  60. $dataHistogram = array_count_values($this->getSerie());
  61.  
  62. if ($min && $max) {
  63. for ($i = $min; $i <= $max; $i++) {
  64. if (array_key_exists($i, $dataHistogram)) {
  65. echo ' <br>' . "$i. " . str_repeat("*", $dataHistogram[$i]);
  66. } else {
  67. echo ' <br>' . "$i. ";
  68. }
  69. }
  70. } else {
  71. foreach ($dataHistogram as $key => $value) {
  72. echo ' <br>' . "$key. " . str_repeat("*", $value);
  73. }
  74. }
  75. }
  76.  
  77. /**
  78. * Foreign interface binding method
  79. *
  80. * @return void
  81. */
  82. public function injectData(HistogramInterface $object)
  83. {
  84. $this->serie = $object->getHistogramSerie();
  85. $this->min = $object->getHistogramMin();
  86. $this->max = $object->getHistogramMax();
  87. }
  88. }
  89.  
  90.  
  91. /**
  92. * A dice which has the ability to present data to be used for creating
  93. * a histogram.
  94. */
  95. class DiceHistogram2 extends Dice implements HistogramInterface
  96. {
  97. use HistogramTrait2;
  98.  
  99.  
  100.  
  101. /**
  102. * Get max value for the histogram.
  103. *
  104. * @return int with the max value.
  105. */
  106. public function getHistogramMax()
  107. {
  108. return count($this->arrayRand);
  109. }
  110.  
  111.  
  112.  
  113. /**
  114. * Roll the dice, remember its value in the serie and return
  115. * its value.
  116. *
  117. * @return int the value of the rolled dice.
  118. */
  119. public function roll()
  120. {
  121. $this->serie[] = parent::roll();
  122. return $this->getLastRoll();
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement