example.php 935 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. use LuckyBox\LuckyBox;
  3. use LuckyBox\Card\IdCard;
  4. $loader = require_once __DIR__ . "/../vendor/autoload.php";
  5. $loader->add('LuckyBox\\', __DIR__);
  6. // Items
  7. $items = array(
  8. 1 => array('name' => 'Coin', 'rate' => 60), // 60%
  9. 2 => array('name' => 'Mushroom', 'rate' => 35), // 35%
  10. 3 => array('name' => 'Star', 'rate' => 5), // 5%
  11. );
  12. $luckyBox = new LuckyBox();
  13. // Adds cards into LuckyBox.
  14. foreach ($items as $id => $item) {
  15. $card = new IdCard();
  16. $card->setId($id)
  17. ->setRate($item['rate']);
  18. $luckyBox->add($card);
  19. }
  20. $result = array(
  21. 1 => 0,
  22. 2 => 0,
  23. 3 => 0
  24. );
  25. // Draws the card a hundred times and count up the result.
  26. for ($i = 0; $i < 100; $i++) {
  27. $card = $luckyBox->draw();
  28. $result[$card->getId()]++;
  29. }
  30. // Output the result which must be close to the rate of items.
  31. foreach ($result as $id => $count) {
  32. echo $items[$id]['name'] . ': ' . $count . PHP_EOL;
  33. }