123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550 |
- 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(0, card1);
- players[i].setCards(1, 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() {
- int slot = 2;
- System.out.println("Карты на столе ");
- for (Card card : table) {
- System.out.print(
- deck.CardName.get(card.getDenomination())
- + Lear.SYMBOL[card.Lear.ordinal()] + " ");
- for (Player p : players) {
- p.setCards(slot, card);
- }
- slot++;
- }
- System.out.println("\n\nКарты игроков");
- for (Player player : players) {
- try {
- Sorting(player.cards);
- } catch (Exception e) {
- e.printStackTrace();
- }
- player.Calc();
- PrintCard(player.cards);
- if (!isRoyalFlush(player.cards, player)) {
- if (!isStraightFlush(player.cards, player)) {
- if (!isFourOfAKind(player.cards, player)) {
- if (!isFullHouse(player.cards, player)) {
- if (!isFlush(player.cards, player)) {
- if (!isStraight(player.cards, player)) {
- if (!isThreeOfAKind(player.cards, player)) {
- if (!isTwoPairs(player.cards, player)) {
- if (!isOnePair(player.cards, player)) {
- player.combination.Value = player.cards[0].getDenomination();
- player.combination.CurrentCombination = Combination.LIST.HIGH_CARD;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- System.out.println(player.getName() + " " + player.combination.Value);
- }
- Sorting(players);
- Player pWin;
- int count = 0;
- if (players[0].combination.Value == players[1].combination.Value) {
- int value = players[0].combination.Value;
- count++;
- for (int q = 2; q < players.length - 1; q++) {
- if (players[q].combination.Value == players[q + 1].combination.Value) {
- if (value == players[q].combination.Value) {
- count++;
- } else {
- break;
- }
- }
- }
- }
- if (count > 0) {
- Player[] winPlayers = new Player[count + 1];
- for (int wp = 0; wp <= count; wp++) {
- winPlayers[wp] = players[wp];
- }
- SortingBySum(winPlayers);
- pWin = winPlayers[0];
- } else {
- pWin = players[0];
- }
- System.out.println("Игрок " + pWin.getName() + " выйграл");
- }
- 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;
- }
- }
- }
- }
- public void Sorting(Player[] players) {
- for (int i = 0; i < players.length; i++) {
- for (int j = 0; j < players.length - 1; j++) {
- if (players[j].combination.Value < players[j + 1].combination.Value) {
- Player temp = players[j];
- players[j] = players[j + 1];
- players[j + 1] = temp;
- }
- }
- }
- }
- public void SortingBySum(Player[] players) {
- for (int i = 0; i < players.length; i++) {
- for (int j = 0; j < players.length - 1; j++) {
- if (players[j].SumDenomination < players[j + 1].SumDenomination) {
- Player temp = players[j];
- players[j] = players[j + 1];
- players[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;
- }
- }
|