package com.company; import java.util.Random; public class Dealer { private final Player[] players; private final Deck deck = new Deck(); private final Card[] table = new Card[5]; private int CardOnDesk = 0; private RATE_TYPE currentRateType; Random random = new Random(System.currentTimeMillis()); public Dealer(int playerCount) throws Exception { if (playerCount < 2 || playerCount > 9) { throw new Exception("Некорректное количество игроков!"); } players = new Player[playerCount]; for (int i = 0; i < playerCount; i++) { players[i] = new Player("Player-" + (i + 1)); } currentRateType = RATE_TYPE.PREFLOP; } /** * Раздача карт */ public void HandOut() { switch (currentRateType) { case PREFLOP: { for (int i = 0; i < players.length; i++) { int cardIndex1 = random.nextInt(52); int cardIndex2 = random.nextInt(52); Card card1 = deck.Cards[cardIndex1]; Card card2 = deck.Cards[cardIndex2]; if (card1.onHands || card2.onHands) { i--; continue; } card1.onHands = true; card2.onHands = true; players[i].setCards(Player.CARD_SLOT.FIRST, card1); players[i].setCards(Player.CARD_SLOT.SECOND, card2); } currentRateType = RATE_TYPE.FLOP; } break; case FLOP: { PutOnDesk(3); currentRateType = RATE_TYPE.TURN; } break; case TURN: { PutOnDesk(1); currentRateType = RATE_TYPE.RIVER; } break; case RIVER: { PutOnDesk(1); currentRateType = RATE_TYPE.OPENING; } break; case OPENING: { OpeningCard(); CardOnDesk = 0; currentRateType = RATE_TYPE.PREFLOP; } } } /** * Положить на стол cardCount карт * * @param cardCount количество карт для раздачи */ public void PutOnDesk(int cardCount) { for (int i = 0; i < cardCount; i++) { int cardIndex = random.nextInt(52); Card currentCard = deck.Cards[cardIndex]; if (currentCard.onHands) { i--; continue; } table[CardOnDesk++] = currentCard; currentCard.onHands = true; } } // ♦♣♥♠ public void OpeningCard() { System.out.println("Карты на столе "); for (Card card : table) { System.out.print( deck.CardName.get(card.getDenomination()) + Lear.SYMBOL[card.Lear.ordinal()] + " "); } System.out.println("\n\nКарты игроков"); for (Player player : players) { Card[] sharedCards = new Card[7]; int i = 0; for (; i < table.length; i++) { sharedCards[i] = table[i]; } sharedCards[i++] = player.getCards(Player.CARD_SLOT.FIRST); sharedCards[i] = player.getCards(Player.CARD_SLOT.SECOND); try { Sorting(sharedCards); } catch (Exception e) { e.printStackTrace(); } PrintCard(sharedCards); if (!isRoyalFlush(sharedCards, player)) { if (!isStraightFlush(sharedCards, player)) { if (!isFourOfAKind(sharedCards, player)) { if (!isFullHouse(sharedCards, player)) { if (!isFlush(sharedCards, player)) { if (!isStraight(sharedCards, player)) { if (!isThreeOfAKind(sharedCards, player)) { if (!isTwoPairs(sharedCards, player)) { if (!isOnePair(sharedCards, player)) { } } } } } } } } } System.out.println(player.getName() + " " + player.combination.Value); } for (int k = 0; k < players.length; k++) { for (int l = 1; l < players.length; l++) { if (players[k].combination.Value == players[l].combination.Value) { for (Card card : sharedCards) { } } } } } public void PrintCard(Card[] cards) { for (Card card : cards) { System.out.print( deck.CardName.get(card.getDenomination()) + Lear.SYMBOL[card.Lear.ordinal()] + " "); } System.out.println(); } /** * Сортировка массива карт по убыванию его номинала * * @param cards массив карт (7 штук) */ public void Sorting(Card[] cards) throws Exception { if (cards.length != 7) { throw new Exception("Что-то пошло не так"); } for (int i = 0; i < cards.length; i++) { for (int j = 0; j < cards.length - 1; j++) { if (cards[j].getDenomination() < cards[j + 1].getDenomination()) { Card temp = cards[j]; cards[j] = cards[j + 1]; cards[j + 1] = temp; } } } } /** * Проверяет есть ли в переданном массиве комбинация Стрит * * @param cards массив карт */ public boolean isRoyalFlush(Card[] cards, Player player) { boolean cardK = false; boolean cardQ = false; boolean cardJ = false; boolean card10 = false; if (cards[0].getDenomination() == 14 && isStraightFlush(cards, player)) { for (int i = 0; i < cards.length - 1; i++) { if (cards[i].getDenomination() == 13) { cardK = true; } if (cards[i].getDenomination() == 12) { cardQ = true; } if (cards[i].getDenomination() == 11) { cardJ = true; } if (cards[i].getDenomination() == 10) { card10 = true; } } if (card10 && cardJ && cardK && cardQ) { player.combination.CurrentCombination = Combination.LIST.ROYAL_FLUSH; player.combination.Value = 60 * Combination.Coefficient[Combination.LIST.ROYAL_FLUSH.ordinal()]; return true; } } return false; } /** * Стрит-флеш * @param cards массив карт игрока и на столе отсортированных по номиналу * @param player экземпляр игрока */ public boolean isStraightFlush(Card[] cards, Player player) { int counter = 0; player.combination.Value = 0; for (int i = 0; i < cards.length - 1; i++) { if (cards[i].getDenomination() == cards[i + 1].getDenomination()) { continue; } if (cards[i].getDenomination() != cards[i + 1].getDenomination() + 1) { if (i < 2) { counter = 0; player.combination.Value = 0; continue; } break; } if (cards[i].Lear == cards[i + 1].Lear && cards[i].getDenomination() == cards[i + 1].getDenomination() + 1) { counter++; player.combination.Value += cards[i].getDenomination(); if (counter == 4) { player.combination.CurrentCombination = Combination.LIST.STRAIGHT_FLUSH; player.combination.Value += cards[i + 1].getDenomination(); player.combination.Value *= Combination.Coefficient[Combination.LIST.STRAIGHT_FLUSH.ordinal()]; return true; } } } return false; } /** * Каре (четыре одного вида) * @param cards массив карт игрока и на столе отсортированных по номиналу * @param player экземпляр игрока */ public boolean isFourOfAKind(Card[] cards, Player player) { int count = 0; player.combination.Value = 0; for (int i = 0; i < cards.length - 1; i++) { if (cards[i].getDenomination() == cards[i + 1].getDenomination()) { count++; player.combination.Value += cards[i].getDenomination(); if (count == 3) { player.combination.Value += cards[i + 1].getDenomination(); player.combination.Value *= Combination.Coefficient[Combination.LIST.FOUR_OF_A_KIND.ordinal()]; return true; } } else { count = 0; player.combination.Value = 0; } } return false; } public boolean isFullHouse(Card[] cards, Player player) { int count1 = 0; int count2 = 0; int value1 = 0; int value2 = 0; player.combination.Value = 0; for (int i = 0; i < cards.length - 1; i++) { if (cards[i].getDenomination() == cards[i + 1].getDenomination()) { count1++; value1 = cards[i].getDenomination(); } if (cards[i].getDenomination() != cards[i + 1].getDenomination()) { if (count1 == 2 || count1 == 1) { break; } else { count1 = 0; value1 = 0; } } } for (int j = 0; j < cards.length - 1; j++) { if (cards[j].getDenomination() == value1) { continue; } if (cards[j].getDenomination() == cards[j + 1].getDenomination()) { count2++; value2 = cards[j].getDenomination(); } if (count2 == 2) { break; } if (cards[j].getDenomination() != cards[j + 1].getDenomination()) { if (count2 == 1) { break; } else count2 = 0; } } if (count1 == count2 && count1 > 1) { if (value1 > value2) { player.combination.Value = value1 * 3 + value2 * 2; } else { player.combination.Value = value1 * 2 + value2 * 3; } player.combination.CurrentCombination = Combination.LIST.FULL_HOUSE; player.combination.Value *= Combination.Coefficient[Combination.LIST.FULL_HOUSE.ordinal()]; return true; } else { if (count1 == 1 && count2 == 2 || count1 == 2 && count2 == 1) { player.combination.Value = value1 * count1 + value2 * count2 * Combination.Coefficient[Combination.LIST.FULL_HOUSE.ordinal()]; player.combination.CurrentCombination = Combination.LIST.FULL_HOUSE; return true; } } return false; } /** * * !!! ДОРАБОТАТЬ !!! * */ public boolean isFlush(Card[] cards, Player player) { int CountHearts = 0; int CountDiamonds = 0; int CountClubs = 0; int CountSpades = 0; for (int i = 0; i < cards.length - 1; i++) { if (cards[i].Lear == Lear.NAME.CLUBS) { CountClubs++; } if (cards[i].Lear == Lear.NAME.DIAMONDS) { CountDiamonds++; } if (cards[i].Lear == Lear.NAME.SPADES) { CountSpades++; } if (cards[i].Lear == Lear.NAME.HEARTS) { CountHearts++; } } if (CountClubs >= 5 || CountDiamonds >= 5 || CountSpades >= 5 || CountHearts >= 5) { return true; } else return false; } public boolean isStraight(Card[] cards, Player player) { int counter = 0; player.combination.Value = 0; if (cards[0].getDenomination() == 14 && cards[6].getDenomination() == 2) { boolean CardIsFive = false; boolean CardIsFour = false; boolean CardIsThree = false; for (int i = 0; i < cards.length - 1; i++) { if (cards[i].getDenomination() == 3) { CardIsThree = true; } if (cards[i].getDenomination() == 4) { CardIsFour = true; } if (cards[i].getDenomination() == 5) { CardIsFive = true; } } if (CardIsFive && CardIsFour && CardIsThree) { player.combination.CurrentCombination = Combination.LIST.STRAIGHT; player.combination.Value = 60 * Combination.LIST.STRAIGHT.ordinal(); return true; } } for (int i = 0; i < cards.length - 1; i++) { if (cards[i].getDenomination() == cards[i + 1].getDenomination()) { continue; } if (cards[i].getDenomination() != cards[i + 1].getDenomination() + 1) { if (i < 2) { counter = 0; player.combination.Value = 0; continue; } break; } counter++; player.combination.Value += cards[i].getDenomination(); if (counter == 4) { player.combination.Value += cards[i + 1].getDenomination(); player.combination.Value *= Combination.Coefficient[Combination.LIST.STRAIGHT.ordinal()]; player.combination.CurrentCombination = Combination.LIST.STRAIGHT; return true; } } return false; } public boolean isThreeOfAKind(Card[] cards, Player player) { int count = 0; player.combination.Value = 0; for (int i = 0; i < cards.length - 1; i++) { if (cards[i].getDenomination() == cards[i + 1].getDenomination()) { count++; player.combination.Value += cards[i].getDenomination(); } else { count = 0; player.combination.Value = 0; } if (count == 2) { player.combination.Value += cards[i + 1].getDenomination(); player.combination.Value *= Combination.Coefficient[Combination.LIST.THREE_OF_A_KIND.ordinal()]; player.combination.CurrentCombination = Combination.LIST.THREE_OF_A_KIND; return true; } } return false; } public boolean isTwoPairs(Card[] cards, Player player) { int count1 = 0, value1 = 0; int count2 = 0, value2 = 0; player.combination.Value = 0; for (int i = 0; i < cards.length - 1; i++) { if (cards[i].getDenomination() == cards[i + 1].getDenomination()) { count1++; if (count1 == 1) { value1 = cards[i].getDenomination(); break; } } } for (int j = 0; j < cards.length - 1; j++) { if (cards[j].getDenomination() == value1) { continue; } if (cards[j].getDenomination() == cards[j + 1].getDenomination()) { count2++; if (count2 == 1) { value2 = cards[j].getDenomination(); break; } } } if (count1 == 1 && count2 == 1) { player.combination.Value = (value1 * 2 + value2 * 2) * Combination.Coefficient[Combination.LIST.TWO_PAIRS.ordinal()]; player.combination.CurrentCombination = Combination.LIST.TWO_PAIRS; return true; } return false; } public boolean isOnePair(Card[] cards, Player player) { player.combination.Value = 0; for (int i = 0; i < cards.length - 1; i++) { if (cards[i].getDenomination() == cards[i + 1].getDenomination()) { player.combination.Value = cards[i].getDenomination() * 2 * Combination.Coefficient[Combination.LIST.ONE_PAIR.ordinal()]; player.combination.CurrentCombination = Combination.LIST.ONE_PAIR; return true; } } return false; } }