Browse Source

Чуточку поправил, готово для написания алгоритмов определения комбинаций

Alec 3 years ago
parent
commit
40c00b83da

+ 2 - 7
src/com/company/Card.java

@@ -1,8 +1,5 @@
 package com.company;
 
-import java.util.HashMap;
-import java.util.Map;
-
 public class Card {
     // Номинал карты (2 - 14)
     private int Denomination;
@@ -22,18 +19,16 @@ public class Card {
     /**
      * Установить номинал карты. Диапазон допустимых значений 2-14.
      * @param denomination номинал карты
-     * @return true если удалось установить
      */
-    public boolean setDenomination(int denomination) {
+    public void setDenomination(int denomination) {
         // Проверим, что переданное значение в метод находится
         // в нужном диапазоне допустимых значений.
         if (denomination < 2 || denomination > 14) {
             // Вернуть ложь, если переданное значение некорректное
-            return false;
+            return;
         }
         // Установить значение переданное в метод текущей карте
         Denomination = denomination;
         // Вернуть истину, если номинал установлен
-        return true;
     }
 }

+ 222 - 208
src/com/company/Dealler.java → src/com/company/Dealer.java

@@ -1,208 +1,222 @@
-package com.company;
-
-import java.util.Random;
-
-public class Dealler {
-    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 Dealler(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();
-        }
-
-        currentRateType = RATE_TYPE.PREFLOP;
-    }
-
-    public void HandOut() {
-        boolean isFirstCardOnHand = false;
-        switch (currentRateType) {
-            case PREFLOP: {
-                for (int i = 0; i < players.length; i++) {
-                    int cardIndex = random.nextInt(52);
-                    Card currentCard = deck.Cards[cardIndex];
-                    if (!isFirstCardOnHand) {
-                        if (currentCard.onHands) {
-                            i--;
-                            continue;
-                        }
-                        currentCard.onHands = true;
-                        players[i].setCards(PLAYER_CARD.FIRST, currentCard);
-                    }
-                    isFirstCardOnHand = true;
-                    cardIndex = random.nextInt(52);
-                    currentCard = deck.Cards[cardIndex];
-                    if (currentCard.onHands) {
-                        i--;
-                        continue;
-                    }
-                    currentCard.onHands = true;
-                    players[i].setCards(PLAYER_CARD.SECOND, currentCard);
-                    isFirstCardOnHand = false;
-                }
-                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;
-            }
-        }
-    }
-
-    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;
-        }
-    }
-// ♦♣♥♠
-    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.FIRST);
-            sharedCards[i] = player.getCards(PLAYER_CARD.SECOND);
-
-            try {
-                Sorting(sharedCards);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-
-            for (i = 0; i < sharedCards.length; i++) {
-                System.out.print(
-                        deck.CardName.get(sharedCards[i].getDenomination())
-                                + Lear.SYMBOL[sharedCards[i].Lear.ordinal()] + " ");
-            }
-            System.out.println();
-
-//            System.out.print(
-//                    deck.CardName.get(player.getCards(PLAYER_CARD.FIRST).getDenomination())
-//                            + Lear.SYMBOL[player.getCards(PLAYER_CARD.FIRST).Lear.ordinal()] + " "
-//            );
-//            System.out.println(
-//                    deck.CardName.get(player.getCards(PLAYER_CARD.SECOND).getDenomination())
-//                            + Lear.SYMBOL[player.getCards(PLAYER_CARD.SECOND).Lear.ordinal()] + " "
-//            );
-        }
-    }
-
-    /**
-     * Сортировка массива карт по убыванию его номинала
-     * @param cards массив карт (7 штук)
-     * @throws Exception
-     */
-    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 массив карт
-     * @return true, если есть
-     */
-    public boolean isStreet(Card []cards) {
-        if (cards.length != 7) {
-            return false;
-        }
-
-        // ВОТ ТУТ ДЫРА В КОДЕ :)
-        // Стрит не всегда может не с 0 элемента начинаться
-        int counter = 0;
-        for (int i = 0; i < cards.length - 1; i++) {
-            if (counter == 5) {
-                return true;
-            }
-            if (cards[i].getDenomination() != cards[i + 1].getDenomination() + 1) {
-                break;
-            }
-
-            counter++;
-        }
-
-        return false;
-    }
-
-    public boolean isFlash(Card []cards, int cardCount) {
-        Lear.NAME currentLear;
-        int learCount = 0;
-
-        for (int i = 0; i < cardCount; i++) {
-            currentLear = cards[i].Lear;
-
-            for (int j = 0; j < cardCount; j++) {
-                if (i == j) {
-                    continue;
-                }
-
-                if (cards[j].Lear == currentLear) {
-                    learCount++;
-                }
-            }
-        }
-
-        return learCount >= 5;
-    }
-
-    public boolean isRoyalFlash(Card []cards) {
-        if (cards[0].getDenomination() != 14) {
-            return false;
-        }
-
-        return (isFlash(cards, 5) && isStreet(cards));
-    }
-}
-
-
-// A K Q J 10 все одной масти
+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);
+
+//            System.out.print(
+//                    deck.CardName.get(player.getCards(PLAYER_CARD.FIRST).getDenomination())
+//                            + Lear.SYMBOL[player.getCards(PLAYER_CARD.FIRST).Lear.ordinal()] + " "
+//            );
+//            System.out.println(
+//                    deck.CardName.get(player.getCards(PLAYER_CARD.SECOND).getDenomination())
+//                            + Lear.SYMBOL[player.getCards(PLAYER_CARD.SECOND).Lear.ordinal()] + " "
+//            );
+        }
+    }
+
+    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 isStreet(Card[] cards) {
+        int counter = 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;
+                    continue;
+                }
+                break;
+            }
+
+            counter++;
+
+            if (counter >= 4) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    public boolean isFlash(Card[] cards, int cardCount) {
+        Lear.NAME currentLear;
+        int learCount = 0;
+
+        for (int i = 0; i < cardCount; i++) {
+            currentLear = cards[i].Lear;
+
+            for (int j = 0; j < cardCount; j++) {
+                if (i == j) {
+                    continue;
+                }
+
+                if (cards[j].Lear == currentLear) {
+                    learCount++;
+                }
+            }
+        }
+
+        return learCount >= 5;
+    }
+
+    public boolean isRoyalFlash(Card[] cards) {
+        if (cards[0].getDenomination() != 14) {
+            return false;
+        }
+
+        return (isFlash(cards, 5) && isStreet(cards));
+    }
+}
+
+
+// A K Q J 10 все одной масти

+ 1 - 1
src/com/company/LEAR.java → src/com/company/Lear.java

@@ -2,7 +2,7 @@ package com.company;
 
 
 
-class Lear {
+public class Lear {
     /**
      * Перечисление мастей карт
      */

+ 27 - 6
src/com/company/Main.java

@@ -4,11 +4,32 @@ public class Main {
 
     public static void main(String[] args) throws Exception {
 
-        Dealler dealler = new Dealler(4);
-        dealler.HandOut();
-        dealler.HandOut();
-        dealler.HandOut();
-        dealler.HandOut();
-        dealler.HandOut();
+        Dealer dealer = new Dealer(4);
+//        dealer.HandOut();
+//        dealer.HandOut();
+//        dealer.HandOut();
+//        dealer.HandOut();
+//        dealer.HandOut();
+
+        // TEST STREET COMBINATION
+
+        Card []cards = new Card[7];
+        for (int i = 0; i < cards.length; i++) {
+            cards[i] = new Card();
+            cards[i].Lear = Lear.NAME.CLUBS;
+        }
+
+        cards[0].setDenomination(14);
+        cards[1].setDenomination(12);
+        cards[2].setDenomination(11);
+        cards[3].setDenomination(10);
+        cards[4].setDenomination(9);
+        cards[5].setDenomination(9);
+        cards[6].setDenomination(8);
+
+        dealer.Sorting(cards);
+        dealer.PrintCard(cards);
+
+        System.out.println("STREET TEST " + dealer.isStreet(cards));
     }
 }

+ 20 - 10
src/com/company/Player.java

@@ -1,28 +1,38 @@
 package com.company;
 
+
+
 /**
- * Перечисление карт игрока.
- * FIRST - первая карта (или 0)
- * SECOND - вторая карта (или 1)
+ * Игрок
  */
-enum PLAYER_CARD {
-    FIRST,
-    SECOND,
-}
-
 public class Player {
     private final Card[] cards = new Card[2];
+    private String name;
+
+    public Player(String name) {
+        this.name = name;
+    }
 
     /**
      * Установить карту в указанный слот
      * @param slot
      * @param card
      */
-    public void setCards(PLAYER_CARD slot, Card card) {
+    public void setCards(CARD_SLOT slot, Card card) {
         cards[slot.ordinal()] = card;
     }
 
-    public Card getCards(PLAYER_CARD slot) {
+    public Card getCards(CARD_SLOT slot) {
         return cards[slot.ordinal()];
     }
+
+    /**
+     * Перечисление карт игрока.
+     * FIRST - первая карта (или 0)
+     * SECOND - вторая карта (или 1)
+     */
+    public enum CARD_SLOT {
+        FIRST,
+        SECOND,
+    }
 }

+ 1 - 1
src/com/company/RATE_TYPE.java

@@ -9,4 +9,4 @@ public enum RATE_TYPE {
     TURN,
     RIVER,
     OPENING,
-}
+};