我正在编写一个代码,以查找用户获得的卡片。返回是否为RoyalFlush。
我有一些找到RoyalFlush的代码,但未按预期工作。
这是我的代码;
{
private Card [] hand; // the hand of 5 cards
// the default constructor
public PokerHand ()
{
hand = new Card [5];
}
// A constructor to help with testing
public PokerHand (Card c0, Card c1, Card c2, Card c3, Card c4)
{
hand = new Card [5];
hand[0] = c0;
hand[1] = c1;
hand[2] = c2;
hand[3] = c3;
hand[4] = c4;
}
// This methods fills the hand with cards from the deck.
// It uses an insertion sort so that the cards are ordered by rank.
public void fillHand (Deck deck)
{
for (int i=0; i<5; i++)
{
int j=i-1;
Card temp = deck.dealCard();
while (j>=0 && hand[j].getRank() > temp.getRank())
{
hand[j+1] = hand[j];
j--;
}
hand[j+1] = temp;
}
}
// PLACE ADDITIONAL METHODS AFTER THIS COMMENT
public boolean isRoyalFlush()
{
boolean royalFlush = false;
// simplify the name of hand
Card c0 = hand[0];
Card c1 = hand[1];
Card c2 = hand[2];
Card c3 = hand[3];
Card c4 = hand[4];
// if the five cards in this hand have the same suit, it is flush
if((c0.getSuit() == c1.getSuit()) && (c1.getSuit() == c2.getSuit())
&& (c2.getSuit() == c3.getSuit()) && (c3.getSuit() == c4.getSuit()))
{
// if the five cards in this hand consists of ace, king, queen, jack
// and ten, it is royal
if((c0.getRank() == 10) && (c1.getRank() == 11) && (c2.getRank() == 12)
&& (c3.getRank() == 13) && (c4.getRank() == 14))
royalFlush = true;
}
return royalFlush;
}
在另一堂课上,我将需要计算有多少用户获得了RoyalFlush。
if(hand.isRoyalFlush())
count[0]++;
但计数始终显示为0。
最佳答案
因为您已经检查了具有特定等级的特定卡。每个人只有一张等级。在哪里进行皇家同花顺的命令可以是任何东西。您要检查的是第一张卡是10,第二张是插孔,依此类推。但是在某些情况下,第一张牌是第二张牌是10,依此类推,这将是一次皇家同花顺。
解决方案-在检查皇家同花顺之前,根据等级对卡列表进行排序