Entertainment on 2nd days -- PHP dice game -- Optimization

Source: Internet
Author: User

In the previous article about the PHP version of dice game, see this), the implementation of the for Loop is obviously very low in efficiency under high concurrency.

The following are the optimization results. If you have a better method, I hope you will not be enlightened!

 
 
  1. <? Php
  2. /**
  3. * Description of Dice
  4. *
  5. * @ Author momosweb (AT) qq.com
  6. */
  7. Class Dice {
  8. Private $ dice_type = 6; // dice type
  9. Private $ number = 1; // number of dice
  10. Private $ people = 1; // number of players
  11. /**
  12. * Set the dice type
  13. * @ Param int $ dice_type
  14. */
  15. Public function set_dice ($ dice_type)
  16. {
  17. $ Dice_type = intval ($ dice_type );
  18. $ This-> dice_type = $ dice_type? $ Dice_type: $ this-> dice_type;
  19. }
  20.  
  21. /**
  22. * Set the number of dice
  23. * @ Param int $ number
  24. */
  25. Public function set_number ($ number)
  26. {
  27. $ Number = intval ($ number );
  28. $ This-> number = $ number? $ Number: $ this-> number;
  29. }
  30. /**
  31. * Set the number of players
  32. * @ Param int $ people
  33. */
  34. Public function set_people ($ people)
  35. {
  36. $ People = intval ($ people );
  37. $ This-> people = $ people? $ People: $ this-> people;
  38. }
  39.  
  40. /**
  41. * Returns the number of dice.
  42. * @ Return intval
  43. */
  44. Private function roll ()
  45. {
  46. Return mt_rand (1, $ this-> dice_type );
  47. }
  48. /**
  49. * Returns the result of the dice game.
  50. * @ Return array
  51. */
  52. Public function result ()
  53. {
  54. // Returns a single result based on the number of people
  55. $ Fun_people = function (){
  56. // Construct the dice Generator
  57. $ Fun_game = function (){
  58. Return $ this-> roll ();
  59. };
  60. // Generate a single result based on the number of dice
  61. $ Arr_number = range (1, $ this-> number );
  62. Return array_map ($ fun_game, $ arr_number );
  63. };
  64. $ Arr_people = range (1, $ this-> people );
  65. $ Result = array_map ($ fun_people, $ arr_people );
  66. Return $ result;
  67. }
  68. }
  69.  
  70. $ Dice = new Dice ();
  71. $ Result = $ Dice-> result ();
  72. Var_dump ($ result );
  73.  
  74. ?>

Haha, it's time for question! If you want to compare the final result, who has the maximum number of points?

This article from the "desert-send entertainment to learn" blog, please be sure to keep this source http://saron.blog.51cto.com/2581496/1175780

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.