纯php实现中秋博饼游戏(2):掷骰子并输出结果
发布时间:2020-12-13 21:21:49 所属栏目:PHP教程 来源:网络整理
导读:这篇是纯php实现中秋博饼游戏系列博文(2) 上文是: 纯php实现中秋博饼游戏(1):绘制骰子图案 http://www.cnblogs.com/zqifa/p/php-dice-1.html 要纯php实现,就要用php来生成图案,第一步就先绘制骰子图案。下面就是编码实现业务逻辑,具体代码如下: ?
这篇是纯php实现中秋博饼游戏系列博文(2) 上文是: http://www.cnblogs.com/zqifa/p/php-dice-1.html ? 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,1)"> 17 'rankName' => $rankName,1)"> 18 ]; 19 } 20 21 /** 22 * 获取筛子排名结果 23 * @param $dice 24 * @return array 25 */ 26 function getRes() 27 28 29 30 31 32 'dice' => 33 'format' => $format,1)"> 34 'rank' => 35 'rankName' => 36 37 38 39 40 * 掷骰子 41 42 43 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 56 57 function formatDice($list = []) 58 59 $data = 60 if (count($list) != 6 61 $data 62 63 64 1 => 0,1)"> 65 2 => 0,1)"> 66 3 => 0,1)"> 67 4 => 0,1)"> 68 5 => 0,1)"> 69 6 => 0,1)"> 70 71 foreach ($list as $val 72 isset($data[])) { 73 $val] += 1 74 } 75 76 $data $key => 77 if ($val == 0 78 unset($key]); 79 80 81 82 83 84 85 * 判断筛子结果的大小 86 * @param $list 87 * @return int|string 88 89 function getRank($list 90 91 $ruleList = _getRule(); 92 $res = _defRank; 93 if (!empty($ruleList)) { 94 $ruleList $rank => $rankRules 95 $rankRules $rule 96 $rule $dian => $num 97 $list[$dian 98 $dian] == 99 $res = 100 } else { 101 规则中只要有一条不满足就跳出当前规则验证 102 103 break104 } 105 } 106 107 108 109 } 110 } 111 有一条规则匹配,跳出循环, 112 $res != _defRank) { 113 114 115 } 116 117 118 119 120 121 122 123 124 125 126 * 根据排序获取掷骰子结果名称 127 * @param int $rank 128 129 130 function getName($rank = NULL131 132 133 'cjh' => '状元插金花',1)">134 'lbh' => '六杯红',1)">135 'bdj' => '遍地锦',1)">136 'ww' => '五王',1)">137 'wzdyx' => '五子带一秀',1)">138 'wzdk' => '五子登科',1)">139 'zy' => '状元',1)">140 'by' => '榜眼',1)">141 'sh' => '三红',1)">142 'sj' => '四进',1)">143 'eq' => '二举',1)">144 'yx' => '一秀',1)">145 'lk' => '轮空',1)">146 147 148 $rankName = ''149 150 ]; 151 152 $rankName153 154 155 156 157 158 * 返回规则 159 160 161 private _getRule() 162 163 164 'cjh' =>165 [2 => 2,4 => 4] 166 ],1)">167 'lbh' =>168 [4 => 6169 ],1)">170 'bdj' =>171 [1 => 6],1)">172 [2 => 6],1)">173 [3 => 6],1)">174 [5 => 6],1)">175 [6 => 6],1)">176 ],1)">177 'ww' =>178 [4 => 5],1)">179 ],1)">180 'wzdyx' =>181 [1 => 5,4 => 1],1)">182 [2 => 5,1)">183 [3 => 5,1)">184 [5 => 5,1)">185 [6 => 5,1)">186 ],1)">187 'wzdk' =>188 [1 => 5],1)">189 [2 => 5],1)">190 [3 => 5],1)">191 [5 => 5],1)">192 [6 => 5],1)">193 ],1)">194 'zy' =>195 [4 => 4196 ],1)">197 'by' =>198 [1 => 1,2 => 1,3 => 1,4 => 1,5 => 1,6 => 1199 ],1)">200 'sh' =>201 [4 => 3202 ],1)">203 'sj' =>204 [1 => 4],1)">205 [2 => 4],1)">206 [3 => 4],1)">207 [5 => 4],1)">208 [6 => 4],1)">209 ],1)">210 'eq' =>211 [4 => 2212 ],1)">213 'yx' =>214 [4 => 1215 ],1)">216 217 218 } 219 220 $roll = new roll(); 221 $roll->lottery(); 222 223 echo '<h2>骰子点数:</h2>'224 echo '<p>'225 foreach($res['dice'] ){ 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 <? 2 3 imgDock 4 5 function getImg($num = 0 6 7 if(!)){ 8 header('Content-Type:image/png' 9 $img = imagecreatetruecolor(200,20010 $white = imagecolorallocate($img,25511 $grey = imagecolorallocate(12 $blue = imagecolorallocate(13 $red = imagecolorallocate(14 imagefill($white15 imageline($grey16 imageline(17 imageline(18 imageline(19 imageline(20 imageline(21 imageline(22 imageline(23 24 1/2/3/4/5/6 25 switch(26 case 1: 27 imagefilledarc($blue,1)"> IMG_ARC_PIE); 28 29 case 2: 30 imagefilledarc($red,1)">31 imagefilledarc(32 33 case 3: 34 imagefilledarc(35 imagefilledarc(36 imagefilledarc(37 38 case 4: 39 imagefilledarc(40 imagefilledarc(41 imagefilledarc(42 imagefilledarc(43 44 case 5: 45 imagefilledarc(46 imagefilledarc(47 imagefilledarc(48 imagefilledarc(49 imagefilledarc(50 51 case 6: 52 imagefilledarc(53 imagefilledarc(54 55 imagefilledarc(56 imagefilledarc(57 58 imagefilledarc(59 imagefilledarc(60 61 default: 62 63 64 imagepng($img65 imagedestroy(66 67 68 69 70 71 if($_GET['num'])){ 72 $num = intval(73 74 $imgDock = imgDock(); 75 $imgDock->getImg($num); ? 下面是我抽中状元的效果图,O(∩_∩)O哈哈~ ? 纯php实现中秋博饼游戏系列博文: 纯php实现中秋博饼游戏(1):绘制骰子图案 done! ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |