纯php实现中秋博饼游戏(2):掷骰子并输出结果
这篇是纯php实现中秋博饼游戏系列博文(2)
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):掷骰子并输出结果的更多相关文章
- 纯php实现中秋博饼游戏(1):绘制骰子图案
最近公司中秋博饼(在厦门),自己没事也想玩玩,所以就想动手写了一个纯php实现的中秋博饼游戏,既然要纯php实现,就要用php来生成图案,所以第一步就先绘制骰子图案. 平时很少使用php绘图,不过查查 ...
- tyvj1519博彩游戏
博彩游戏 From admin 背景 Background Bob最近迷上了一个博彩游戏…… 描述 Description 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到 ...
- tyvj P1519 博彩游戏(AC自动机+DP滚动数组)
P1519 博彩游戏 背景 Bob最近迷上了一个博彩游戏…… 描述 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到一个随机序列:有M个序列,如果某个序列是产生的随机序列的 ...
- 如何使用纯 CSS 制作四子连珠游戏
序言:你是否想过单纯使用 CSS 也可以制作一款游戏?甚至可以双人对决!这是一篇非常有趣的文章,作者详细讲解了使用纯 CSS 制作四子连珠游戏的思路以及使用奇淫巧技解决困难问题的方法.因为案例本身比较 ...
- 一款纯css3实现的数字统计游戏
今天给大家分享一款纯css3实现的数字统计游戏.这款游戏的规则的是将所有的数字相加等于72.这款游戏的数字按钮做得很美观,需要的时候可以借用下.一起看下效果图: 在线预览 源码下载 实现的代码. ...
- 博彩游戏(tyvj 1519)
背景 Bob最近迷上了一个博彩游戏…… 描述 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到一个随机序列:有M个序列,如果某个序列是产生的随机序列的子串,那么就中奖了,否 ...
- [JoyOI1519] 博彩游戏
题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目背景 Bob最近迷上了一个博彩游戏…… 题目描述 这个游戏的规则是这样的:每花一块钱可以 ...
- TurnipBit开发板掷骰子小游戏DIY教程实例
转载请以链接形式注明文章来源(MicroPythonQQ技术交流群:157816561,公众号:MicroPython玩家汇) 0x00前言 下面带大家用TurnipBit开发板实现一个简单的小游戏- ...
- 掷骰子游戏窗体实现--Java初级小项目
掷骰子 **多线程&&观察者模式 题目要求:<掷骰子>窗体小游戏,在该游戏中,玩家初始拥有1000的金钱,每次输入押大还是押小,以及下注金额,随机3个骰子的点数,如果3个骰 ...
随机推荐
- TED #03# 10 ways to have a better conversation
Teach you how to talk and how to listen Many of you have already heard a lot of advice on this, thin ...
- PHP curl模拟浏览器采集阿里巴巴的实现代码
<?php set_time_limit(0); function _rand() { $length=26; $chars = "0123456789abcdefghijklmnop ...
- Vim提示E325(锁机制)
背景 用vim命令处理一些超大文件时,有时会遇到卡死现象,不得不强制退出.但是,再次用vim命令访问这个文件时,会出现“E325:ATTENTION”提示.如果不做处理,以后每次都会出现. 分析 经过 ...
- Maven mybatis-generator自动生成代码
mybatis-generator可以自动生成代码,不管你是否喜欢它生成的代码的风格,它确实有助于我们更快速便捷的生成代码. Maven pom文件配置: <build> <plug ...
- CSS设置文本末行显示省略号...
首先设置文本标签或文字所在标签的宽度 最主要是以下三点: ①white-space:nowrap;如果是中文需要设置行末不断行 ②overflow:hidden;设置控 ...
- UVA-12166 Equilibrium Mobile(二叉树)
题目大意:改变二叉树上的权值,使树平衡,问最少该几个值. 题目分析:不会做,查的题解.有条奇妙的性质:如果将第d层权值为w的节点为基准做改动,则整棵树的总重量为w<<d,即w*2^d.仔细 ...
- 前端工程构建工具——Yeoman
一.Yeoman 简介 通常在开发新项目时我们都需要配置工程环境,开发目录,需要下载一些库.框架文件(如 jQuery.Backbone 等),配置编译环境(Less.Sass.Coffeescrip ...
- BZOJ1074 [SCOI2007]折纸origami
我们先看每个点可能从哪些点折过来的,2^10枚举对角线是否用到. 然后再模拟折法,查看每个点是否满足要求. 恩,计算几何比较恶心,还好前几天刚写过一道更恶心的计算几何,点类直接拷过来2333. /** ...
- mysql 索引原理及查询优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- VS2013命令行界面查看虚函数的内存布局
内存布局可能使用vs的界面调试看到的旺旺是一串数字,很不方便,但是vs的命令行界面可以很直观的显示出一个类中具体的内存布局. 打开命令行.界面如下所示: 测试代码如下所示: class Base1 { ...