共有4个文件组成:
1、Suit.java
2、Rank.java
3、Card.java
4、CardDeal.java
Suit.java用来定义牌的套数。ENUM中的排列顺序,决定了对Card排序时的优先级。

package app.xjtu;
/**
* 定义牌的套数,共有“桃”“杏”“梅花”“方块”四套
*/
public enum Suit {
HEARTS("桃"),SPADES("杏"),CLUBS("梅花"),DIAMONDS("方块");
private String info;
Suit(String info){
this.info = info;
}
@Override
public String toString() {
return info;
}
Rank.java定义了每套牌的牌面。(这里省略了2张特殊的牌)。ENUM中的排列顺序,决定了对Card排序时的优先级。

* 定义每套牌的牌面
public enum Rank{
ACE("A"),DEUCE("2"),THREE("3"),FOUR("4"),FIVE("5"),SIX("6"),SEVEN("7"),
EIGHT("8"),NINE("9"),TEN("10"),JACK("J"),QUEEN("Q"),KING("K");
Rank(String info){
Card.java定义了一张牌。

import java.util.ArrayList;
import java.util.List;
public class Card implements Comparable<Card> {
private final Rank rank;
private final Suit suit;
private Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
private static final List<Card> protoDeck = new ArrayList<Card>();
/**
* 完成对一副新扑克的制作
*/
static {
for (Suit suit : Suit.values()) {
for (Rank rank : Rank.values()) {
protoDeck.add(new Card(rank, suit));
}
}
* @return 返回一副整齐的扑克
public static ArrayList<Card> newDeck() {
return new ArrayList<Card>(protoDeck);
* 排序:
* 1、不同级,按桃、杏、梅、方的顺序排列
* 2、同级,则按A 2 3 4 5 6 7 8 9 10 J Q K的顺序排列
public int compareTo(Card o) {
if(this.equals(o)){
return 0;
}else{
if(this.suit.equals(o.suit)){
return this.rank.compareTo(o.rank);
}else{
return this.suit.compareTo(o.suit);
public boolean equals(Object obj) {
if (obj instanceof Card) {
Card other = (Card) obj;
if (this.rank.equals(other.rank) && this.suit.equals(other.suit))
return true;
else
return false;
} else {
return false;
return this.suit+":"+this.rank;
CardDeal.java包装了对Card的基本操作:

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class CardDeal {
* 得到一副牌(可以是整齐的、也可以是洗过的)
* @param shuffle 是否洗牌。
* @return 一副牌
public static List<Card> getProtoDeck(boolean shuffle){
List<Card> deck = Card.newDeck();
if(shuffle){
Collections.shuffle(deck);
return deck;
* 对指定的牌,洗牌。
* @param deck 要洗的牌
public static void shuffle(List<Card> deck){
Collections.shuffle(deck);
* 发牌给所有人。在牌数不均时,玩家序号大的,多得1张牌。
* @param deck 所有的牌
* @param n 参与的玩家
* @return 玩家的序号和玩家的牌,所组成的Map类型。
public static Map<Integer, ArrayList<Card>> deal(List<Card> deck, int n) {
Map<Integer, ArrayList<Card>> map = new HashMap<Integer, ArrayList<Card>>();
int key = n;
int deckSize = deck.size();
// 每人至少有这么多的牌
int perHand = (deckSize - 1) / n;
do {
deckSize = deck.size();
List<Card> handView = deck.subList(deckSize - perHand, deckSize);
ArrayList<Card> hand = new ArrayList<Card>(handView);
map.put(key, hand);
handView.clear();
key--;
} while (key > 0);
// 发多余的牌
for (int i = 0; i < deck.size(); i++) {
map.get(n - i).add(deck.get(i));
return map;
* 对一系列的Card进行排序
* @param cards 要排序的Card集合
public static void sortCards(List<Card> cards){
Collections.sort(cards);
测试程序:

import java.util.Map.Entry;
public class TestCard {
public static void main(String[] args) {
// 获得一副牌
List<Card> deck = CardDeal.getProtoDeck(false);
// 要求洗牌
CardDeal.shuffle(deck);
// 发牌
Map<Integer,ArrayList<Card>> hands = CardDeal.deal(deck, 5);
// 查看每个人的牌
for(Entry<Integer,ArrayList<Card>> hand : hands.entrySet()){
// 排列得到的牌
CardDeal.sortCards(hand.getValue());
// 输出每个玩家的牌
System.out.println("玩家 "+hand.getKey()+" : (共有"+hand.getValue().size()+")的牌 :");
System.out.println(hand.getValue());
测试结果信息: