纯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个骰 ...
随机推荐
- 用python解析word文件(段落篇(paragraph) 表格篇(table) 样式篇(style))
首先需要安装相应的支持库: 直接在命令行执行pip install python-docx 示例代码如下: import docxfrom docx import Document #导入库 path ...
- [POI2006][luogu3435] OKR-Periods of Words [kmp+next数组]
题面 传送门 思路 先把题面转成人话: 对于给定串的每个前缀i,求最长的,使这个字符串重复两边能覆盖原前缀i的前缀(就是前缀i的一个前缀),求所有的这些"前缀的前缀"的长度和 利用 ...
- Git WorkBehavior
https://tortoisegit.org/docs/tortoisegit/tgit-dug-showlog.html Repository Demo https://github.com/Ch ...
- JS判定注册表单的几个方式 及 Ajax进行用户名存在判定
最近感觉不赶紧把代码逻辑记一下梳理一下,再做的时候就容易进入"逻辑误区". 有个表单,简单点. <!DOCTYPE html> <!-- 注册表单验证,用户名格式 ...
- 解决 docker: Error response from daemon: ... : net/http: TLS handshake timeout.
参考:解决 Docker pull 出现的net/http: TLS handshake timeout 的一个办法 问题: 执行 $ sudo docker run hello-world 时出现: ...
- shell 字符串加入变量
your_name='runoob' str="Hello, I know you are \"$your_name\"! \n" echo $str
- ipconfig会出现多个IP地址
一.问题描述 今天调试程序的时候发现电脑有两个IP地址,一时间不知道该用哪个?如下图: 二.问题分析 第一个叫ppp适配器,是一个逻辑的虚拟设备,ppp的意思是Point-to-Point Proto ...
- thinkphp3.2笔记(4)模板函数的使用 foreach标签
一 模板函数的使用 1.代码 效果: 函数会按照从左到右的顺序依次调用.如果你觉得这样写起来比较麻烦,也可以直接这样写:{:substr(strtoupper(md5($name)),0,3)} 默 ...
- ✅问题:Rails.ajax自定义请求
chatroom.coffee中的js代码: document.addEventListener 'turbolinks:load', -> document.getElementById(&q ...
- tomcat 容器下web项目由http改为https操作步骤及相关的坑
一.https介绍: HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP ...