美文网首页
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实现中秋博饼小游戏

    又到了中秋了,中秋博饼这个习俗,在闽南是比较讨喜的活动,在网上找了一些代码,发现大多是那种利用各种的foreach...

  • 博饼趣谈

    中秋博饼是厦门重要的节日活动,每次临近中秋,家家户户都会提前拿出博饼用的大碗和骰子,在博饼声中开启中秋节,这个博饼...

  • PHP实现vivo小游戏授权登录

    PHP实现vivo小游戏授权登录

  • 成功日记

    中秋博饼博到状元

  • 中秋博饼

    博饼(跋饼),是福建闽南地区和台湾几百年来独有的中秋传统活动,是一种独特的月饼文化,也是闽南人对历史的一种传...

  • 中秋博饼

  • 周检视第11周  反复确认的才是你想要的

    一年一度的中秋前夕,这一周,参加了好多公司的博饼活动。博饼风俗来源于厦门,蔓延整个福建,中秋前后都是博饼的高峰,博...

  • 有趣的生活 684 -- 中秋博饼,博是彩头,饼是团圆!

    中秋佳节,月圆人团圆!在各地都有着不同的风俗,在福建这里有一种特殊活动叫博饼! 博饼由来 中秋博饼,是厦门人几百年...

  • 厦门中秋博饼

    小时候中秋节家里会准备一桌的水果及糕饼,摆在天井点香供拜“月娘妈”(月亮)。每到那天的夜色很美,圆月晴空,满天的...

  • 中秋博饼乐

    中秋节休假三天,新初一的孩子有一项作业:调查各地的中秋习俗。 上网一查,才知道各地庆祝中秋节的仪式可谓百里异习:广...

网友评论

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

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