php实现的中秋博饼游戏之掷骰子并输出结果功能详解


Posted in PHP onNovember 06, 2017

本文实例讲述了php实现的中秋博饼游戏之掷骰子并输出结果功能。分享给大家供大家参考,具体如下:

前面讲述了php实现的中秋博饼游戏之绘制骰子图案功能,纯php实现,就要用php来生成图案,第一步就先绘制骰子图案。下面就是编码实现业务逻辑,具体代码如下:

<?php
class roll
{
  private $_defRank = 'lk';
  public function lottery()
  {
    $dice   = $this->rollDice();
    $format  = $this->formatDice($dice);
    $rank   = $this->getRank($format);
    $rankName = $this->getName($rank);
    return [
      'dice'   => $dice,
      //'format'  => $format,
      'rank'   => $rank,
      'rankName' => $rankName,
    ];
  }
  /**
   * 获取筛子排名结果
   * @param $dice
   * @return array
   */
  public function getRes($dice)
  {
    $format  = $this->formatDice($dice);
    $rank   = $this->getRank($format);
    $rankName = $this->getName($rank);
    return [
      'dice'   => $dice,
      'format'  => $format,
      'rank'   => $rank,
      'rankName' => $rankName,
    ];
  }
  /**
   * 掷骰子
   * @return array
   */
  public function rollDice()
  {
    $res = [];
    for ($i = 0; $i < 6; $i++) {
      $res[] = mt_rand(1, 6);
    }
    return $res;
  }
  /**
   * 格式化掷骰子结果
   * @param array $list
   * @return array
   */
  public function formatDice($list = [])
  {
    $data = [];
    if (count($list) != 6) {
      return $data;
    }
    $data = [
      1 => 0,
      2 => 0,
      3 => 0,
      4 => 0,
      5 => 0,
      6 => 0,
    ];
    foreach ($list as $val) {
      if (isset($data[$val])) {
        $data[$val] += 1;
      }
    }
    foreach ($data as $key => $val) {
      if ($val == 0) {
        unset($data[$key]);
      }
    }
    return $data;
  }
  /**
   * 判断筛子结果的大小
   * @param $list
   * @return int|string
   */
  public function getRank($list)
  {
    $ruleList = $this->_getRule();
    $res   = $this->_defRank;
    if (!empty($ruleList)) {
      foreach ($ruleList as $rank => $rankRules) {
        foreach ($rankRules as $rule) {
          foreach ($rule as $dian => $num) {
            if (isset($list[$dian])) {
              if ($list[$dian] == $num) {
                $res = $rank;
              } else {
                //规则中只要有一条不满足就跳出当前规则验证
                $res = $this->_defRank;
                break;
              }
            } else {
              //规则中只要有一条不满足就跳出当前规则验证
              $res = $this->_defRank;
              break;
            }
          }
          //有一条规则匹配,跳出循环,
          if ($res != $this->_defRank) {
            break;
          }
        }
        //有一条规则匹配,跳出循环,
        if ($res != $this->_defRank) {
          break;
        }
      }
    }
    return $res;
  }
  /**
   * 根据排序获取掷骰子结果名称
   * @param int $rank
   * @return array
   */
  public function getName($rank = NULL)
  {
    $list = [
      'cjh'  => '状元插金花',
      'lbh'  => '六杯红',
      'bdj'  => '遍地锦',
      'ww'  => '五王',
      'wzdyx' => '五子带一秀',
      'wzdk' => '五子登科',
      'zy'  => '状元',
      'by'  => '榜眼',
      'sh'  => '三红',
      'sj'  => '四进',
      'eq'  => '二举',
      'yx'  => '一秀',
      'lk'  => '轮空',
    ];
    if (!empty($rank)) {
      $rankName = '';
      if (isset($list[$rank])) {
        $rankName = $list[$rank];
      }
      return $rankName;
    }
    return $list;
  }
  /**
   * 返回规则
   * @return array
   */
  private function _getRule()
  {
    return [
      'cjh'  => [
        [2 => 2, 4 => 4]
      ],
      'lbh'  => [
        [4 => 6]
      ],
      'bdj'  => [
        [1 => 6],
        [2 => 6],
        [3 => 6],
        [5 => 6],
        [6 => 6],
      ],
      'ww'  => [
        [4 => 5],
      ],
      'wzdyx' => [
        [1 => 5, 4 => 1],
        [2 => 5, 4 => 1],
        [3 => 5, 4 => 1],
        [5 => 5, 4 => 1],
        [6 => 5, 4 => 1],
      ],
      'wzdk' => [
        [1 => 5],
        [2 => 5],
        [3 => 5],
        [5 => 5],
        [6 => 5],
      ],
      'zy'  => [
        [4 => 4]
      ],
      'by'  => [
        [1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1]
      ],
      'sh'  => [
        [4 => 3]
      ],
      'sj'  => [
        [1 => 4],
        [2 => 4],
        [3 => 4],
        [5 => 4],
        [6 => 4],
      ],
      'eq'  => [
        [4 => 2]
      ],
      'yx'  => [
        [4 => 1]
      ],
    ];
  }
}
$roll = new roll();
$res = $roll->lottery();
echo '<h2>骰子点数:</h2>';
echo '<p>';
foreach($res['dice'] as $val){
  echo '<img src="img.php?num='.$val.'" >';
}
echo '</p>';
echo '<h2>结果:</h2>';
echo '<h2 style="color:red;">'.$res['rankName'].'</h2>';

其中img.php是使用php生成图片的文件,参数num是点数,然后输出相应点数的图片,代码如下:

<?php
class imgDock
{
  public function getImg($num = 0)
  {
    if(!empty($num)){
      header('Content-Type:image/png');
      $img  = imagecreatetruecolor(200, 200);
      $white = imagecolorallocate($img, 255, 255, 255);
      $grey = imagecolorallocate($img, 100, 100, 100);
      $blue = imagecolorallocate($img, 0, 102, 255);
      $red  = imagecolorallocate($img, 255, 0, 0);
      imagefill($img, 0, 0, $white);
      imageline($img, 10, 20, 10, 180, $grey);
      imageline($img, 10, 180, 20, 190, $grey);
      imageline($img, 20, 190, 180, 190, $grey);
      imageline($img, 180, 190, 190, 180, $grey);
      imageline($img, 190, 180, 190, 20, $grey);
      imageline($img, 190, 20, 180, 10, $grey);
      imageline($img, 180, 10, 20, 10, $grey);
      imageline($img, 20, 10, 10, 20, $grey);
      //1/2/3/4/5/6
      switch($num){
        case 1:
          imagefilledarc($img, 100, 100, 50, 50, 0, 0, $blue, IMG_ARC_PIE);
          break;
        case 2:
          imagefilledarc($img, 60, 100, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          imagefilledarc($img, 140, 100, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          break;
        case 3:
          imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $blue, IMG_ARC_PIE);
          imagefilledarc($img, 100, 100, 40, 40, 0, 0 , $blue, IMG_ARC_PIE);
          imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $blue, IMG_ARC_PIE);
          break;
        case 4:
          imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          imagefilledarc($img, 50, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          imagefilledarc($img, 150, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          break;
        case 5:
          imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $blue, IMG_ARC_PIE);
          imagefilledarc($img, 50, 150, 40, 40, 0, 0 , $blue, IMG_ARC_PIE);
          imagefilledarc($img, 100, 100, 40, 40, 0, 0 , $blue, IMG_ARC_PIE);
          imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $blue, IMG_ARC_PIE);
          imagefilledarc($img, 150, 50, 40, 40, 0, 0 , $blue, IMG_ARC_PIE);
          break;
        case 6:
          imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          imagefilledarc($img, 50, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          imagefilledarc($img, 100, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          imagefilledarc($img, 100, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          imagefilledarc($img, 150, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE);
          break;
        default:
          break;
      }
      imagepng($img);
      imagedestroy($img);
    }
  }
}
$num = 0;
if(isset($_GET['num'])){
  $num = intval($_GET['num']);
}
$imgDock = new imgDock();
$imgDock->getImg($num);

下面是我抽中状元的效果图,O(∩_∩)O哈哈~

php实现的中秋博饼游戏之掷骰子并输出结果功能详解

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
一个域名查询的程序
Oct 09 PHP
PHP 工厂模式使用方法
May 18 PHP
使用gd库实现php服务端图片裁剪和生成缩略图功能分享
Dec 25 PHP
windwos下使用php连接oracle数据库的过程分享
May 26 PHP
ThinkPHP CURD方法之data方法详解
Jun 18 PHP
php对包含html标签的字符串进行截取的函数分享
Jun 19 PHP
ThinkPHP应用模式扩展详解
Jul 16 PHP
php判断并删除空目录及空子目录的方法
Feb 11 PHP
详解WordPress中过滤链接与过滤SQL语句的方法
Dec 18 PHP
thinkPHP3.2简单实现文件上传的方法
May 16 PHP
PHP正则匹配反斜杠'\'和美元'$'的方法
Feb 08 PHP
解决Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000]
Apr 06 PHP
php实现的中秋博饼游戏之绘制骰子图案功能示例
Nov 06 #PHP
PHP简单实现欧拉函数Euler功能示例
Nov 06 #PHP
Laravel中服务提供者和门面模式的入门介绍
Nov 06 #PHP
php实现的生成迷宫与迷宫寻址算法完整实例
Nov 06 #PHP
使用 laravel sms 构建短信验证码发送校验功能
Nov 06 #PHP
PHP中危险的file_put_contents函数详解
Nov 04 #PHP
PHP回调函数概念与用法实例分析
Nov 03 #PHP
You might like
php中文件上传的安全问题
2006/10/09 PHP
PHP imagecreatefrombmp 从BMP文件或URL新建一图像
2012/07/16 PHP
PHP制作登录异常ip检测功能的实例代码
2016/11/16 PHP
Extjs ajax同步请求时post方式参数发送方式
2009/08/05 Javascript
js实现图片轮换效果代码
2013/04/16 Javascript
深入讲解AngularJS中的自定义指令的使用
2015/06/18 Javascript
JS实现仿雅虎首页快捷登录入口及导航模块效果
2015/09/19 Javascript
基于Jquery和html5的7款个性化地图插件
2015/11/17 Javascript
jquery html动态添加的元素绑定事件详解
2016/05/24 Javascript
纯javascript版日历控件
2016/11/24 Javascript
fullPage.js和CSS3实现全屏滚动效果
2017/05/05 Javascript
JavaScript之iterable_动力节点Java学院整理
2017/06/29 Javascript
深入理解jquery的$.extend()、$.fn和$.fn.extend()
2017/07/08 jQuery
bootstrap select下拉搜索插件使用方法详解
2017/11/23 Javascript
js replace替换字符串同时替换多个方法
2018/11/27 Javascript
使用Three.js实现太阳系八大行星的自转公转示例代码
2019/04/09 Javascript
koa2 从入门到精通(小结)
2019/07/23 Javascript
JS async 函数的含义和用法实例总结
2020/04/08 Javascript
vue项目或网页上实现文字转换成语音播放功能
2020/06/09 Javascript
[01:05:56]Liquid vs VP Supermajor决赛 BO 第二场 6.10
2018/07/04 DOTA
python3使用pyqt5制作一个超简单浏览器的实例
2017/10/19 Python
python:pandas合并csv文件的方法(图书数据集成)
2018/04/12 Python
python批量下载抖音视频
2019/06/17 Python
django 自定义过滤器(filter)处理较为复杂的变量方法
2019/08/12 Python
Python sep参数使用方法详解
2020/02/12 Python
Python图像读写方法对比
2020/11/16 Python
咖啡蛋糕店创业计划书
2014/01/28 职场文书
公司授权委托书范文
2014/09/21 职场文书
“六查”、“三学”、“三干”查摆问题整改措施
2014/09/27 职场文书
教师批评与自我批评(群众路线)
2014/10/15 职场文书
销售经理岗位职责
2015/01/31 职场文书
社区国庆节活动总结
2015/03/23 职场文书
青年干部培训班学习心得体会
2016/01/06 职场文书
导游词之海南-南湾猴岛
2019/10/12 职场文书
springboot临时文件存储目录配置方式
2021/07/01 Java/Android
Win11控制面板快捷键是什么?Win11打开控制面板的方法汇总
2022/07/07 数码科技