Dealer.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package com.company;
  2. import java.util.Random;
  3. public class Dealer {
  4. private final Player[] players;
  5. private final Deck deck = new Deck();
  6. private final Card[] table = new Card[5];
  7. private int CardOnDesk = 0;
  8. private RATE_TYPE currentRateType;
  9. Random random = new Random(System.currentTimeMillis());
  10. public Dealer(int playerCount) throws Exception {
  11. if (playerCount < 2 || playerCount > 9) {
  12. throw new Exception("Некорректное количество игроков!");
  13. }
  14. players = new Player[playerCount];
  15. for (int i = 0; i < playerCount; i++) {
  16. players[i] = new Player("Player-" + (i + 1));
  17. }
  18. currentRateType = RATE_TYPE.PREFLOP;
  19. }
  20. /**
  21. * Раздача карт
  22. */
  23. public void HandOut() {
  24. switch (currentRateType) {
  25. case PREFLOP: {
  26. for (int i = 0; i < players.length; i++) {
  27. int cardIndex1 = random.nextInt(52);
  28. int cardIndex2 = random.nextInt(52);
  29. Card card1 = deck.Cards[cardIndex1];
  30. Card card2 = deck.Cards[cardIndex2];
  31. if (card1.onHands || card2.onHands) {
  32. i--;
  33. continue;
  34. }
  35. card1.onHands = true;
  36. card2.onHands = true;
  37. players[i].setCards(0, card1);
  38. players[i].setCards(1, card2);
  39. }
  40. currentRateType = RATE_TYPE.FLOP;
  41. }
  42. break;
  43. case FLOP: {
  44. PutOnDesk(3);
  45. currentRateType = RATE_TYPE.TURN;
  46. }
  47. break;
  48. case TURN: {
  49. PutOnDesk(1);
  50. currentRateType = RATE_TYPE.RIVER;
  51. }
  52. break;
  53. case RIVER: {
  54. PutOnDesk(1);
  55. currentRateType = RATE_TYPE.OPENING;
  56. }
  57. break;
  58. case OPENING: {
  59. OpeningCard();
  60. CardOnDesk = 0;
  61. currentRateType = RATE_TYPE.PREFLOP;
  62. }
  63. }
  64. }
  65. /**
  66. * Положить на стол cardCount карт
  67. *
  68. * @param cardCount количество карт для раздачи
  69. */
  70. public void PutOnDesk(int cardCount) {
  71. for (int i = 0; i < cardCount; i++) {
  72. int cardIndex = random.nextInt(52);
  73. Card currentCard = deck.Cards[cardIndex];
  74. if (currentCard.onHands) {
  75. i--;
  76. continue;
  77. }
  78. table[CardOnDesk++] = currentCard;
  79. currentCard.onHands = true;
  80. }
  81. }
  82. // ♦♣♥♠
  83. public void OpeningCard() {
  84. int slot = 2;
  85. System.out.println("Карты на столе ");
  86. for (Card card : table) {
  87. System.out.print(
  88. deck.CardName.get(card.getDenomination())
  89. + Lear.SYMBOL[card.Lear.ordinal()] + " ");
  90. for (Player p : players) {
  91. p.setCards(slot, card);
  92. }
  93. slot++;
  94. }
  95. System.out.println("\n\nКарты игроков");
  96. for (Player player : players) {
  97. try {
  98. Sorting(player.cards);
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. player.Calc();
  103. PrintCard(player.cards);
  104. if (!isRoyalFlush(player.cards, player)) {
  105. if (!isStraightFlush(player.cards, player)) {
  106. if (!isFourOfAKind(player.cards, player)) {
  107. if (!isFullHouse(player.cards, player)) {
  108. if (!isFlush(player.cards, player)) {
  109. if (!isStraight(player.cards, player)) {
  110. if (!isThreeOfAKind(player.cards, player)) {
  111. if (!isTwoPairs(player.cards, player)) {
  112. if (!isOnePair(player.cards, player)) {
  113. player.combination.Value = player.cards[0].getDenomination();
  114. player.combination.CurrentCombination = Combination.LIST.HIGH_CARD;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. System.out.println(player.getName() + " " + player.combination.Value);
  125. }
  126. Sorting(players);
  127. Player pWin;
  128. int count = 0;
  129. if (players[0].combination.Value == players[1].combination.Value) {
  130. int value = players[0].combination.Value;
  131. count++;
  132. for (int q = 2; q < players.length - 1; q++) {
  133. if (players[q].combination.Value == players[q + 1].combination.Value) {
  134. if (value == players[q].combination.Value) {
  135. count++;
  136. } else {
  137. break;
  138. }
  139. }
  140. }
  141. }
  142. if (count > 0) {
  143. Player[] winPlayers = new Player[count + 1];
  144. for (int wp = 0; wp <= count; wp++) {
  145. winPlayers[wp] = players[wp];
  146. }
  147. SortingBySum(winPlayers);
  148. pWin = winPlayers[0];
  149. } else {
  150. pWin = players[0];
  151. }
  152. System.out.println("Игрок " + pWin.getName() + " выйграл");
  153. }
  154. public void PrintCard(Card[] cards) {
  155. for (Card card : cards) {
  156. System.out.print(
  157. deck.CardName.get(card.getDenomination())
  158. + Lear.SYMBOL[card.Lear.ordinal()] + " ");
  159. }
  160. System.out.println();
  161. }
  162. /**
  163. * Сортировка массива карт по убыванию его номинала
  164. *
  165. * @param cards массив карт (7 штук)
  166. */
  167. public void Sorting(Card[] cards) throws Exception {
  168. if (cards.length != 7) {
  169. throw new Exception("Что-то пошло не так");
  170. }
  171. for (int i = 0; i < cards.length; i++) {
  172. for (int j = 0; j < cards.length - 1; j++) {
  173. if (cards[j].getDenomination() < cards[j + 1].getDenomination()) {
  174. Card temp = cards[j];
  175. cards[j] = cards[j + 1];
  176. cards[j + 1] = temp;
  177. }
  178. }
  179. }
  180. }
  181. public void Sorting(Player[] players) {
  182. for (int i = 0; i < players.length; i++) {
  183. for (int j = 0; j < players.length - 1; j++) {
  184. if (players[j].combination.Value < players[j + 1].combination.Value) {
  185. Player temp = players[j];
  186. players[j] = players[j + 1];
  187. players[j + 1] = temp;
  188. }
  189. }
  190. }
  191. }
  192. public void SortingBySum(Player[] players) {
  193. for (int i = 0; i < players.length; i++) {
  194. for (int j = 0; j < players.length - 1; j++) {
  195. if (players[j].SumDenomination < players[j + 1].SumDenomination) {
  196. Player temp = players[j];
  197. players[j] = players[j + 1];
  198. players[j + 1] = temp;
  199. }
  200. }
  201. }
  202. }
  203. /**
  204. * Проверяет есть ли в переданном массиве комбинация Стрит
  205. *
  206. * @param cards массив карт
  207. */
  208. public boolean isRoyalFlush(Card[] cards, Player player) {
  209. boolean cardK = false;
  210. boolean cardQ = false;
  211. boolean cardJ = false;
  212. boolean card10 = false;
  213. if (cards[0].getDenomination() == 14 && isStraightFlush(cards, player)) {
  214. for (int i = 0; i < cards.length - 1; i++) {
  215. if (cards[i].getDenomination() == 13) {
  216. cardK = true;
  217. }
  218. if (cards[i].getDenomination() == 12) {
  219. cardQ = true;
  220. }
  221. if (cards[i].getDenomination() == 11) {
  222. cardJ = true;
  223. }
  224. if (cards[i].getDenomination() == 10) {
  225. card10 = true;
  226. }
  227. }
  228. if (card10 && cardJ && cardK && cardQ) {
  229. player.combination.CurrentCombination = Combination.LIST.ROYAL_FLUSH;
  230. player.combination.Value = 60 * Combination.Coefficient[Combination.LIST.ROYAL_FLUSH.ordinal()];
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. /**
  237. * Стрит-флеш
  238. *
  239. * @param cards массив карт игрока и на столе отсортированных по номиналу
  240. * @param player экземпляр игрока
  241. */
  242. public boolean isStraightFlush(Card[] cards, Player player) {
  243. int counter = 0;
  244. player.combination.Value = 0;
  245. for (int i = 0; i < cards.length - 1; i++) {
  246. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  247. continue;
  248. }
  249. if (cards[i].getDenomination() != cards[i + 1].getDenomination() + 1) {
  250. if (i < 2) {
  251. counter = 0;
  252. player.combination.Value = 0;
  253. continue;
  254. }
  255. break;
  256. }
  257. if (cards[i].Lear == cards[i + 1].Lear && cards[i].getDenomination() == cards[i + 1].getDenomination() + 1) {
  258. counter++;
  259. player.combination.Value += cards[i].getDenomination();
  260. if (counter == 4) {
  261. player.combination.CurrentCombination = Combination.LIST.STRAIGHT_FLUSH;
  262. player.combination.Value += cards[i + 1].getDenomination();
  263. player.combination.Value *= Combination.Coefficient[Combination.LIST.STRAIGHT_FLUSH.ordinal()];
  264. return true;
  265. }
  266. }
  267. }
  268. return false;
  269. }
  270. /**
  271. * Каре (четыре одного вида)
  272. *
  273. * @param cards массив карт игрока и на столе отсортированных по номиналу
  274. * @param player экземпляр игрока
  275. */
  276. public boolean isFourOfAKind(Card[] cards, Player player) {
  277. int count = 0;
  278. player.combination.Value = 0;
  279. for (int i = 0; i < cards.length - 1; i++) {
  280. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  281. count++;
  282. player.combination.Value += cards[i].getDenomination();
  283. if (count == 3) {
  284. player.combination.Value += cards[i + 1].getDenomination();
  285. player.combination.Value *= Combination.Coefficient[Combination.LIST.FOUR_OF_A_KIND.ordinal()];
  286. return true;
  287. }
  288. } else {
  289. count = 0;
  290. player.combination.Value = 0;
  291. }
  292. }
  293. return false;
  294. }
  295. public boolean isFullHouse(Card[] cards, Player player) {
  296. int count1 = 0;
  297. int count2 = 0;
  298. int value1 = 0;
  299. int value2 = 0;
  300. player.combination.Value = 0;
  301. for (int i = 0; i < cards.length - 1; i++) {
  302. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  303. count1++;
  304. value1 = cards[i].getDenomination();
  305. }
  306. if (cards[i].getDenomination() != cards[i + 1].getDenomination()) {
  307. if (count1 == 2 || count1 == 1) {
  308. break;
  309. } else {
  310. count1 = 0;
  311. value1 = 0;
  312. }
  313. }
  314. }
  315. for (int j = 0; j < cards.length - 1; j++) {
  316. if (cards[j].getDenomination() == value1) {
  317. continue;
  318. }
  319. if (cards[j].getDenomination() == cards[j + 1].getDenomination()) {
  320. count2++;
  321. value2 = cards[j].getDenomination();
  322. }
  323. if (count2 == 2) {
  324. break;
  325. }
  326. if (cards[j].getDenomination() != cards[j + 1].getDenomination()) {
  327. if (count2 == 1) {
  328. break;
  329. } else count2 = 0;
  330. }
  331. }
  332. if (count1 == count2 && count1 > 1) {
  333. if (value1 > value2) {
  334. player.combination.Value = value1 * 3 + value2 * 2;
  335. } else {
  336. player.combination.Value = value1 * 2 + value2 * 3;
  337. }
  338. player.combination.CurrentCombination = Combination.LIST.FULL_HOUSE;
  339. player.combination.Value *= Combination.Coefficient[Combination.LIST.FULL_HOUSE.ordinal()];
  340. return true;
  341. } else {
  342. if (count1 == 1 && count2 == 2 || count1 == 2 && count2 == 1) {
  343. player.combination.Value = value1 * count1 + value2 * count2 * Combination.Coefficient[Combination.LIST.FULL_HOUSE.ordinal()];
  344. player.combination.CurrentCombination = Combination.LIST.FULL_HOUSE;
  345. return true;
  346. }
  347. }
  348. return false;
  349. }
  350. /**
  351. * !!! ДОРАБОТАТЬ !!!
  352. */
  353. public boolean isFlush(Card[] cards, Player player) {
  354. int CountHearts = 0;
  355. int CountDiamonds = 0;
  356. int CountClubs = 0;
  357. int CountSpades = 0;
  358. for (int i = 0; i < cards.length - 1; i++) {
  359. if (cards[i].Lear == Lear.NAME.CLUBS) {
  360. CountClubs++;
  361. }
  362. if (cards[i].Lear == Lear.NAME.DIAMONDS) {
  363. CountDiamonds++;
  364. }
  365. if (cards[i].Lear == Lear.NAME.SPADES) {
  366. CountSpades++;
  367. }
  368. if (cards[i].Lear == Lear.NAME.HEARTS) {
  369. CountHearts++;
  370. }
  371. }
  372. if (CountClubs >= 5 || CountDiamonds >= 5 || CountSpades >= 5 || CountHearts >= 5) {
  373. return true;
  374. } else return false;
  375. }
  376. public boolean isStraight(Card[] cards, Player player) {
  377. int counter = 0;
  378. player.combination.Value = 0;
  379. if (cards[0].getDenomination() == 14 && cards[6].getDenomination() == 2) {
  380. boolean CardIsFive = false;
  381. boolean CardIsFour = false;
  382. boolean CardIsThree = false;
  383. for (int i = 0; i < cards.length - 1; i++) {
  384. if (cards[i].getDenomination() == 3) {
  385. CardIsThree = true;
  386. }
  387. if (cards[i].getDenomination() == 4) {
  388. CardIsFour = true;
  389. }
  390. if (cards[i].getDenomination() == 5) {
  391. CardIsFive = true;
  392. }
  393. }
  394. if (CardIsFive && CardIsFour && CardIsThree) {
  395. player.combination.CurrentCombination = Combination.LIST.STRAIGHT;
  396. player.combination.Value = 60 * Combination.LIST.STRAIGHT.ordinal();
  397. return true;
  398. }
  399. }
  400. for (int i = 0; i < cards.length - 1; i++) {
  401. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  402. continue;
  403. }
  404. if (cards[i].getDenomination() != cards[i + 1].getDenomination() + 1) {
  405. if (i < 2) {
  406. counter = 0;
  407. player.combination.Value = 0;
  408. continue;
  409. }
  410. break;
  411. }
  412. counter++;
  413. player.combination.Value += cards[i].getDenomination();
  414. if (counter == 4) {
  415. player.combination.Value += cards[i + 1].getDenomination();
  416. player.combination.Value *= Combination.Coefficient[Combination.LIST.STRAIGHT.ordinal()];
  417. player.combination.CurrentCombination = Combination.LIST.STRAIGHT;
  418. return true;
  419. }
  420. }
  421. return false;
  422. }
  423. public boolean isThreeOfAKind(Card[] cards, Player player) {
  424. int count = 0;
  425. player.combination.Value = 0;
  426. for (int i = 0; i < cards.length - 1; i++) {
  427. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  428. count++;
  429. player.combination.Value += cards[i].getDenomination();
  430. } else {
  431. count = 0;
  432. player.combination.Value = 0;
  433. }
  434. if (count == 2) {
  435. player.combination.Value += cards[i + 1].getDenomination();
  436. player.combination.Value *= Combination.Coefficient[Combination.LIST.THREE_OF_A_KIND.ordinal()];
  437. player.combination.CurrentCombination = Combination.LIST.THREE_OF_A_KIND;
  438. return true;
  439. }
  440. }
  441. return false;
  442. }
  443. public boolean isTwoPairs(Card[] cards, Player player) {
  444. int count1 = 0, value1 = 0;
  445. int count2 = 0, value2 = 0;
  446. player.combination.Value = 0;
  447. for (int i = 0; i < cards.length - 1; i++) {
  448. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  449. count1++;
  450. if (count1 == 1) {
  451. value1 = cards[i].getDenomination();
  452. break;
  453. }
  454. }
  455. }
  456. for (int j = 0; j < cards.length - 1; j++) {
  457. if (cards[j].getDenomination() == value1) {
  458. continue;
  459. }
  460. if (cards[j].getDenomination() == cards[j + 1].getDenomination()) {
  461. count2++;
  462. if (count2 == 1) {
  463. value2 = cards[j].getDenomination();
  464. break;
  465. }
  466. }
  467. }
  468. if (count1 == 1 && count2 == 1) {
  469. player.combination.Value = (value1 * 2 + value2 * 2) * Combination.Coefficient[Combination.LIST.TWO_PAIRS.ordinal()];
  470. player.combination.CurrentCombination = Combination.LIST.TWO_PAIRS;
  471. return true;
  472. }
  473. return false;
  474. }
  475. public boolean isOnePair(Card[] cards, Player player) {
  476. player.combination.Value = 0;
  477. for (int i = 0; i < cards.length - 1; i++) {
  478. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  479. player.combination.Value = cards[i].getDenomination() * 2 * Combination.Coefficient[Combination.LIST.ONE_PAIR.ordinal()];
  480. player.combination.CurrentCombination = Combination.LIST.ONE_PAIR;
  481. return true;
  482. }
  483. }
  484. return false;
  485. }
  486. }