生活随笔
收集整理的這篇文章主要介紹了
寄娱于学第2天——PHP骰子游戏篇--优化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前一篇關于PHP版本的骰子游戲(詳見此),for循環(huán)的實現,在高并發(fā)下,效率顯然是非常地低的。
下面是優(yōu)化結果,如果您有更好的方法,望不吝賜教!
<?php??????class?Dice?{?????private?$dice_type?=?6;??????private?$number?=?1;?????????private?$people?=?1;??????????????????????public?function?set_dice($dice_type)?????{?????????$dice_type?=?intval($dice_type);?????????$this->dice_type?=?$dice_type???$dice_type?:?$this->dice_type;?????}??????????????public?function?set_number($number)?????{?????????$number?=?intval($number);?????????$this->number?=?$number???$number?:?$this->number;?????}??????????????????public?function?set_people($people)?????{?????????$people?=?intval($people);?????????$this->people?=?$people???$people?:?$this->people;?????}??????????????private?function?roll()?????{?????????return?mt_rand(1,?$this->dice_type);?????}??????????????????public?function?result()?????{?????????????????????$fun_people?=?function()?{??????????????????????????$fun_game?=?function()?{?????????????????return?$this->roll();?????????????};???????????????????????????????????????$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);???>? 哈哈,又到出題時間啦!如果需要比對最終結果,看誰的總點數最大呢?
轉載于:https://blog.51cto.com/saron/1175780
《新程序員》:云原生和全面數字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的寄娱于学第2天——PHP骰子游戏篇--优化的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。