Dealer.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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(Player.CARD_SLOT.FIRST, card1);
  38. players[i].setCards(Player.CARD_SLOT.SECOND, 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. System.out.println("Карты на столе ");
  85. for (Card card : table) {
  86. System.out.print(
  87. deck.CardName.get(card.getDenomination())
  88. + Lear.SYMBOL[card.Lear.ordinal()] + " ");
  89. }
  90. System.out.println("\n\nКарты игроков");
  91. for (Player player : players) {
  92. Card[] sharedCards = new Card[7];
  93. int i = 0;
  94. for (; i < table.length; i++) {
  95. sharedCards[i] = table[i];
  96. }
  97. sharedCards[i++] = player.getCards(Player.CARD_SLOT.FIRST);
  98. sharedCards[i] = player.getCards(Player.CARD_SLOT.SECOND);
  99. try {
  100. Sorting(sharedCards);
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. }
  104. PrintCard(sharedCards);
  105. if (!isRoyalFlush(sharedCards, player)) {
  106. if (!isStraightFlush(sharedCards, player)) {
  107. if (!isFourOfAKind(sharedCards, player)) {
  108. if (!isFullHouse(sharedCards, player)) {
  109. if (!isFlush(sharedCards, player)) {
  110. if (!isStraight(sharedCards, player)) {
  111. if (!isThreeOfAKind(sharedCards, player)) {
  112. if (!isTwoPairs(sharedCards, player)) {
  113. if (!isOnePair(sharedCards, player)) {
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. System.out.println(player.getName() + " " + player.combination.Value);
  124. }
  125. for (int k = 0; k < players.length; k++) {
  126. for (int l = 1; l < players.length; l++) {
  127. if (players[k].combination.Value == players[l].combination.Value) {
  128. for (Card card : sharedCards) {
  129. }
  130. }
  131. }
  132. }
  133. }
  134. public void PrintCard(Card[] cards) {
  135. for (Card card : cards) {
  136. System.out.print(
  137. deck.CardName.get(card.getDenomination())
  138. + Lear.SYMBOL[card.Lear.ordinal()] + " ");
  139. }
  140. System.out.println();
  141. }
  142. /**
  143. * Сортировка массива карт по убыванию его номинала
  144. *
  145. * @param cards массив карт (7 штук)
  146. */
  147. public void Sorting(Card[] cards) throws Exception {
  148. if (cards.length != 7) {
  149. throw new Exception("Что-то пошло не так");
  150. }
  151. for (int i = 0; i < cards.length; i++) {
  152. for (int j = 0; j < cards.length - 1; j++) {
  153. if (cards[j].getDenomination() < cards[j + 1].getDenomination()) {
  154. Card temp = cards[j];
  155. cards[j] = cards[j + 1];
  156. cards[j + 1] = temp;
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * Проверяет есть ли в переданном массиве комбинация Стрит
  163. *
  164. * @param cards массив карт
  165. */
  166. public boolean isRoyalFlush(Card[] cards, Player player) {
  167. boolean cardK = false;
  168. boolean cardQ = false;
  169. boolean cardJ = false;
  170. boolean card10 = false;
  171. if (cards[0].getDenomination() == 14 && isStraightFlush(cards, player)) {
  172. for (int i = 0; i < cards.length - 1; i++) {
  173. if (cards[i].getDenomination() == 13) {
  174. cardK = true;
  175. }
  176. if (cards[i].getDenomination() == 12) {
  177. cardQ = true;
  178. }
  179. if (cards[i].getDenomination() == 11) {
  180. cardJ = true;
  181. }
  182. if (cards[i].getDenomination() == 10) {
  183. card10 = true;
  184. }
  185. }
  186. if (card10 && cardJ && cardK && cardQ) {
  187. player.combination.CurrentCombination = Combination.LIST.ROYAL_FLUSH;
  188. player.combination.Value = 60 * Combination.Coefficient[Combination.LIST.ROYAL_FLUSH.ordinal()];
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * Стрит-флеш
  196. * @param cards массив карт игрока и на столе отсортированных по номиналу
  197. * @param player экземпляр игрока
  198. */
  199. public boolean isStraightFlush(Card[] cards, Player player) {
  200. int counter = 0;
  201. player.combination.Value = 0;
  202. for (int i = 0; i < cards.length - 1; i++) {
  203. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  204. continue;
  205. }
  206. if (cards[i].getDenomination() != cards[i + 1].getDenomination() + 1) {
  207. if (i < 2) {
  208. counter = 0;
  209. player.combination.Value = 0;
  210. continue;
  211. }
  212. break;
  213. }
  214. if (cards[i].Lear == cards[i + 1].Lear && cards[i].getDenomination() == cards[i + 1].getDenomination() + 1) {
  215. counter++;
  216. player.combination.Value += cards[i].getDenomination();
  217. if (counter == 4) {
  218. player.combination.CurrentCombination = Combination.LIST.STRAIGHT_FLUSH;
  219. player.combination.Value += cards[i + 1].getDenomination();
  220. player.combination.Value *= Combination.Coefficient[Combination.LIST.STRAIGHT_FLUSH.ordinal()];
  221. return true;
  222. }
  223. }
  224. }
  225. return false;
  226. }
  227. /**
  228. * Каре (четыре одного вида)
  229. * @param cards массив карт игрока и на столе отсортированных по номиналу
  230. * @param player экземпляр игрока
  231. */
  232. public boolean isFourOfAKind(Card[] cards, Player player) {
  233. int count = 0;
  234. player.combination.Value = 0;
  235. for (int i = 0; i < cards.length - 1; i++) {
  236. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  237. count++;
  238. player.combination.Value += cards[i].getDenomination();
  239. if (count == 3) {
  240. player.combination.Value += cards[i + 1].getDenomination();
  241. player.combination.Value *= Combination.Coefficient[Combination.LIST.FOUR_OF_A_KIND.ordinal()];
  242. return true;
  243. }
  244. } else {
  245. count = 0;
  246. player.combination.Value = 0;
  247. }
  248. }
  249. return false;
  250. }
  251. public boolean isFullHouse(Card[] cards, Player player) {
  252. int count1 = 0;
  253. int count2 = 0;
  254. int value1 = 0;
  255. int value2 = 0;
  256. player.combination.Value = 0;
  257. for (int i = 0; i < cards.length - 1; i++) {
  258. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  259. count1++;
  260. value1 = cards[i].getDenomination();
  261. }
  262. if (cards[i].getDenomination() != cards[i + 1].getDenomination()) {
  263. if (count1 == 2 || count1 == 1) {
  264. break;
  265. } else {
  266. count1 = 0;
  267. value1 = 0;
  268. }
  269. }
  270. }
  271. for (int j = 0; j < cards.length - 1; j++) {
  272. if (cards[j].getDenomination() == value1) {
  273. continue;
  274. }
  275. if (cards[j].getDenomination() == cards[j + 1].getDenomination()) {
  276. count2++;
  277. value2 = cards[j].getDenomination();
  278. }
  279. if (count2 == 2) {
  280. break;
  281. }
  282. if (cards[j].getDenomination() != cards[j + 1].getDenomination()) {
  283. if (count2 == 1) {
  284. break;
  285. } else count2 = 0;
  286. }
  287. }
  288. if (count1 == count2 && count1 > 1) {
  289. if (value1 > value2) {
  290. player.combination.Value = value1 * 3 + value2 * 2;
  291. } else {
  292. player.combination.Value = value1 * 2 + value2 * 3;
  293. }
  294. player.combination.CurrentCombination = Combination.LIST.FULL_HOUSE;
  295. player.combination.Value *= Combination.Coefficient[Combination.LIST.FULL_HOUSE.ordinal()];
  296. return true;
  297. } else {
  298. if (count1 == 1 && count2 == 2 || count1 == 2 && count2 == 1) {
  299. player.combination.Value = value1 * count1 + value2 * count2 * Combination.Coefficient[Combination.LIST.FULL_HOUSE.ordinal()];
  300. player.combination.CurrentCombination = Combination.LIST.FULL_HOUSE;
  301. return true;
  302. }
  303. }
  304. return false;
  305. }
  306. /**
  307. *
  308. * !!! ДОРАБОТАТЬ !!!
  309. *
  310. */
  311. public boolean isFlush(Card[] cards, Player player) {
  312. int CountHearts = 0;
  313. int CountDiamonds = 0;
  314. int CountClubs = 0;
  315. int CountSpades = 0;
  316. for (int i = 0; i < cards.length - 1; i++) {
  317. if (cards[i].Lear == Lear.NAME.CLUBS) {
  318. CountClubs++;
  319. }
  320. if (cards[i].Lear == Lear.NAME.DIAMONDS) {
  321. CountDiamonds++;
  322. }
  323. if (cards[i].Lear == Lear.NAME.SPADES) {
  324. CountSpades++;
  325. }
  326. if (cards[i].Lear == Lear.NAME.HEARTS) {
  327. CountHearts++;
  328. }
  329. }
  330. if (CountClubs >= 5 || CountDiamonds >= 5 || CountSpades >= 5 || CountHearts >= 5) {
  331. return true;
  332. } else return false;
  333. }
  334. public boolean isStraight(Card[] cards, Player player) {
  335. int counter = 0;
  336. player.combination.Value = 0;
  337. if (cards[0].getDenomination() == 14 && cards[6].getDenomination() == 2) {
  338. boolean CardIsFive = false;
  339. boolean CardIsFour = false;
  340. boolean CardIsThree = false;
  341. for (int i = 0; i < cards.length - 1; i++) {
  342. if (cards[i].getDenomination() == 3) {
  343. CardIsThree = true;
  344. }
  345. if (cards[i].getDenomination() == 4) {
  346. CardIsFour = true;
  347. }
  348. if (cards[i].getDenomination() == 5) {
  349. CardIsFive = true;
  350. }
  351. }
  352. if (CardIsFive && CardIsFour && CardIsThree) {
  353. player.combination.CurrentCombination = Combination.LIST.STRAIGHT;
  354. player.combination.Value = 60 * Combination.LIST.STRAIGHT.ordinal();
  355. return true;
  356. }
  357. }
  358. for (int i = 0; i < cards.length - 1; i++) {
  359. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  360. continue;
  361. }
  362. if (cards[i].getDenomination() != cards[i + 1].getDenomination() + 1) {
  363. if (i < 2) {
  364. counter = 0;
  365. player.combination.Value = 0;
  366. continue;
  367. }
  368. break;
  369. }
  370. counter++;
  371. player.combination.Value += cards[i].getDenomination();
  372. if (counter == 4) {
  373. player.combination.Value += cards[i + 1].getDenomination();
  374. player.combination.Value *= Combination.Coefficient[Combination.LIST.STRAIGHT.ordinal()];
  375. player.combination.CurrentCombination = Combination.LIST.STRAIGHT;
  376. return true;
  377. }
  378. }
  379. return false;
  380. }
  381. public boolean isThreeOfAKind(Card[] cards, Player player) {
  382. int count = 0;
  383. player.combination.Value = 0;
  384. for (int i = 0; i < cards.length - 1; i++) {
  385. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  386. count++;
  387. player.combination.Value += cards[i].getDenomination();
  388. } else {
  389. count = 0;
  390. player.combination.Value = 0;
  391. }
  392. if (count == 2) {
  393. player.combination.Value += cards[i + 1].getDenomination();
  394. player.combination.Value *= Combination.Coefficient[Combination.LIST.THREE_OF_A_KIND.ordinal()];
  395. player.combination.CurrentCombination = Combination.LIST.THREE_OF_A_KIND;
  396. return true;
  397. }
  398. }
  399. return false;
  400. }
  401. public boolean isTwoPairs(Card[] cards, Player player) {
  402. int count1 = 0, value1 = 0;
  403. int count2 = 0, value2 = 0;
  404. player.combination.Value = 0;
  405. for (int i = 0; i < cards.length - 1; i++) {
  406. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  407. count1++;
  408. if (count1 == 1) {
  409. value1 = cards[i].getDenomination();
  410. break;
  411. }
  412. }
  413. }
  414. for (int j = 0; j < cards.length - 1; j++) {
  415. if (cards[j].getDenomination() == value1) {
  416. continue;
  417. }
  418. if (cards[j].getDenomination() == cards[j + 1].getDenomination()) {
  419. count2++;
  420. if (count2 == 1) {
  421. value2 = cards[j].getDenomination();
  422. break;
  423. }
  424. }
  425. }
  426. if (count1 == 1 && count2 == 1) {
  427. player.combination.Value = (value1 * 2 + value2 * 2) * Combination.Coefficient[Combination.LIST.TWO_PAIRS.ordinal()];
  428. player.combination.CurrentCombination = Combination.LIST.TWO_PAIRS;
  429. return true;
  430. }
  431. return false;
  432. }
  433. public boolean isOnePair(Card[] cards, Player player) {
  434. player.combination.Value = 0;
  435. for (int i = 0; i < cards.length - 1; i++) {
  436. if (cards[i].getDenomination() == cards[i + 1].getDenomination()) {
  437. player.combination.Value = cards[i].getDenomination() * 2 * Combination.Coefficient[Combination.LIST.ONE_PAIR.ordinal()];
  438. player.combination.CurrentCombination = Combination.LIST.ONE_PAIR;
  439. return true;
  440. }
  441. }
  442. return false;
  443. }
  444. }