美文网首页
php实现中秋博饼小游戏

php实现中秋博饼小游戏

作者: Chzzr | 来源:发表于2018-09-08 19:19 被阅读0次

    又到了中秋了,中秋博饼这个习俗,在闽南是比较讨喜的活动,在网上找了一些代码,发现大多是那种利用各种的foreach和if来处理觉得有新的方法可以处理,就自己用PHP码了一个博饼的类出来。

    <?php
    class Bobing {

        private static $result = array(
          0=>'未中奖',1=>'一秀',2=>'二举',3=>'四进',4=>'三红',5=>'对堂',
            6=>'状元',7=>'五子登科',8=>'五红',9=>'遍地锦',10=>'六杯红',11=>'状元插金花',12=>'六杯黑'
        );

        private static $special = array(
            5=>array(1,2,3,4,5,6),
            9=>array(1,1,1,1,1,1),
            10=>array(4,4,4,4,4,4),
            11=>array(1,1,4,4,4,4)
        );

        private static $liuhei = array(
            array(2,2,2,2,2,2),
            array(3,3,3,3,3,3),
            array(5,5,5,5,5,5),
            array(6,6,6,6,6,6),
        );

        private static $wuhong = array(4,4,4,4,4);

        private static $zhuangyuan = array(4,4,4,4);

        private $_dices = null;

        private  $_level = 0;

        public function __construct()
        {
            $this->sharkDices();
        }

        /**摇骰子调用
         * @return $this
         */
        public function sharkDices(){
            $level = $this->sharkItOff();
            $this->_level = $level;
            return $this;
        }

        /**摇奖奖励
         * @return int
         */
        public function sharkItOff(){
            $dices = $this->randDices();
            $this->_dices = $dices;
            $temp = $dices;
            sort($temp);
            //特殊处理
            foreach (self::$special as $key=> $type){
                if($type == $temp){
                    return $key;
                }
            }
            //六黑处理
            foreach (self::$liuhei as $type){
                if($type == $temp){
                    return 12;
                }
            }
            //等级筛选
            $temp = $this->delDice($temp);
            //统计等级剔除剩余骰子数
            $count = count($temp);
            switch ($count){
                case 0:
                    return 0;//未中奖
                    break;
                case 1:
                    return 1;//一秀
                    break;
                case 2:
                    return 2;//二举
                    break;
                case 3:
                    return 4;//三红
                    break;
                case 4:
                    if($temp == self::$zhuangyuan){
                        return 6;//状元
                    }
                    return 3;//四进
                    break;
                case 5:
                    if($temp == self::$wuhong){//五红
                        return 8;
                    }
                    return 7;//五子登科
                    break;
                default:
                    break;
            }
            return 0;
        }

        /**获取骰子
         * @return null
         */
        public function getDices(){
            return $this->_dices;
        }

        public function getLevel(){
            return $this->_level;
        }

        public function getReward(){
            return self::$result[$this->_level];
        }


        /**获取骰子
         * @return array
         */
        private function randDices(){
            $dices = [];
            for ($i=0;$i<6;$i++){
                $dices[] = rand(1,6);
            }
            return $dices;
        }

        /**计算骰子等级,去除骰子
         * @param array $dices
         * @return array
         */
        private function delDice(array $dices){
            $temp = $dices[0];
            $i = 1;
            $max = 1;
            $maxDeces = $dices[0];
            foreach ($dices as $key=>$dice){
                if($key == 0){
                    continue;
                }
                if($dice == $temp){
                    $i += 1;
                }else{
                    if($i >= $max){
                        $max = $i;
                        $maxDeces = $temp;
                    }
                    $i = 1;
                    $temp = $dice;
                }
            }
            if($i >= $max){
                $max = $i;
                $maxDeces = $temp;
            }
            if($max < 4){
                $maxDeces = 4;
            }
            foreach ($dices as $key=>$dice){
                if($dice == $maxDeces){
                    continue;
                }
                unset($dices[$key]);
            }
            sort($dices);
            return $dices;
        }


    }

    不喜勿喷,留言共勉!!!

    相关文章

      网友评论

          本文标题:php实现中秋博饼小游戏

          本文链接:https://www.haomeiwen.com/subject/cojlgftx.html