在 Java 中对扑克手牌进行分类

我目前正在研究一个对玩家的手进行分类的 CS 项目。我解决了项目的前半部分,打印出牌组、洗牌牌组以及玩家 1、玩家 2 和剩余牌组的手牌。当我必须评估手牌时,问题就出现了。我的代码必须以某种方式评估手牌的分类,并打印出玩家 1 或玩家 2 是否获胜。到目前为止我已经上了三门课:


public class Card {


    static String[] card_suit = {"hearts", "diamonds", "clubs", "spades"};


    static int[] card_rank = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};// 11 is Jack, 12 is Queen, 13 is King and 14 is Ace 


    public int[] getRank() {

        return card_rank;

    }

    public String[] getSuit() {

        return card_suit;

    }

}

public class Driver {


    public static void main(String[] args) {

        Card card = new Card();

        Deck deck = new Deck();


        deck.getDeck();

        System.out.print("ORIGINAL DECK: ");

        deck.printDeck();

        deck.shuffleDeck();

        System.out.print("SHUFFLED DECK: ");

        deck.printDeck();


        System.out.println();



        System.out.print("PLAYER ONE: ");

        System.out.println(java.util.Arrays.toString(deck.playerOneHands()));

        System.out.print("PLAYER TWO: ");

        System.out.println(java.util.Arrays.toString(deck.playerTwoHands()));

        System.out.print("REMAINING DECK: ");

        System.out.println(java.util.Arrays.toString(deck.remainingDeckCards()));               

        }


}

我认为它会起作用是因为 Deck 类扩展自 Card 类,我可以使用 getRank 方法来比较每手牌,但我不确定如何构造条件。

任何帮助是极大的赞赏。谢谢。


鸿蒙传说
浏览 108回答 3
3回答

慕村225694

不想为你做作业...这是个问题:class&nbsp;Deck&nbsp;extends&nbsp;Card牌组不是卡牌的子类型。一副牌有牌,所以:class&nbsp;Deck&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;List<Card>&nbsp;cards; }是一个更好的选择。另外,以下代码对牌组没有任何作用:public&nbsp;void&nbsp;shuffleDeck()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;Collections.shuffle(Arrays.asList(deck_card)); }它会洗牌牌组的副本,使牌组保持不变。另外,您不应该在循环中构建字符串。相反,在 Card 和 Deck 上实现(覆盖) toString() 方法。另外,制作西装enum。另外,card_rank完全删除 - 它没有任何意义。相反,int rank;向 Card 添加一个字段,或者更好地使排名成为enum。首先修复这些问题,然后通过编写一个方法来重新解决问题,该方法传递一个具有 List 的 Hand (一个新类),以及一个通过评估该手牌是否是同花而返回 HandType (另一个枚举)的方法,否则有四种,否则...一直到高牌 - 从最高到最低。

大话西游666

为了对游戏进行建模,首先要识别Card、Deck等实体(大多数情况下您已经完成了)。我会添加更多内容,例如Player、Evaluator(如下所述)等。排名和花色不是字符串/整数,但它们是预定义的(在游戏的生命周期中不会改变)卡牌中可能的变化。始终使用领域词汇来获得最佳模型。每张卡牌属于一种花色和一种等级。(将 Rank 和 Suit 视为枚举,这将避免未知值在运行时破坏代码。不给出 Card 中的套件和等级的设置方法是必要的(它们组合形成 Card 的标识)全牌组(初始化时)由花色和等级的叉积形成。意思是牌组有(包含)多张牌。请记住,卡牌在牌组之外(在玩家手中时)也可以是活的,因此它不是组合。从 Card 继承 Deck 是绝对错误的。它翻译为陈述Deck 是一种 Card,这是不正确的。牌组将收集卡片。使用继承,会导致违反里氏替换原则(SOLID之一)。为了对 Deck 进行建模,请考虑以下事实:Deck 不包含重复的牌,Deck 一旦形成就不会改变其顺序(除非洗牌)。这是 Set 和 List 之间的棘手选择,我会选择带有添加编程约束的 List 以避免重复(仅在初始化时才需要完成)。但是,最好不要将 Modeling Deck 作为 java 集合,而是让类 Deck 包含合适选择的 java 集合,并且通过定义所需的 API(从领域角度来看)如 shuffle、getTopCard() 等,Deck 类作为包装器工作。这称为对象适配器设计模式。这使得我们的设计平台(实现)独立。您需要对更多的类进行建模,例如Player持有CardInHand等。关于评估手中的卡片,最好将其建模为单独的类,因为其不同的关注点和规则可以独立于其他类而改变。扑克游戏是学习面向对象编程的最佳作业。

holdtom

看来你的类Card只有静态字段;我会更改它,以便 的实例Card代表 中的一张卡Deck。我也会把套房做成一种enum类型。您还可以为数字和 A 添加整数常量。该类可以实现Comparable<Card>:public class Card implements Comparable<Card> {&nbsp; &nbsp; public enum Suite {CLUBS, DIAMONDS, HEARTS, SPADES};&nbsp; &nbsp; public static final int JACK = 11;&nbsp; &nbsp; public static final int QUEEN = 12;&nbsp; &nbsp; public static final int KING = 13;&nbsp; &nbsp; public static final int ACE = 14;&nbsp; &nbsp; public final Suite suite;&nbsp; &nbsp; public final int rank;&nbsp; &nbsp; public Card(Suite suite, int rank) {&nbsp; &nbsp; &nbsp; &nbsp; if (suite == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Suite cannot be null");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (rank < 2 || rank > 14) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Value must be between 2 and 14");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; this.suite = suite;&nbsp; &nbsp; &nbsp; &nbsp; this.rank = rank;&nbsp; &nbsp; }&nbsp; &nbsp; public Suite getSuite() {&nbsp; &nbsp; &nbsp; &nbsp; return suite;&nbsp; &nbsp; }&nbsp; &nbsp; public int getRank() {&nbsp; &nbsp; &nbsp; &nbsp; return rank;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder buf = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; if (rank >= 2 && rank <= 10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.append(rank);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (rank) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case JACK:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.append("jack");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case QUEEN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.append("queen");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case KING:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.append("king");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ACE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.append("ace");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; buf.append(" of ");&nbsp; &nbsp; &nbsp; &nbsp; buf.append(suite.toString().toLowerCase());&nbsp; &nbsp; &nbsp; &nbsp; return buf.toString();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compareTo(Card other) {&nbsp; &nbsp; &nbsp; &nbsp; if (rank > other.rank) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; } else if (rank < other.rank) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return suite.compareTo(other.suite);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}请注意,您还可以有 的两个子类Card:一个用于数字,一个用于数字。这Deck是 52 张卡片的集合。它是通过将每张卡添加到列表中来初始化的。一个人可以使用shuffle一副牌或take牌组中的一张牌:public class Deck {&nbsp; &nbsp; private final List<Card> cards = new ArrayList<>();&nbsp; &nbsp; public Deck() {&nbsp; &nbsp; &nbsp; &nbsp; for (Card.Suite suite: Card.Suite.values()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 2; i <= 14; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards.add(new Card(suite,i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void shuffle() {&nbsp; &nbsp; &nbsp; &nbsp; Collections.shuffle(cards);&nbsp; &nbsp; }&nbsp; &nbsp; public boolean isEmpty() {&nbsp; &nbsp; &nbsp; &nbsp; return cards.isEmpty();&nbsp; &nbsp; }&nbsp; &nbsp; public Card take() {&nbsp; &nbsp; &nbsp; &nbsp; if (cards.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalStateException("Deck is empty");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return cards.remove(0);&nbsp; &nbsp; }}您可以洗牌并从牌库中取出 5 张牌,如下所示:&nbsp; &nbsp; Deck deck = new Deck();&nbsp; &nbsp; deck.shuffle();&nbsp; &nbsp; for (int i = 0; i < 5; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; Card card = deck.take();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(card);&nbsp; &nbsp; }现在 aHand是从 a 中取出的一组五张牌Deck。我们可以声明Hand为实现Comparable<Hand>,这样我们就可以知道两只手中哪一只的价值最高:public class Hand implements Comparable<Hand> {&nbsp; &nbsp; private final Card[] cards = new Card[5];&nbsp; &nbsp; public Hand(Deck deck) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 5; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = deck.take();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(cards);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compareTo(Hand other) {&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }}现在有趣的部分来了:您必须将手牌类型识别为以下类型之一(枚举类型):public enum HandType {&nbsp; &nbsp; SINGLE, PAIR, TWO_PAIRS, THREE, STRAIGHT, FLUSH, FULL_HOUSE, FOUR,&nbsp; &nbsp; STRAIGHT_FLUSH, ROYAL_FLUSH;}请注意,常数是从最低到最高排列的。此外,牌的排列方式必须能够在出现平局的情况下,您可以比较牌来确定获胜者。我建议你将相同等级的卡牌分组;如果您有五个不同的组,它仍然可以是同花或顺子。另一种方法是声明Handfor every 的子类HandType,但我认为这样做不会获得太多好处。public class Hand implements Comparable<Hand> {&nbsp; &nbsp; public enum HandType {&nbsp; &nbsp; &nbsp; &nbsp; SINGLE, PAIR, TWO_PAIRS, THREE, STRAIGHT, FLUSH, FULL_HOUSE, FOUR,&nbsp; &nbsp; &nbsp; &nbsp; STRAIGHT_FLUSH, ROYAL_FLUSH;&nbsp; &nbsp; }&nbsp; &nbsp; private final Card[] cards = new Card[5];&nbsp; &nbsp; private final int[] groupSize;&nbsp; &nbsp; private final HandType type;&nbsp; &nbsp; public Hand(Deck deck) {&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 5; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i] = deck.take();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; groupSize = group(cards);&nbsp; &nbsp; &nbsp; &nbsp; type = identifyType(groupSize, cards);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compareTo(Hand other) {&nbsp; &nbsp; &nbsp; &nbsp; int r = type.compareTo(other.type);&nbsp; &nbsp; &nbsp; &nbsp; if (r != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return r;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i = cards.length; --i >= 0; ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int r1 = cards[i].getRank();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int r2 = other.cards[i].getRank();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (r1 < r2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (r1 > r2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder buf = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; buf.append(type);&nbsp; &nbsp; &nbsp; &nbsp; buf.append(": ");&nbsp; &nbsp; &nbsp; &nbsp; buf.append(cards[0]);&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < 5; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.append(", ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.append(cards[i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return buf.toString();&nbsp; &nbsp; }&nbsp; &nbsp; private static int[] group(Card[] cards) {&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(cards);&nbsp; &nbsp; &nbsp; &nbsp; List<List<Card>> groups = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; int val = -1; // invalid rank&nbsp; &nbsp; &nbsp; &nbsp; List<Card> currentGroup = null;&nbsp; &nbsp; &nbsp; &nbsp; for (Card card: cards) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (val == card.getRank()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentGroup.add(card);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (currentGroup != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; groups.add(currentGroup);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentGroup = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentGroup.add(card);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val = card.getRank();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (currentGroup != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; groups.add(currentGroup);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // identify groups of cards of same value&nbsp; &nbsp; &nbsp; &nbsp; // sort groups by size and highest card&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(groups, (List<Card> group1, List<Card> group2) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int s1 = group1.size();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int s2 = group2.size();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (s1 < s2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (s1 > s2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return group1.get(s1-1).compareTo(group2.get(s2-1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; int[] groupSize = new int[groups.size()];&nbsp; &nbsp; &nbsp; &nbsp; int g = 0;&nbsp; &nbsp; &nbsp; &nbsp; int i = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (List<Card> group: groups) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; groupSize[g++] = group.size();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Card card: group) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards[i++] = card;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; assert sum(groupSize) == 5;&nbsp; &nbsp; &nbsp; &nbsp; return groupSize;&nbsp; &nbsp; }&nbsp; &nbsp; private static HandType identifyType(int[] groupSize, Card[] cards) {&nbsp; &nbsp; &nbsp; &nbsp; switch (groupSize.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // can be a full house or four cards&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (groupSize[0] == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HandType.FOUR;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (groupSize[0] == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HandType.FULL_HOUSE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assert false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (groupSize[0] == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // three cards or double pair&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (groupSize[1] == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HandType.THREE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assert groupSize[1] == 2 && groupSize[2] == 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HandType.TWO_PAIRS;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assert false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 4:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // one pair&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HandType.PAIR;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 5:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // all different values: check for flush&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Card prev = cards[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean sameSuite = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean straight = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i < 5; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Card card = cards[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; straight &= card.getRank() == prev.getRank()+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sameSuite &= card.getSuite() == prev.getSuite();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sameSuite) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (straight) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (cards[4].getRank() == Card.ACE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HandType.ROYAL_FLUSH;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HandType.STRAIGHT_FLUSH;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HandType.FLUSH;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (straight) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HandType.STRAIGHT;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return HandType.SINGLE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; assert false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static int sum(int[] groupSize) {&nbsp; &nbsp; &nbsp; &nbsp; int sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int s: groupSize) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += s;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return sum;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java