+ -
当前位置:首页 → 问答吧 → PHP算法之翻牌游戏

PHP算法之翻牌游戏

时间:2011-06-22

来源:互联网

/**
* 翻牌游戏
*
* 1-52张扑克牌,初始都翻开朝上
* 从2开始, 以倍数为基础, 将2翻一次, 4翻一次, 6翻一次...52翻一次
* 从3开始, 以倍数为基础, 将3翻一次, 6翻一次, 9翻一次...48翻一次
* 从4开始, 以倍数为基础, 将4翻一次, 8翻一次, 13翻一次...48翻一次
* .....
* 求最后朝上的牌有哪些
*/
  1. class up {
  2.        
  3.         protected $max = 52;
  4.         protected $min = 2;
  5.         protected $rs = array(1);        // 结果集,第一张牌是朝上的
  6.        
  7.         public function __construct() {
  8.         }
  9.        
  10.         // 循环得到2-52的整数数组
  11.         public function setp1() {
  12.                 for($i = $this->min; $i <= $this->max; $i++) {
  13.                         for($j = $this->min; $j <= $this->max; $j++) {
  14.                                 if(0 == $i % $j) {
  15.                                         $arr[$i][] = $j;
  16.                                 }
  17.                         }
  18.                 }
  19.                 return $arr;
  20.         }
  21.        
  22.         // 获得整除组合为偶数的牌
  23.         public function execute($arr) {
  24.                 foreach($arr as $k => $v) {
  25.                         if($this->setp3(count($v))) {
  26.                                 $this->rs[$k][] = $v;
  27.                         }
  28.                 }
  29.                 return $this->rs;
  30.         }
  31.        
  32.         // 判断奇偶数
  33.         public function setp3($num) {
  34.                 if(0 == $num % 2) {
  35.                         return true;
  36.                 } else {
  37.                         return false;
  38.                 }
  39.         }
  40.        
  41. }

  42. $arr = array();
  43. $up = new up();
  44. $arr = $up->setp1();
  45. print_r($up->execute($arr));
复制代码

作者: howk   发布时间: 2011-06-22

没玩过这种游戏。。。。。

作者: exploit   发布时间: 2011-06-22