共有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());
測試結果資訊: