【问题标题】:Problem with for loop stopping while checking Poker Hand检查扑克手时for循环停止的问题
【发布时间】:2020-05-20 02:52:43
【问题描述】:

我正在创建一个德州扑克游戏,如果你知道扑克,这很简单。

我正在研究一个 SevenCardHand 类,它基本上在构造函数中采用 7 张卡片的 ArrayList。我正在创建算法来查找 7 张牌的等级(无论是同花、顺子、对子、三种...)

但是有些手牌发生的几率非常低,所以为了测试这一点,我想自动处理一大堆手牌。所以我正在循环创建新的 SevenCardHands 并打印它们是否是同花顺(是我现在正在测试的)

问题是它不会让我用那个 for 循环创建超过 7 个手。该程序不会终止,但不会取得任何进展。不管我做了什么,它在冻结之前不会超过七。

这是我的 Card、DeckOfCards、HoldEmDriver 和 SevenCardHand 类的代码

班级HoldEmDriver.java

import java.util.ArrayList;
import java.util.Scanner;

public class HoldEmDriver {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        DeckOfCards theDeck = new DeckOfCards();
        TableCards table = new TableCards();
        SevenCardHand seven = new SevenCardHand();
        ArrayList<Card> cards = new ArrayList();


        for(int j=0; j<7; j++) {
            cards.clear();
            for(int i=0; i<7; i++) {
                cards.add(theDeck.dealCard());
            }
            seven = new SevenCardHand(cards);
            System.out.println(""+seven+seven.hasFlush());
        }


public enum Card {

    ACEOFSPADES(1, "Spades"),
    TWOOFSPADES(2, "Spades"),
    THREEOFSPADES(3, "Spades"),
    FOUROFSPADES(4, "Spades"),
    FIVEOFSPADES(5, "Spades"),
    SIXOFSPADES(6, "Spades"),
    SEVENOFSPADES(7, "Spades"),
    EIGHTOFSPADES(8, "Spades"),
    NINEOFSPADES(9, "Spades"),
    TENOFSPADES(10, "Spades"),
    JACKOFSPADES(11, "Spades"),
    QUEENOFSPADES(12, "Spades"),
    KINGOFSPADES(13, "Spades"),
    ACEOFCLUBS(1, "Clubs"),
    TWOOFCLUBS(2, "Clubs"),
    THREEOFCLUBS(3, "Clubs"),
    FOUROFCLUBS(4, "Clubs"),
    FIVEOFCLUBS(5, "Clubs"),
    SIXOFCLUBS(6, "Clubs"),
    SEVENOFCLUBS(7, "Clubs"),
    EIGHTOFCLUBS(8, "Clubs"),
    NINEOFCLUBS(9, "Clubs"),
    TENOFCLUBS(10, "Clubs"),
    JACKOFCLUBS(11, "Clubs"),
    QUEENOFCLUBS(12, "Clubs"),
    KINGOFCLUBS(13, "Clubs"),
    ACEOFDIAMONDS(1, "Diamonds"),
    TWOOFDIAMONDS(2, "Diamonds"),
    THREEOFDIAMONDS(3, "Diamonds"),
    FOUROFDIAMONDS(4, "Diamonds"),
    FIVEOFDIAMONDS(5, "Diamonds"),
    SIXOFDIAMONDS(6, "Diamonds"),
    SEVENOFDIAMONDS(7, "Diamonds"),
    EIGHTOFDIAMONDS(8, "Diamonds"),
    NINEOFDIAMONDS(9, "Diamonds"),
    TENOFDIAMONDS(10, "Diamonds"),
    JACKOFDIAMONDS(11, "Diamonds"),
    QUEENOFDIAMONDS(12, "Diamonds"),
    KINGOFDIAMONDS(13, "Diamonds"),
    ACEOFHEARTS(1, "Hearts"),
    TWOOFHEARTS(2, "Hearts"),
    THREEOFHEARTS(3, "Hearts"),
    FOUROFHEARTS(4, "Hearts"),
    FIVEOFHEARTS(5, "Hearts"),
    SIXOFHEARTS(6, "Hearts"),
    SEVENOFHEARTS(7, "Hearts"),
    EIGHTOFHEARTS(8, "Hearts"),
    NINEOFHEARTS(9, "Hearts"),
    TENOFHEARTS(10, "Hearts"),
    JACKOFHEARTS(11, "Hearts"),
    QUEENOFHEARTS(12, "Hearts"),
    KINGOFHEARTS(13, "Hearts");

    /**The value of the card Ace-King */
    private int cardValue;
    /** The suit of the card SCDM */
    private String cardSuit;


    /**
     * Creates a card object with a suit and value
     * @param cardValue the cards value
     * @param cardSuit the cards suit
     */
    private Card(int cardValue, String cardSuit) {
        this.cardValue = cardValue;
        this.cardSuit = cardSuit;
    }

    /** Checks if this card is the same card as other
     * @param other Another card
     * @return True if the same, false otherwise
     */
    public boolean isSameCard(Card other) {
        if(other.cardValue == cardValue && cardSuit.equals(other.cardSuit)) {
            return true;
        } return false;
    }

    /**
     * 
     * @return This cards value Ace-King
     */
    public int getCardValue() {
        return cardValue;
    }

    /**
     * Gets this cards suit
     * @return The card's suit
     */
    public String getCardSuit() {
        return cardSuit;
    }

    /**
     * Checks if two cards have the same suit
     * @param other The other card
     * @return True if same suit, false otherwise
     */
    public boolean isSameSuit(Card other) {
        if(this.cardSuit.equals(other.cardSuit)) {
            return true;
        } return false;
    }

    /**
     * Checks if a card is followed by another card
     * 2 followed by 3, 10 followed by Jack
     * @param other The card to be compared with
     * @return True if other's cardValue is 1 higher than this card
     * False otherwise
     */
    public boolean isFollowedBy(Card other) {
        if(this.cardValue==other.cardValue-1) {
            return true;
        }
        return false;
    }


    /**
     * Checks if a card has the same value as another card
     * @param other The card to compare with
     * @return True if same value, false otherwise
     */
    public boolean isSameValue(Card other) {
        if(this.cardValue==other.cardValue) {
            return true;
        }
        return false;
    }



    /**
     * Returns the card in readable format
     * Ace of Hearts
     * 7 of Clubs
     * 
     */
    public String toString() {
        String card = "";
        if(cardValue == 1) {
            card = card + "Ace ";
        } else if(cardValue == 11) {
            card = card + "Jack ";
        } else if(cardValue == 12) {
            card = card + "Queen ";
        } else if(cardValue == 13) {
            card = card + "King ";
        } else {
            card = card + cardValue+" ";
        }
        if(cardSuit.equals("Spades")) {
            card = card + "of Spades";
        } else if(cardSuit.equals("Clubs")) {
            card = card + "of Clubs";
        } else if(cardSuit.equals("Hearts")) {
            card = card + "of Hearts";
        } else if(cardSuit.equals("Diamonds")) {
            card = card + "of Diamonds";
        }
        return card;
    }


}

班级DeckOfCards.java

import java.util.ArrayList;
import java.util.Random;

public class DeckOfCards {

    private ArrayList<Card> dealtCards= new ArrayList();
    private Random random = new Random();

    /**
     * Makes a Deck with no cards yet dealt
     */
    public DeckOfCards() {

    }

    /**
     * Deals a random card from the deck that hasn't been
     * dealt already, and adds that card to dealtCards
     * @return A unique Card object randomly chosen
     */
    public Card dealCard() {
        Card newCard = Card.values()[random.nextInt(52)];
        while(isAlreadyDealt(newCard)) {
            newCard = Card.values()[random.nextInt(52)];
        }
        dealtCards.add(newCard);
        return newCard;
    }


    /**
     * Checks if a card has already been dealt
     * @param theCard The card to check
     * @return True if the card has been dealt, otherwise false
     */
    public boolean isAlreadyDealt(Card theCard) {
        boolean isDealt = false;
        for(int i=0; i<dealtCards.size(); i++) {
            if (theCard.isSameCard(dealtCards.get(i))) {
                return true;
            }
        }
        return false;
    }


}

班级SevenCardHand.java

import java.util.ArrayList;

public class SevenCardHand {

    ArrayList<Card> allCards = new ArrayList();

    public SevenCardHand(ArrayList<Card> holeCards, ArrayList<Card> tableCards) {
        if(holeCards.size()!=2 || tableCards.size()!=5) {
            throw new IllegalArgumentException();
        }
        allCards.addAll(holeCards);
        allCards.addAll(tableCards);
        allCards.sort(new CardComparator());
    }

    public SevenCardHand() {
    }

    public SevenCardHand(ArrayList<Card> sevenCards) {
        if(sevenCards.size()!=7) {
            throw new IllegalArgumentException();
        }
        allCards.addAll(sevenCards);
        allCards.sort(new CardComparator());
    }


    public ArrayList<Card> getAllCards(){
        return allCards;
    }


    public String toString() {
        allCards.sort(new CardComparator());
        return ""+allCards;
    }


    public boolean hasRoyalFlush() {

    }



    public boolean hasStraightFlush() {


    }


    public boolean hasFourOfAKind() {


    }

    public boolean hasFullHouse() {



    }


    public boolean hasFlush() {
        int clubs = 0;
        int spades = 0;
        int hearts = 0;
        int diamonds = 0;

        for(Card current: allCards) {
            if(current.getCardSuit().equals("Spades")) {
                spades = spades + 1;
            } else if(current.getCardSuit().equals("Clubs")) {
                clubs = clubs + 1;
            } else if(current.getCardSuit().equals("Hearts")) {
                hearts = hearts + 1;
            } else if(current.getCardSuit().equals("Diamonds")) {
                diamonds = diamonds + 1;
            }
        }
        if(clubs>4 || spades>4 || hearts>4 || diamonds>4) {
            return true;
        }
        return false;
    }


    public boolean hasStraight() {
        int cardsInRow = 1;
        int lastInStraight = allCards.get(0).getCardValue();
        for(int i=1; i<allCards.size(); i++) {
            if(allCards.get(i).getCardValue()==(lastInStraight+1)) {
                cardsInRow = cardsInRow + 1;
                lastInStraight = lastInStraight+1;
            } else if(allCards.get(i).getCardValue()!=lastInStraight) {
                cardsInRow = 1;
                lastInStraight = allCards.get(i).getCardValue();
            }
            if(lastInStraight == 13 && cardsInRow>3) {
                if(allCards.get(0).getCardValue()==1) {
                    return true;
                }
            }
            if (cardsInRow >4) {
                return true;
            }
        }
        return false;

    }


    public boolean hasThreeOfAKind() {
        for(int i=0; i<allCards.size()-2; i++) {
            if(allCards.get(i).isSameValue(allCards.get(i+1))) {
                if(allCards.get(i).isSameValue(allCards.get(i+2))) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean hasTwoPair() {
        int numberOfPairs = 0;
        int valueOfFirstPair = 0;
        for(int i=0; i<allCards.size()-1; i++) {
            if(allCards.get(i).isSameValue(allCards.get(i+1))) {
                if(allCards.get(i).getCardValue()!=valueOfFirstPair) {
                    numberOfPairs = numberOfPairs + 1;
                    valueOfFirstPair = allCards.get(i).getCardValue();
                }
            }
            if(numberOfPairs>1) {
                return true;
            }
        }
        return false;
    }


    public boolean hasPair() {
        for(int i=0; i<allCards.size()-1; i++) {
            if(allCards.get(i).isSameValue(allCards.get(i+1))) {
                return true;
            }
        }
        return false;
    }
}

【问题讨论】:

  • 一副标准牌组的 7 张牌不能超过七张。
  • 谢谢!一定是这个。谢谢。

标签: java loops for-loop poker


【解决方案1】:

我不完全确定为什么要使用双 for 循环来执行此操作,如果您要查找的是某人在七张牌中拥有同花的几率。据我所知,外循环在这里给你带来了麻烦,因为它只要求 7 次新的 7 张牌。为了增加您的样本量,我可能会尝试以下方法:

int count = 0;
int not_flush = 0;
int is_flush = 0;
while (count < 1000) {
    cards.clear();
    for(int i=0; i<7; i++) {
        cards.add(theDeck.dealCard());
    }
    seven = new SevenCardHand(cards);
    if (seven.hasFlush()) {
        is_flush++;
    }
    else {
        not_flush++;
    }
    count++;
}
float result = (float)is_flush/(float)not_flush;

【讨论】:

    猜你喜欢
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多