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 相关文章推荐
php &amp;&amp; 逻辑与运算符使用说明
Mar 04 PHP
JS 网站性能优化笔记
May 24 PHP
PHP判断文件是否存在、是否可读、目录是否存在的代码
Oct 03 PHP
php中将指针移动到数据集初始位置的实现代码[mysql_data_seek]
Nov 01 PHP
基于MySQL分区性能的详细介绍
May 02 PHP
从零开始学YII2框架(三)扩展插件yii2-gird
Aug 20 PHP
PHP随机生成信用卡卡号的方法
Mar 23 PHP
PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)
Jan 09 PHP
php 数组处理函数extract详解及实例代码
Nov 23 PHP
php头像上传预览实例代码
May 02 PHP
thinkPHP框架实现的无限回复评论功能示例
Jun 09 PHP
PHP之header函数详解
Mar 02 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
PHPUnit安装及使用示例
2014/10/29 PHP
php实现的通用图片处理类
2015/03/24 PHP
PHP工厂模式简单实现方法示例
2018/05/23 PHP
详解PHP版本兼容之openssl调用参数
2018/07/25 PHP
Laravel中正确地返回HTTP状态码方法示例
2019/09/10 PHP
动态创建的表格单元格中的事件实现代码
2008/12/30 Javascript
JavaScript 动态添加表格行 使用模板、标记
2009/10/24 Javascript
dess中一个简单的多路委托的实现
2010/07/20 Javascript
js中判断文本框是否为空的两种方法
2011/07/31 Javascript
Notify - 基于jquery的消息通知插件
2011/10/18 Javascript
window.open关于浏览器拦截问题分析及解决方法
2013/02/05 Javascript
jquery批量控制form禁用的代码
2013/08/06 Javascript
jQuery Trim去除字符串首尾空字符的实现方法说明
2014/02/11 Javascript
jQuery模拟原生态App上拉刷新下拉加载更多页面及原理
2015/08/10 Javascript
jQuery实现Meizu魅族官方网站的导航菜单效果
2015/09/14 Javascript
基于JQuery实现仿网易邮箱全屏动感滚动插件fullPage
2015/09/20 Javascript
学习JavaScript设计模式之中介者模式
2016/01/14 Javascript
Vue2.0如何发布项目实战
2017/07/27 Javascript
详解.vue文件中监听input输入事件(oninput)
2017/09/19 Javascript
layer子层给父层页面元素赋值,以达到向父层页面传值的效果实例
2017/09/22 Javascript
Vue在页面右上角实现可悬浮/隐藏的系统菜单
2018/05/04 Javascript
js 根据对象数组中的属性进行排序实现代码
2019/09/12 Javascript
微信小程序开发搜索功能实现(前端+后端+数据库)
2020/03/04 Javascript
Python计算字符宽度的方法
2016/06/14 Python
Linux 修改Python命令的方法示例
2018/12/03 Python
ASOS亚洲:ASOS Asia
2018/03/04 全球购物
高等教育专业自荐信范文
2014/03/26 职场文书
淘宝客服专员岗位职责
2014/04/11 职场文书
《槐乡五月》教学反思
2014/04/25 职场文书
新学期开学演讲稿
2014/05/24 职场文书
娱乐节目策划方案
2014/06/10 职场文书
医药销售自我评价200字
2014/09/11 职场文书
2016年企业先进员工事迹材料
2016/02/25 职场文书
Sql-Server数据库单表查询 4.3实验课
2021/04/05 SQL Server
php+laravel 扫码二维码签到功能
2021/05/15 PHP
PostgreSQL 插入INSERT、删除DELETE、更新UPDATE、事务transaction
2022/04/12 PostgreSQL