Advertisement
repente

Untitled

Jun 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 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. /**
  33. * Return a string with a textual representation of the histogram.
  34. *
  35. * @return string representing the histogram.
  36. */
  37. public function getAsText()
  38. {
  39. $dataHistogram = array_count_values($this->getSerie());
  40.  
  41. if ($this->min && $this->max) {
  42. for ($i = $this->min; $i <= $this->$max; $i++) {
  43. if (array_key_exists($i, $dataHistogram)) {
  44. echo ' <br>' . "$i. " . str_repeat("*", $dataHistogram[$i]);
  45. } else {
  46. echo ' <br>' . "$i. ";
  47. }
  48. }
  49. } else {
  50. foreach ($dataHistogram as $key => $value) {
  51. echo ' <br>' . "$key. " . str_repeat("*", $value);
  52. }
  53. }
  54. }
  55.  
  56. /**
  57. * Foreign interface binding method
  58. *
  59. * @return void
  60. */
  61. public function injectData(HistogramInterface $object)
  62. {
  63. $this->serie = $object->getHistogramSerie();
  64. $this->min = $object->getHistogramMin();
  65. $this->max = $object->getHistogramMax();
  66. }
  67. }
  68.  
  69.  
  70. /**
  71. * A dice which has the ability to present data to be used for creating
  72. * a histogram.
  73. */
  74. class DiceHistogram2 extends Dice implements HistogramInterface
  75. {
  76. use HistogramTrait2;
  77.  
  78.  
  79.  
  80. /**
  81. * Get max value for the histogram.
  82. *
  83. * @return int with the max value.
  84. */
  85. public function getHistogramMax()
  86. {
  87. return count($this->arrayRand);
  88. }
  89.  
  90.  
  91.  
  92. /**
  93. * Roll the dice, remember its value in the serie and return
  94. * its value.
  95. *
  96. * @return int the value of the rolled dice.
  97. */
  98. public function roll()
  99. {
  100. $this->serie[] = parent::roll();
  101. return $this->getLastRoll();
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement