我正在创建一个德州扑克游戏,如果您知道扑克,这很简单。
我正在研究SevenCardHand类,该类基本上在构造函数中使用7张卡的ArrayList。我正在创建用于查找7张牌的手的等级的算法(无论是同花,顺子,对子还是三张同等...)
但是有些手出现的几率很低,因此要测试这一点,我想自动处理整手牌。因此,我循环遍历创建新的SevenCardHands并打印它们是否齐平(我正在测试的是齐平的)]
问题是,它不会让我用for循环创建超过7手牌。该程序不会终止,但是不会取得任何进一步的进展。不管我做了什么,冻结之前它的产量都不会超过7。
这是我的Card,DeckOfCards,HoldEmDriver和SevenCardHand类中的代码
import java.util.ArrayList;导入java.util.Scanner;
公共类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());
}
公共枚举卡{
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;
}
}
import java.util.ArrayList;导入java.util.Random;
公共类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;
}
}
import java.util.ArrayList;
公共类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;
}
}
如果您要查找的是某人在七张牌中有同花的几率,我不完全确定为什么要使用double-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;