枚举扑克牌

枚举类牌等级Rank 花色Suit
Rank.java

public enum Rank{
ACE(“A”,1),TWO(“2”,2),THREE(“3”,3),FOUR(“4”,4),FIVE(“5”,5),
SIX(“6”,6),SEVEN(“7”,7),EIGHT(“8”,8),NINE(“9”,9),TEN(“10”,10),
JACK(“J”,11),QUEEN(“Q”,12),KING(“K”,13);
private String name;
private int r;
Rank(String name, int r){
this.name=name;
this.r=r;
}

public int getR() {
    return r;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Suit.java

public enum Suit{
SPADE(1),HEART(2),CLUB(3),DIAMOND(4);
private int index;

public int getIndex() {
    return index;
}

Suit(int index){
    this.index=index;
}

}
Card.java

public class Card {
private Rank rank;
private Suit suit;
public Card(Rank rank,Suit suit){
this.rank=rank;
this.suit=suit;
}

public Rank getRank() {
    return rank;
}

public Suit getSuit() {
    return suit;
}


@Override
public String toString() {
    return suit.name()+" "+rank.getR();
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
五张扑克牌组成的集合
CardSet.java

import java.util.Arrays;
import java.util.Comparator;

public class CardSet implements Comparable {
private Card[] cards = new Card[5];

public CardSet(Card[] cards) {
    this.cards = cards;
    Arrays.sort(cards, new cmp());//升序排序
}

public int getHigh() {
    int max = 0;
    for (Card c : cards) {
        if (c.getRank().getR() > max)
            max = c.getRank().getR();
        if (c.getRank().getR() == 1)
            max = 14;
    }
    return max;
}


public int getPair() {
    int res = 0;
    for(int i=0;i<5;i++){
        for(int j=i+1;j<5;j++){
            if(cards[i].getRank()==cards[j].getRank())
                res++;
        }
    }
    return res;
}


public boolean isThree() {
    return (getPair()==3);
}


public int isStraight() {
    int a = cards[4].getRank().getR() - cards[3].getRank().getR();
    int b = cards[3].getRank().getR() - cards[2].getRank().getR();
    int c = cards[2].getRank().getR() - cards[1].getRank().getR();
    int d = cards[1].getRank().getR() - cards[0].getRank().getR();
    if (a == b && b == c && c == d) {
        return 1;
    } else if (a == b && b == c && d == 9) {
        return 2;
    }
    return 0;
}


public boolean isFlush() {
    for (int i = 0; i < 4; i++) {
        if (cards[i].getSuit() != cards[i + 1].getSuit())
            return false;
    }
    return true;
}


public boolean isFullHouse() {
    return (getPair() == 4);
}


public boolean isFour() {
    return (getPair()==6);
}

public boolean isStraightFlush() {
    return (isStraight() == 1) && isFlush();
}

public boolean isRoyal() {
    return (isStraight() == 2) && isFlush();
}
//按牌型给出points 若无特殊牌型则给出最大一张牌的rank
public int getPoints() {
    if (isRoyal())
        return 100;
    else if (isStraightFlush())
        return 90;
    else if (isFour())
        return 80;
    else if (isFullHouse())
        return 70;
    else if (isFlush())
        return 60;
    else if (isStraight() != 0)
        return 50;
    else if (isThree())
        return 40;
    else if (getPair() == 2)
        return 30;
    else if (getPair() == 1)
        return 20;
    else
        return getHigh();

}

@Override
public int compareTo(Object o) {
    if (this.getPoints() != ((CardSet) o).getPoints()) {
        if (this.getPoints() > ((CardSet) o).getPoints())
            return 1;
        else
            return -1;
    } else
        return 0;
}

//排序比较器
class cmp implements Comparator {
    @Override
    public int compare(Object o1, Object o2) {
        Card c1 = (Card) o1;
        Card c2 = (Card) o2;
        if (c1.getRank().getR() != c2.getRank().getR())
            return c1.getRank().getR() > c2.getRank().getR() ? 1 : -1;
        else
            return 0;
    }
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值