DeZhou.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. require_once dirname(__FILE__) . '/Poker.class.php';
  3. /**
  4. * Created by PhpStorm.
  5. * User: L
  6. * Date: 2016/12/14
  7. * Time: 09:49
  8. */
  9. class DeZhou extends Poker
  10. {
  11. /**
  12. * @var array
  13. */
  14. protected $type = array(
  15. 'thds' => 0,
  16. 'ths' => 1,
  17. 't4' => 2,
  18. 'hl' => 3,
  19. 'th' => 4,
  20. 'sz' => 5,
  21. 't3' => 6,
  22. 'd2' => 7,
  23. 'd1' => 8,
  24. 'gp' => 9,
  25. );
  26. public $gp = array();
  27. public function checkCards($cards)
  28. {
  29. if (sizeof($cards) == 5) {
  30. $th = true;
  31. $sz = true;
  32. $t4 = false;
  33. $t3 = false;
  34. $color_flag = false;
  35. $figure_flag = false;
  36. $figures = array();
  37. $colors = array();
  38. foreach ($cards as $key => $value) {
  39. $colors[$key] = $color = $this->colors[$value[0]];
  40. $figures[$key] = $figure = $this->figures[$value[1]];
  41. if (!$key) {
  42. $color_flag = $color;
  43. } else {
  44. if ($color_flag != $color) {
  45. $th = false;
  46. }
  47. if ($figures[$key - 1] - 1 != $figure) {
  48. $sz = false;
  49. }
  50. }
  51. }
  52. $d = 0;
  53. foreach (array_count_values($figures) as $key => $value) {
  54. switch ($value) {
  55. case 4:
  56. $t4 = true;
  57. break;
  58. case 3:
  59. $t3 = true;
  60. break;
  61. case 2:
  62. $d++;
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. if ($figures == array(12, 3, 2, 1, 0)) {
  69. $sz = true;
  70. }
  71. $d2 = $d == 2;
  72. $d1 = $d == 1;
  73. $ths = $th && $sz;
  74. $hl = $t3 && $d1;
  75. $thds = $ths && $figures == array(12, 11, 10, 9, 8);
  76. $type = 'gp';
  77. foreach ($this->type as $key => $value) {
  78. if (isset($$key) && $$key) {
  79. $type = $key;
  80. break;
  81. }
  82. }
  83. $type = $this->type[$type];
  84. return compact('type', 'figures', 'colors');
  85. } else {
  86. $cards = array_merge($cards, $this->gp);
  87. // 冒泡排序
  88. $max = sizeof($cards) - 1;
  89. for ($j = 0; $j < $max; $j++) {
  90. for ($i = 0; $i < $max - $j; $i++) {
  91. if ($this->figures[$cards[$i][1]] < $this->figures[$cards[$i + 1][1]]) {
  92. $card = $cards[$i];
  93. $cards[$i] = $cards[$i + 1];
  94. $cards[$i + 1] = $card;
  95. }
  96. }
  97. }
  98. $cards = self::combination($cards, 5);
  99. foreach ($cards as $key => $value) {
  100. $cards[$key] = array(
  101. 'cards' => $value,
  102. 'check' => $this->checkCards($value),
  103. );
  104. }
  105. $max = sizeof($cards) - 1;
  106. for ($j = 0; $j < $max; $j++) {
  107. for ($i = 0; $i < $max - $j; $i++) {
  108. if ($this->compare($cards[$i + 1], $cards[$i])) {
  109. $card = $cards[$i];
  110. $cards[$i] = $cards[$i + 1];
  111. $cards[$i + 1] = $card;
  112. }
  113. }
  114. }
  115. return $cards[0]['check'];
  116. }
  117. }
  118. public function compare($player1, $player2)
  119. {
  120. $type1 = $player1['check']['type'];
  121. $type2 = $player2['check']['type'];
  122. if ($type1 != $type2) {
  123. return $type1 < $type2;
  124. }
  125. $figures1 = $player1['check']['figures'];
  126. $figures2 = $player2['check']['figures'];
  127. if ($figures1 == $figures2) {
  128. return false;
  129. }
  130. $color1 = $player1['check']['colors'];
  131. $color2 = $player2['check']['colors'];
  132. switch ($type1) {
  133. case 7:
  134. // 两对
  135. $figures1 = array_count_values($figures1);
  136. $figures2 = array_count_values($figures2);
  137. arsort($figures1);
  138. arsort($figures2);
  139. $figures1 = array_keys($figures1);
  140. $figures2 = array_keys($figures2);
  141. if ($figures1[1] > $figures1[2]) {
  142. $value = $figures1[1];
  143. $figures1[1] = $figures1[2];
  144. $figures1[2] = $value;
  145. }
  146. if ($figures2[1] > $figures2[2]) {
  147. $value = $figures2[1];
  148. $figures2[1] = $figures2[2];
  149. $figures2[2] = $value;
  150. }
  151. break;
  152. case 2:
  153. // 四条
  154. case 3:
  155. // 葫芦
  156. case 6:
  157. // 三条
  158. case 8:
  159. // 一对
  160. $figures1 = array_count_values($figures1);
  161. $figures2 = array_count_values($figures2);
  162. arsort($figures1);
  163. arsort($figures2);
  164. $figures1 = array_keys($figures1);
  165. $figures2 = array_keys($figures2);
  166. break;
  167. case 5:
  168. // 同花顺
  169. case 1:
  170. //顺子
  171. if ($figures1 == array(12, 3, 2, 1, 0)) {
  172. return false;
  173. }
  174. if ($figures2 == array(12, 3, 2, 1, 0)) {
  175. return true;
  176. }
  177. return $figures1[0] > $figures2[0];
  178. break;
  179. case 0:
  180. // 皇家同花顺
  181. case 4:
  182. // 同花
  183. default:
  184. // 高牌
  185. break;
  186. }
  187. foreach ($figures1 as $key => $value) {
  188. if ($value != $figures2[$key]) {
  189. return $value > $figures2[$key];
  190. }
  191. }
  192. return false;
  193. }
  194. public function play($number = 2)
  195. {
  196. if ($number < 2 || $number > 23) {
  197. $number = 2;
  198. }
  199. if (empty($this->cards)) {
  200. $this->shuffleCards();
  201. }
  202. $this->flop();
  203. $players = array();
  204. for ($i = 0; $i < $number; $i++) {
  205. $cards = $this->pickCards(2);
  206. $players[] = array(
  207. 'cards' => $cards,
  208. 'check' => $this->checkCards($cards),
  209. );
  210. }
  211. return $this->order($players);
  212. }
  213. public function flop($cards = array())
  214. {
  215. if (empty($cards)) {
  216. $this->gp = array_merge($this->gp, $this->pickCards(5 - sizeof($this->gp)));
  217. } else {
  218. if (empty($this->cards)) {
  219. $this->shuffleCards();
  220. }
  221. foreach ($this->cards as $key => $value) {
  222. if (in_array($value, $cards)) {
  223. unset($this->cards[$key]);
  224. }
  225. }
  226. $this->gp = $cards;
  227. }
  228. }
  229. }