当前位置:首页 > PHP教程 > php图像处理 > 列表

php实现的中秋博饼游戏之掷骰子并输出结果功能详解

发布:smiling 来源: PHP粉丝网  添加日期:2021-08-18 11:38:41 浏览: 评论:0 

这篇文章主要介绍了php实现的中秋博饼游戏之掷骰子并输出结果功能,结合实例形式分析了php掷骰子的原理及游戏结果的图形输出相关操作技巧,需要的朋友可以参考下

本文实例讲述了php实现的中秋博饼游戏之掷骰子并输出结果功能,分享给大家供大家参考,具体如下:

前面讲述了php实现的中秋博饼游戏之绘制骰子图案功能,纯php实现,就要用php来生成图案,第一步就先绘制骰子图案,下面就是编码实现业务逻辑,具体代码如下:

  1. <?php 
  2. class roll 
  3. { 
  4.   private $_defRank = 'lk'; 
  5.   public function lottery() 
  6.   { 
  7.     $dice   = $this->rollDice(); 
  8.     $format  = $this->formatDice($dice); 
  9.     $rank   = $this->getRank($format); 
  10.     $rankName = $this->getName($rank); 
  11.     return [ 
  12.       'dice'   => $dice, 
  13.       //'format'  => $format, 
  14.       'rank'   => $rank, 
  15.       'rankName' => $rankName, 
  16.     ]; 
  17.   } 
  18.   /** 
  19.    * 获取筛子排名结果 
  20.    * @param $dice 
  21.    * @return array 
  22.    */ 
  23.   public function getRes($dice) 
  24.   { 
  25.     $format  = $this->formatDice($dice); 
  26.     $rank   = $this->getRank($format); 
  27.     $rankName = $this->getName($rank); 
  28.     return [ 
  29.       'dice'   => $dice, 
  30.       'format'  => $format, 
  31.       'rank'   => $rank, 
  32.       'rankName' => $rankName, 
  33.     ]; 
  34.   } 
  35.   /** 
  36.    * 掷骰子 
  37.    * @return array 
  38.    */ 
  39.   public function rollDice() 
  40.   { 
  41.     $res = []; 
  42.     for ($i = 0; $i < 6; $i++) { 
  43.       $res[] = mt_rand(1, 6); 
  44.     } 
  45.     return $res; 
  46.   } 
  47.   /** 
  48.    * 格式化掷骰子结果 
  49.    * @param array $list 
  50.    * @return array 
  51.    */ 
  52.   public function formatDice($list = []) 
  53.   { 
  54.     $data = []; 
  55.     if (count($list) != 6) { 
  56.       return $data; 
  57.     } 
  58.     $data = [ 
  59.       1 => 0, 
  60.       2 => 0, 
  61.       3 => 0, 
  62.       4 => 0, 
  63.       5 => 0, 
  64.       6 => 0, 
  65.     ]; 
  66.     foreach ($list as $val) { 
  67.       if (isset($data[$val])) { 
  68.         $data[$val] += 1; 
  69.       } 
  70.     } 
  71.     foreach ($data as $key => $val) { 
  72.       if ($val == 0) { 
  73.         unset($data[$key]); 
  74.       } 
  75.     } 
  76.     return $data; 
  77.   } 
  78.   /** 
  79.    * 判断筛子结果的大小 
  80.    * @param $list 
  81.    * @return int|string 
  82.    */ 
  83.   public function getRank($list) 
  84.   { 
  85.     $ruleList = $this->_getRule(); 
  86.     $res   = $this->_defRank; 
  87.     if (!emptyempty($ruleList)) { 
  88.       foreach ($ruleList as $rank => $rankRules) { 
  89.         foreach ($rankRules as $rule) { 
  90.           foreach ($rule as $dian => $num) { 
  91.             if (isset($list[$dian])) { 
  92.               if ($list[$dian] == $num) { 
  93.                 $res = $rank; 
  94.               } else { 
  95.                 //规则中只要有一条不满足就跳出当前规则验证 
  96.                 $res = $this->_defRank; 
  97.                 break; 
  98.               } 
  99.             } else { 
  100.               //规则中只要有一条不满足就跳出当前规则验证 
  101.               $res = $this->_defRank; 
  102.               break; 
  103.             } 
  104.           } 
  105.           //有一条规则匹配,跳出循环, 
  106.           if ($res != $this->_defRank) { 
  107.             break; 
  108.           } 
  109.         } 
  110.         //有一条规则匹配,跳出循环, 
  111.         if ($res != $this->_defRank) { 
  112.           break; 
  113.         } 
  114.       } 
  115.     } 
  116.     return $res; 
  117.   } 
  118.   /** 
  119.    * 根据排序获取掷骰子结果名称 
  120.    * @param int $rank 
  121.    * @return array 
  122.    */ 
  123.   public function getName($rank = NULL) 
  124.   { 
  125.     $list = [ 
  126.       'cjh'  => '状元插金花', 
  127.       'lbh'  => '六杯红', 
  128.       'bdj'  => '遍地锦', 
  129.       'ww'  => '五王', 
  130.       'wzdyx' => '五子带一秀', 
  131.       'wzdk' => '五子登科', 
  132.       'zy'  => '状元', 
  133.       'by'  => '榜眼', 
  134.       'sh'  => '三红', 
  135.       'sj'  => '四进', 
  136.       'eq'  => '二举', 
  137.       'yx'  => '一秀', 
  138.       'lk'  => '轮空', 
  139.     ]; 
  140.     if (!emptyempty($rank)) { 
  141.       $rankName = ''; 
  142.       if (isset($list[$rank])) { 
  143.         $rankName = $list[$rank]; 
  144.       } 
  145.       return $rankName; 
  146.     } 
  147.     return $list; 
  148.   } 
  149.   /** 
  150.    * 返回规则 
  151.    * @return array 
  152.    */ 
  153.   private function _getRule() 
  154.   { 
  155.     return [ 
  156.       'cjh'  => [ 
  157.         [2 => 2, 4 => 4] 
  158.       ], 
  159.       'lbh'  => [ 
  160.         [4 => 6] 
  161.       ], 
  162.       'bdj'  => [ 
  163.         [1 => 6], 
  164.         [2 => 6], 
  165.         [3 => 6], 
  166.         [5 => 6], 
  167.         [6 => 6], 
  168.       ], 
  169.       'ww'  => [ 
  170.         [4 => 5], 
  171.       ], 
  172.       'wzdyx' => [ 
  173.         [1 => 5, 4 => 1], 
  174.         [2 => 5, 4 => 1], 
  175.         [3 => 5, 4 => 1], 
  176.         [5 => 5, 4 => 1], 
  177.         [6 => 5, 4 => 1], 
  178.       ], 
  179.       'wzdk' => [ 
  180.         [1 => 5], 
  181.         [2 => 5], 
  182.         [3 => 5], 
  183.         [5 => 5], 
  184.         [6 => 5], 
  185.       ], 
  186.       'zy'  => [ 
  187.         [4 => 4] 
  188.       ], 
  189.       'by'  => [ 
  190.         [1 => 1, 2 => 1, 3 => 1, 4 => 1, 5 => 1, 6 => 1] 
  191.       ], 
  192.       'sh'  => [ 
  193.         [4 => 3] 
  194.       ], 
  195.       'sj'  => [ 
  196.         [1 => 4], 
  197.         [2 => 4], 
  198.         [3 => 4], 
  199.         [5 => 4], 
  200.         [6 => 4], 
  201.       ], 
  202.       'eq'  => [ 
  203.         [4 => 2] 
  204.       ], 
  205.       'yx'  => [ 
  206.         [4 => 1] 
  207.       ], 
  208.     ]; 
  209.   } 
  210. } 
  211. $roll = new roll(); 
  212. $res = $roll->lottery(); 
  213. echo '<h2>骰子点数:</h2>'; 
  214. echo '<p>'; 
  215. foreach($res['dice'] as $val){ 
  216.   echo '<img src="img.php?num='.$val.'" >'; 
  217. } 
  218. echo '</p>'; 
  219. echo '<h2>结果:</h2>'; 
  220. echo '<h2 style="color:red;">'.$res['rankName'].'</h2>'; 

其中img.php是使用php生成图片的文件,参数num是点数,然后输出相应点数的图片,代码如下:

  1. <?php 
  2. class imgDock 
  3. { 
  4.   public function getImg($num = 0) 
  5.   { 
  6.     if(!emptyempty($num)){ 
  7.       header('Content-Type:image/png'); 
  8.       $img  = imagecreatetruecolor(200, 200); 
  9.       $white = imagecolorallocate($img, 255, 255, 255); 
  10.       $grey = imagecolorallocate($img, 100, 100, 100); 
  11.       $blue = imagecolorallocate($img, 0, 102, 255); 
  12.       $red  = imagecolorallocate($img, 255, 0, 0); 
  13.       imagefill($img, 0, 0, $white); 
  14.       imageline($img, 10, 20, 10, 180, $grey); 
  15.       imageline($img, 10, 180, 20, 190, $grey); 
  16.       imageline($img, 20, 190, 180, 190, $grey); 
  17.       imageline($img, 180, 190, 190, 180, $grey); 
  18.       imageline($img, 190, 180, 190, 20, $grey); 
  19.       imageline($img, 190, 20, 180, 10, $grey); 
  20.       imageline($img, 180, 10, 20, 10, $grey); 
  21.       imageline($img, 20, 10, 10, 20, $grey); 
  22.       //1/2/3/4/5/6 
  23.       switch($num){ 
  24.         case 1: 
  25.           imagefilledarc($img, 100, 100, 50, 50, 0, 0, $blue, IMG_ARC_PIE); 
  26.           break; 
  27.         case 2: 
  28.           imagefilledarc($img, 60, 100, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  29.           imagefilledarc($img, 140, 100, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  30.           break; 
  31.         case 3: 
  32.           imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 
  33.           imagefilledarc($img, 100, 100, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 
  34.           imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 
  35.           break; 
  36.         case 4: 
  37.           imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  38.           imagefilledarc($img, 50, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  39.           imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  40.           imagefilledarc($img, 150, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  41.           break; 
  42.         case 5: 
  43.           imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 
  44.           imagefilledarc($img, 50, 150, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 
  45.           imagefilledarc($img, 100, 100, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 
  46.           imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 
  47.           imagefilledarc($img, 150, 50, 40, 40, 0, 0 , $blue, IMG_ARC_PIE); 
  48.           break; 
  49.         case 6: 
  50.           imagefilledarc($img, 50, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  51.           imagefilledarc($img, 50, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  52.           imagefilledarc($img, 100, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  53.           imagefilledarc($img, 100, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  54.           imagefilledarc($img, 150, 150, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  55.           imagefilledarc($img, 150, 50, 40, 40, 0, 0 , $red, IMG_ARC_PIE); 
  56.           break; 
  57.         default: 
  58.           break; 
  59.       } 
  60.       imagepng($img); 
  61.       imagedestroy($img); 
  62.     } 
  63.   } 
  64. } 
  65. $num = 0; 
  66. if(isset($_GET['num'])){ 
  67.   $num = intval($_GET['num']); 
  68. } 
  69. $imgDock = new imgDock(); 
  70. $imgDock->getImg($num);

Tags: php中秋博饼 php骰子图案

分享到: