纯php实现中秋博饼游戏(2):掷骰子并输出结果
这篇是纯php实现中秋博饼游戏系列博文(2)
http://www.cnblogs.com/zqifa/p/php-dice-1.html
要纯php实现,就要用php来生成图案,第一步就先绘制骰子图案。下面就是编码实现业务逻辑,具体代码如下:
1 <?php 2 3 class roll 4 { 5 private $_defRank = 'lk'; 6 7 public function lottery() 8 { 9 $dice = $this->rollDice(); 10 $format = $this->formatDice($dice); 11 $rank = $this->getRank($format); 12 $rankName = $this->getName($rank); 13 return [ 14 'dice' => $dice, 15 //'format' => $format, 16 'rank' => $rank, 17 'rankName' => $rankName, 18 ]; 19 } 20 21 /** 22 * 获取筛子排名结果 23 * @param $dice 24 * @return array 25 */ 26 public function getRes($dice) 27 { 28 $format = $this->formatDice($dice); 29 $rank = $this->getRank($format); 30 $rankName = $this->getName($rank); 31 return [ 32 'dice' => $dice, 33 'format' => $format, 34 'rank' => $rank, 35 'rankName' => $rankName, 36 ]; 37 } 38 39 /** 40 * 掷骰子 41 * @return array 42 */ 43 public function rollDice() 44 { 45 $res = []; 46 for ($i = 0; $i < 6; $i++) { 47 $res[] = mt_rand(1, 6); 48 } 49 return $res; 50 } 51 52 /** 53 * 格式化掷骰子结果 54 * @param array $list 55 * @return array 56 */ 57 public function formatDice($list = []) 58 { 59 $data = []; 60 if (count($list) != 6) { 61 return $data; 62 } 63 $data = [ 64 1 => 0, 65 2 => 0, 66 3 => 0, 67 4 => 0, 68 5 => 0, 69 6 => 0, 70 ]; 71 foreach ($list as $val) { 72 if (isset($data[$val])) { 73 $data[$val] += 1; 74 } 75 } 76 foreach ($data as $key => $val) { 77 if ($val == 0) { 78 unset($data[$key]); 79 } 80 } 81 return $data; 82 } 83 84 /** 85 * 判断筛子结果的大小 86 * @param $list 87 * @return int|string 88 */ 89 public function getRank($list) 90 { 91 $ruleList = $this->_getRule(); 92 $res = $this->_defRank; 93 if (!empty($ruleList)) { 94 foreach ($ruleList as $rank => $rankRules) { 95 foreach ($rankRules as $rule) { 96 foreach ($rule as $dian => $num) { 97 if (isset($list[$dian])) { 98 if ($list[$dian] == $num) { 99 $res = $rank; 100 } else { 101 //规则中只要有一条不满足就跳出当前规则验证 102 $res = $this->_defRank; 103 break; 104 } 105 } else { 106 //规则中只要有一条不满足就跳出当前规则验证 107 $res = $this->_defRank; 108 break; 109 } 110 } 111 //有一条规则匹配,跳出循环, 112 if ($res != $this->_defRank) { 113 break; 114 } 115 } 116 //有一条规则匹配,跳出循环, 117 if ($res != $this->_defRank) { 118 break; 119 } 120 } 121 } 122 return $res; 123 } 124 125 /** 126 * 根据排序获取掷骰子结果名称 127 * @param int $rank 128 * @return array 129 */ 130 public function getName($rank = NULL) 131 { 132 $list = [ 133 'cjh' => '状元插金花', 134 'lbh' => '六杯红', 135 'bdj' => '遍地锦', 136 'ww' => '五王', 137 'wzdyx' => '五子带一秀', 138 'wzdk' => '五子登科', 139 'zy' => '状元', 140 'by' => '榜眼', 141 'sh' => '三红', 142 'sj' => '四进', 143 'eq' => '二举', 144 'yx' => '一秀', 145 'lk' => '轮空', 146 ]; 147 if (!empty($rank)) { 148 $rankName = ''; 149 if (isset($list[$rank])) { 150 $rankName = $list[$rank]; 151 } 152 return $rankName; 153 } 154 return $list; 155 } 156 157 /** 158 * 返回规则 159 * @return array 160 */ 161 private function _getRule() 162 { 163 return [ 164 'cjh' => [ 165 [2 => 2, 4 => 4] 166 ], 167 'lbh' => [ 168 [4 => 6] 169 ], 170 'bdj' => [ 171 [1 => 6], 172 [2 => 6], 173 [3 => 6], 174 [5 => 6], 175 [6 => 6], 176 ], 177 'ww' => [ 178 [4 => 5], 179 ], 180 'wzdyx' => [ 181 [1 => 5, 4 => 1], 182 [2 => 5, 4 => 1], 183 [3 => 5, 4 => 1], 184 [5 => 5, 4 => 1], 185 [6 => 5, 4 => 1], 186 ], 187 'wzdk' => [ 188 [1 => 5], 189 [2 => 5], 190 [3 => 5], 191 [5 => 5], 192 [6 => 5], 193 ], 194 'zy' => [ 195 [4 => 4] 196 ], 197 'by' => [ 198 [1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1] 199 ], 200 'sh' => [ 201 [4 => 3] 202 ], 203 'sj' => [ 204 [1 => 4], 205 [2 => 4], 206 [3 => 4], 207 [5 => 4], 208 [6 => 4], 209 ], 210 'eq' => [ 211 [4 => 2] 212 ], 213 'yx' => [ 214 [4 => 1] 215 ], 216 ]; 217 } 218 } 219 220 $roll = new roll(); 221 $res = $roll->lottery(); 222 223 echo '<h2>骰子点数:</h2>'; 224 echo '<p>'; 225 foreach($res['dice'] as $val){ 226 echo '<img src="img.php?num='.$val.'" >'; 227 } 228 echo '</p>'; 229 230 echo '<h2>结果:</h2>'; 231 echo '<h2 style="color:red;">'.$res['rankName'].'</h2>';
其中img.php是使用php生成图片的文件,参数num是点数,然后输出相应点数的图片,代码如下:
1 <?php 2 3 class imgDock 4 { 5 public function getImg($num = 0) 6 { 7 if(!empty($num)){ 8 header('Content-Type:image/png'); 9 $img = imagecreatetruecolor(200, 200); 10 $white = imagecolorallocate($img, 255, 255, 255); 11 $grey = imagecolorallocate($img, 100, 100, 100); 12 $blue = imagecolorallocate($img, 0, 102, 255); 13 $red = imagecolorallocate($img, 255, 0, 0); 14 imagefill($img, 0, 0, $white); 15 imageline($img, 10, 20, 10, 180, $grey); 16 imageline($img, 10, 180, 20, 190, $grey); 17 imageline($img, 20, 190, 180, 190, $grey); 18 imageline($img, 180, 190, 190, 180, $grey); 19 imageline($img, 190, 180, 190, 20, $grey); 20 imageline($img, 190, 20, 180, 10, $grey); 21 imageline($img, 180, 10, 20, 10, $grey); 22 imageline($img, 20, 10, 10, 20, $grey); 23 24 //1/2/3/4/5/6 25 switch($num){ 26 case 1: 27 imagefilledarc($img, 100, 100, 50, 50, 0, 0, $blue, IMG_ARC_PIE); 28 break; 29 case 2: 30 imagefilledarc($img, 60, 100, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 31 imagefilledarc($img, 140, 100, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 32 break; 33 case 3: 34 imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 35 imagefilledarc($img, 100, 100, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 36 imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 37 break; 38 case 4: 39 imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 40 imagefilledarc($img, 50, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 41 imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 42 imagefilledarc($img, 150, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 43 break; 44 case 5: 45 imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 46 imagefilledarc($img, 50, 150, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 47 imagefilledarc($img, 100, 100, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 48 imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 49 imagefilledarc($img, 150, 50, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 50 break; 51 case 6: 52 imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 53 imagefilledarc($img, 50, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 54 55 imagefilledarc($img, 100, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 56 imagefilledarc($img, 100, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 57 58 imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 59 imagefilledarc($img, 150, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 60 break; 61 default: 62 break; 63 } 64 imagepng($img); 65 imagedestroy($img); 66 } 67 } 68 } 69 70 $num = 0; 71 if(isset($_GET['num'])){ 72 $num = intval($_GET['num']); 73 } 74 $imgDock = new imgDock(); 75 $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!
如果觉得这文章还算用心,请劳驾点击右下角的推荐,这是对我们这些做开源分享的最大的肯定,谢谢。
作者:zqifa
出处:https://www.cnblogs.com/zqifa/
欢迎访问新博客地址:https://www.l1mn.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决