在Java中洗牌

我还有另一项练习,我真的需要帮助。 我甚至都不知道我的isFlush方法是否正常工作,因为似乎我的套牌没有洗牌并且处理一只手而且我完全卡住了。 有人可以帮助我或指出我正确的方向或什么? 这是练习:

练习12.5本练习的目标是编写一个程序,生成随机扑克牌并对其进行分类,以便我们可以估计各种扑克牌手的概率。 如果你不玩扑克,不要担心; 我会告诉你你需要知道的一切。

一个。 作为一个热身,编写一个程序,使用shuffleDeck和subdeck生成并打印四个随机扑克牌,每个牌有五张牌。 你有什么好处的吗? 以下是可能的扑克牌,按价值递增顺序:对:两张相同等级的牌两对:两对牌同等级三张牌:三张牌同等级直牌:五张牌等牌序列冲洗:五张牌同一套牌全场:三张牌一张,两张牌另有四张牌:四张同等级牌同花顺:五张牌顺序和同一套牌

湾 编写一个名为isFlush的方法,该方法将Deck作为参数并返回一个布尔值,指示该手是否包含刷新。

C。 编写一个名为isThreeKind的方法,它接受一只手并返回一个布尔值,指示该手是否包含三种类型。

d。 写一个循环,生成几千手,并检查它们是否包含齐声或三种类型。 估计获得其中一手的可能性。

即 编写测试其他扑克手的方法。 有些比其他人容易。 您可能会发现编写一些可用于多个测试的通用辅助方法很有用。

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 = 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”。 我需要解决这个错误,因为我不知道如何修复它所以我被卡住了。 有人可以帮忙吗?

您的Deck类型不是数组类型。 它有一个数组,但你不能像它一样使用它 – 这就是产生错误的原因。 考虑一下……如果你的Deck有两张Card[52]怎么办? 那么如果你打电话给deck[25]那就没有任何意义了。 通常,当您在对象中隐藏数组时,您需要使用诸如public Card get(int i)类的访问器方法来公开数组,以从内部数组中检索元素。

为了解决您的问题,请将您突出显示的代码行更改为:

 Card temp = deck.cards[i]; deck.cards[i] = deck.cards[rand]; deck.cards[rand] = temp; 

代码有2个问题。

  • 第一个是Deck不是一个数组,你想访问其中的卡片arrays(因此将.cards添加到数组中)。
  • 另外Deck []期望得到一系列套牌,而不是你试图给它的Card

希望这可以帮助,

背风处