我还有另一个必须做的练习,我真的需要帮助。我什至不知道我的 isFlush 方法是否有效,因为我的牌组似乎没有洗牌和发牌,我完全被卡住了。有人可以帮助我或为我指明正确的方向吗?这是练习:
习题 12.5 这个习题的目的是编写一个程序,生成随机的扑克牌并对其进行分类,以便我们可以估计各种扑克牌的概率。如果您不玩扑克,请不要担心;我会告诉你你需要知道的一切。
一种。作为热身,编写一个程序,使用 shuffleDeck 和 subdeck 生成并打印四张随机扑克手,每手有五张牌。你有得到什么好东西吗?以下是可能的扑克牌,按价值递增顺序排列: 对:两张相同等级的牌 两对:两对相同等级的牌 三类:三张相同等级的牌 顺子:五张等级在同花顺:五张同花色 满堂:三张同花,两张同四张:四张同花顺 同花顺:五张同花顺
湾。编写一个名为 isFlush 的方法,该方法将 Deck 作为参数并返回一个布尔值,指示手牌是否包含同花。
C。编写一个名为 isThreeKind 的方法,该方法接受一只手并返回一个布尔值,指示该手是否包含同类之三。
d。编写一个生成几千手牌的循环,并检查它们是否包含同花或三手同花。估计得到其中一只手的概率。
e. 编写测试其他牌手的方法。有些比其他更容易。您可能会发现编写一些可用于多个测试的通用辅助方法很有用。
F。在一些扑克游戏中,玩家每人得到七张牌,他们与七张中最好的五张组成一手牌。修改您的程序以生成七张牌并重新计算概率。
这是我的代码:
public class Poker {
public static void main(String[] args) {
Deck deck = new Deck ();
Deck.dealDeck (deck);
}
}
class Card {
int suit, rank;
int index = 0;
//Card[] deck = new Card [52];
public Card () {
this.suit = 0; this.rank = 0;
}
public Card (int suit, int rank) {
this.suit = suit; this.rank = rank;
}
public static void printCard (Card c) {
String[] suits = { "Clubs", "Diamonds", "Hearts", "Spades" };
String[] ranks = { "narf", "Ace", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "Jack", "Queen", "King" };
System.out.println (ranks[c.rank] + " of " + suits[c.suit]);
}
}
class Deck {
Card[] cards;
public Deck (int n) {
cards = new Card[n];
}
public Deck () {
cards = new Card[52];
int index = 0;
for (int suit = 0; suit <= 3; suit++) {
for (int rank = 1; rank <= 13; rank++) {
cards[index] = new Card (suit, rank);
index++;
}
}
}
public static Deck dealDeck(Deck deck){
shuffle(deck);
Deck hand1 = subDeck(deck, 0, 4);
Deck hand2 = subDeck(deck, 5, 9);
Deck hand3 = subDeck(deck, 10, 14);
Deck hand4 = subDeck(deck, 15, 19);
Deck pack = subDeck(deck, 20, 51);
return deck;
}
public static Deck shuffle(Deck deck){
for (int i = 0; i < 52; i++){
int rand = (int)(Math.random()*(i + 1));
**Deck[] temp = deck[i];
deck[i] = deck[rand];
deck[rand] = deck[temp];**
}
return deck;
}
public static Deck subDeck(Deck deck, int low, int high) {
Deck sub = new Deck (high-low+1);
for (int i = 0; i < sub.cards.length; i++) {
sub.cards[i] = deck.cards[low+i];
}
return sub;
}
public static void printDeck (Deck hand) {
for (int i = 0; i < hand.cards.length; i++) {
Card.printCard (hand.cards[i]);
}
}
public static boolean isFlush(Card[] x, int y) {
int count = 0;
for(int index = 0; index < y; index++){
boolean comp = compareIfFlush(x[index], x[index + 1]);
if (comp = true){
count++;
}
}
if (count >= 5){
System.out.println("Congratulations! You have a flush!");
return true;
}
else{
System.out.println("Sorry, you do not have a flush.");
return false;
}
}
public static boolean compareIfFlush(Card c1, Card c2){
if (c1.suit != c2.suit){
return false;
}
return true;
}
}
我把我遇到麻烦的部分加粗。我收到错误消息:“需要数组,但找到了 exercise125poker.Deck”。我需要解决这个错误,因为我不知道如何修复它,所以我被卡住了。任何人都可以帮忙吗?