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!
- <? Php
- /**
- * Description of Dice
- *
- * @ Author momosweb (AT) qq.com
- */
- Class Dice {
- Private $ dice_type = 6; // dice type
- Private $ number = 1; // number of dice
- Private $ people = 1; // number of players
- /**
- * Set the dice type
- * @ Param int $ dice_type
- */
- Public function set_dice ($ dice_type)
- {
- $ Dice_type = intval ($ dice_type );
- $ This-> dice_type = $ dice_type? $ Dice_type: $ this-> dice_type;
- }
-
- /**
- * Set the number of dice
- * @ Param int $ number
- */
- Public function set_number ($ number)
- {
- $ Number = intval ($ number );
- $ This-> number = $ number? $ Number: $ this-> number;
- }
- /**
- * Set the number of players
- * @ Param int $ people
- */
- Public function set_people ($ people)
- {
- $ People = intval ($ people );
- $ This-> people = $ people? $ People: $ this-> people;
- }
-
- /**
- * Returns the number of dice.
- * @ Return intval
- */
- Private function roll ()
- {
- Return mt_rand (1, $ this-> dice_type );
- }
- /**
- * Returns the result of the dice game.
- * @ Return array
- */
- Public function result ()
- {
- // Returns a single result based on the number of people
- $ Fun_people = function (){
- // Construct the dice Generator
- $ Fun_game = function (){
- Return $ this-> roll ();
- };
- // Generate a single result based on the number of dice
- $ Arr_number = range (1, $ this-> number );
- Return array_map ($ fun_game, $ arr_number );
- };
- $ Arr_people = range (1, $ this-> people );
- $ Result = array_map ($ fun_people, $ arr_people );
- Return $ result;
- }
- }
-
- $ Dice = new Dice ();
- $ Result = $ Dice-> result ();
- Var_dump ($ result );
-
- ?>
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