这篇是纯php实现中秋博饼游戏系列博文(2)

上文是:
纯php实现中秋博饼游戏(1):绘制骰子图案

http://www.cnblogs.com/zqifa/p/php-dice-1.html
要纯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实现中秋博饼游戏(1):绘制骰子图案
http://www.cnblogs.com/zqifa/p/php-dice-1.html
纯php实现中秋博饼游戏(2):掷骰子并输出结果
http://www.cnblogs.com/zqifa/p/php-dice-2.html

done!

纯php实现中秋博饼游戏(2):掷骰子并输出结果的更多相关文章

  1. 纯php实现中秋博饼游戏(1):绘制骰子图案

    最近公司中秋博饼(在厦门),自己没事也想玩玩,所以就想动手写了一个纯php实现的中秋博饼游戏,既然要纯php实现,就要用php来生成图案,所以第一步就先绘制骰子图案. 平时很少使用php绘图,不过查查 ...

  2. tyvj1519博彩游戏

    博彩游戏 From admin 背景 Background Bob最近迷上了一个博彩游戏…… 描述 Description 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到 ...

  3. tyvj P1519 博彩游戏(AC自动机+DP滚动数组)

    P1519 博彩游戏 背景 Bob最近迷上了一个博彩游戏…… 描述 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到一个随机序列:有M个序列,如果某个序列是产生的随机序列的 ...

  4. 如何使用纯 CSS 制作四子连珠游戏

    序言:你是否想过单纯使用 CSS 也可以制作一款游戏?甚至可以双人对决!这是一篇非常有趣的文章,作者详细讲解了使用纯 CSS 制作四子连珠游戏的思路以及使用奇淫巧技解决困难问题的方法.因为案例本身比较 ...

  5. 一款纯css3实现的数字统计游戏

    今天给大家分享一款纯css3实现的数字统计游戏.这款游戏的规则的是将所有的数字相加等于72.这款游戏的数字按钮做得很美观,需要的时候可以借用下.一起看下效果图: 在线预览   源码下载 实现的代码. ...

  6. 博彩游戏(tyvj 1519)

    背景 Bob最近迷上了一个博彩游戏…… 描述 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到一个随机序列:有M个序列,如果某个序列是产生的随机序列的子串,那么就中奖了,否 ...

  7. [JoyOI1519] 博彩游戏

    题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目背景 Bob最近迷上了一个博彩游戏…… 题目描述 这个游戏的规则是这样的:每花一块钱可以 ...

  8. TurnipBit开发板掷骰子小游戏DIY教程实例

    转载请以链接形式注明文章来源(MicroPythonQQ技术交流群:157816561,公众号:MicroPython玩家汇) 0x00前言 下面带大家用TurnipBit开发板实现一个简单的小游戏- ...

  9. 掷骰子游戏窗体实现--Java初级小项目

    掷骰子 **多线程&&观察者模式 题目要求:<掷骰子>窗体小游戏,在该游戏中,玩家初始拥有1000的金钱,每次输入押大还是押小,以及下注金额,随机3个骰子的点数,如果3个骰 ...

随机推荐

  1. .net core 2.2 & Mongodb

    .net core 2.2 API项目中使用Mongodb 简单的CRUD封装 创建FoodPlan.Core 项目 创建IEntityBase.cs 接口约束 创建Single.cs 实体 IEnt ...

  2. Python面试题之Python反射详解

    0x00 前言 反射,可以理解为利用字符串的形式去对象中操作成员属性和方法 反射的这点特性让我联想到了exec函数,也是把利用字符串的形式去让Python解释器去执行命令 Python Version ...

  3. SqlServer2012数据导入

    1.选择数据库,右击[任务]-->[导入数据]: 2.选择对应的数据源,和数据文件,下一步: 3.填写服务器地址,和数据库的登录信息,选择数据库名称: 4.复制一个或多个表或试图的数据: 5.将 ...

  4. net.sf.json和 com.fasterxml.jackson中对象转json的区别

    近期做项目的时候,发现使用net.sf.json包中的JSONObject或JSONArray将对象转为json数据结构存在一个坑.当对String类型的属性赋值为null情况下,转为json结构为& ...

  5. UVA 11475 Extend to Palindrome(hash)题解

    题意:问你最少加几个字母使所给串变成回文串. 思路:一开始打算将正序和逆序都hash,然后用提取前缀后缀的方法来找,但是RE了,debug失败遂弃之.后来发现可以直接hash,一边hash一边比较.我 ...

  6. php中正则表达式的语法规则

  7. lapply

    正如前面展示的,lapply( )函数接收一个向量和一个函数作为输入参数.它将这个函数应用到向量中的每个元素,再将结果以列表的形式返回.当每次迭代都是相互独立时,这个函数就非常好用.因为在这种情况下, ...

  8. Learn Rails5.2- ActiveRecord: sqlite3的用法, Query查询语法。乐观锁和悲观锁案例,查询语法includes(), 多态关联,destory和delete, Scope, Validats, Migrations

    rails generate model photo title:string album:references 这会产生一个album_id列,当建立belongs_to关联时,需要用到. refe ...

  9. Cobbler自动化安装

    # Cobbler自动化安装 [Cobbler官网](http://cobbler.github.io) ![](/Users/wanyongzhen/Library/Containers/com.t ...

  10. easyui panel自适应浏览器宽度

    一.目标效果: 当浏览器窗口大小改变时.panel宽度始终为浏览器宽度的50%,panel高度则根据其中内容的多少而变化,横向竖向滚动条皆不出现.且不需要重新刷新浏览器或者其他js代码 兼容:chro ...