Advertisement
HristoBaychev

belot3

Apr 2nd, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. <?php
  2.  
  3. function checkForTercet($cards) {
  4. $suits = array();
  5. foreach ($cards as $card) {
  6. $parts = explode(" ", $card);
  7. $suit = $parts[1];
  8. if (!isset($suits[$suit])) {
  9. $suits[$suit] = array();
  10. }
  11. $suits[$suit][] = $card;
  12. }
  13.  
  14. foreach ($suits as $suitCards) {
  15. sort($suitCards);
  16. $count = count($suitCards);
  17. if ($count < 3){
  18. continue;
  19. }
  20. for ($i = 0; $i < $count - 2; $i++) {
  21. $parts = array();
  22. for ($j = 0; $j < 3; $j++) {
  23. $card = $suitCards[$i + $j];
  24. $parts[$j] = explode(" ", $card)[0];
  25. }
  26. for ($j = 0; $j < count($parts); $j++) {
  27. if ($parts[$j] === "J" || $parts[$j] === "Q" || $parts[$j] === "K" || $parts[$j] === "A" || $parts[$j] === "10") {
  28. switch ($parts[$j]) {
  29. case "J":
  30. $parts[$j] = 11;
  31. break;
  32. case "Q":
  33. $parts[$j] = 12;
  34. break;
  35. case "K":
  36. $parts[$j] = 13;
  37. break;
  38. case "A":
  39. $parts[$j] = 14;
  40. break;
  41. case "10":
  42. $parts[$j] = 10;
  43. break;
  44. }
  45. } else {
  46. $parts[$j] = intval($parts[$j]);
  47. }
  48. }
  49. sort($parts);
  50. if ($parts[0] >= 7 && $parts[0] <= 14 && $parts[1] == $parts[0]+1 && $parts[2] == $parts[0]+2) {
  51. return true;
  52. }
  53. }
  54. }
  55. return false;
  56. }
  57.  
  58.  
  59.  
  60.  
  61.  
  62. $number = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
  63. $shape = ["hearts", "diamonds", "clubs", "spades"];
  64.  
  65. $allCards = [];
  66. foreach ($shape as $shapes) {
  67. foreach ($number as $numbers) {
  68. $card = $numbers . ' ' . $shapes;
  69. array_push($allCards, $card);
  70. }
  71. }
  72.  
  73. echo "All cards before: ==> 52 " . PHP_EOL;
  74. print_r($allCards);
  75.  
  76. $cardForBelot = [];
  77. foreach ($allCards as $card) {
  78. $card_parts = explode(" ", $card);
  79. $number = $card_parts[0];
  80. if ($number == '2' || $number == '3' || $number == '4' || $number == '5' || $number == '6'){
  81. continue;
  82. }
  83. array_push($cardForBelot, $card);
  84. }
  85.  
  86. echo "All cards after ==> 32" . PHP_EOL;
  87. print_r($cardForBelot);
  88.  
  89. shuffle($cardForBelot);
  90.  
  91. echo "Shuffle cards: ==>";
  92. print_r($cardForBelot);
  93.  
  94. $players = [
  95. "Player One" => [],
  96. "Player Two" => [],
  97. "Player Three" => [],
  98. "Player Four" => []
  99. ];
  100.  
  101. for ($i = 0; $i < 3; $i++) {
  102. foreach ($players as $name => $cardForRemove) {
  103. array_push($players[$name], array_pop($cardForBelot));
  104. }
  105. }
  106.  
  107. for ($i = 0; $i < 2; $i++) {
  108. foreach ($players as $name => $cardForRemove) {
  109. array_push($players[$name], array_pop($cardForBelot));
  110. }
  111. }
  112.  
  113. for ($i = 0; $i < 3; $i++) {
  114. foreach ($players as $name => $cardForRemove) {
  115. array_push($players[$name], array_pop($cardForBelot));
  116. }
  117. }
  118.  
  119. echo "All cards after: ==> 0 " . PHP_EOL;
  120. print_r($cardForBelot);
  121.  
  122. echo "Player cards: ==> " . PHP_EOL;
  123. foreach ($players as $nameOfPlayer => $cards) {
  124. echo $nameOfPlayer . " ==>> ";
  125. print_r($cards);
  126. }
  127.  
  128. if (checkForTercet($cards)) {
  129. echo "tercet!\n";
  130. } else {
  131. echo "\n";
  132. }
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement