Java基础之集合框架——使用堆栈Stack<>对象模拟发牌(TryDeal)

控制台程序。

1 public enum Rank {
2   TWO,   THREE, FOUR, FIVE, SIX,   SEVEN,
3   EIGHT, NINE,  TEN,  JACK, QUEEN, KING, ACE
4 }
1 public enum Suit {
2   CLUBS, DIAMONDS, HEARTS, SPADES
3 }
 1 public class Card implements Comparable<Card> {
 2     public Card(Rank rank, Suit suit) {
 3     this.rank = rank;
 4     this.suit = suit;
 5   }
 6 
 7   @Override
 8   public String toString() {
 9     return rank + " of " + suit;
10   }
11 
12   // Compare two cards
13   public int compareTo(Card card) {
14     if(suit.equals(card.suit)) {                                       // First compare suits
15       if(rank.equals(card.rank)) {                                     // So check face values
16         return 0;                                                      // They are equal
17       }
18       return rank.compareTo(card.rank) < 0 ? -1 : 1;
19     } else {                                                           // Suits are different
20       return suit.compareTo(card.suit) < 0 ? -1 : 1;                   // Sequence is C<D<H<S
21     }
22   }
23 
24   private Suit suit;
25   private Rank rank;
26 }
 1 // Class defining a hand of cards
 2 import java.util.Vector;
 3 import java.util.Collections;
 4 
 5 public class Hand {
 6   // Add a card to the hand
 7   public void add(Card card) {
 8     hand.add(card);
 9   }
10 
11   @Override
12   public String toString() {
13     StringBuilder str = new StringBuilder();
14     boolean first = true;
15     for(Card card : hand) {
16       if(first) {
17         first = false;
18       } else {
19         str.append(", ");
20       }
21       str.append(card);
22     }
23     return str.toString();
24   }
25 
26   // Sort the hand
27   public Hand sort() {
28     Collections.sort(hand);
29     return this;
30   }
31 
32   private Vector<Card> hand = new Vector<>();      // Stores a hand of cards
33 }
 1 import java.util.Stack;
 2 import java.util.Collections;
 3 
 4 public class CardDeck {
 5   // Create a deck of 52 cards
 6   public CardDeck() {
 7     for(Suit suit : Suit.values())
 8       for(Rank rank : Rank.values())
 9         deck.push(new Card(rank, suit));
10   }
11 
12   // Deal a hand
13   public Hand dealHand(int numCards) {
14     if(deck.size() < numCards) {
15       System.err.println("Not enough cards left in the deck!");
16       System.exit(1);
17     }
18 
19     Hand hand = new Hand();
20     for(int i = 0; i < numCards; ++i) {
21       hand.add(deck.pop());
22     }
23     return hand;
24   }
25 
26   // Shuffle the deck
27   public void shuffle() {
28     Collections.shuffle(deck);
29   }
30 
31   private Stack<Card> deck = new Stack<>();
32 }

洗牌使用Collections类中一个静态的参数化方法shuffle()方法,该方法会打乱实现了List<>接口的任何集合中的内容。

 1 class TryDeal {
 2   public static void main(String[] args) {
 3     CardDeck deck = new CardDeck();
 4     deck.shuffle();
 5 
 6     Hand myHand = deck.dealHand(5).sort();
 7     Hand yourHand = deck.dealHand(5).sort();
 8     System.out.println("
My hand is:
" + myHand);
 9     System.out.println("
Your hand is:
" + yourHand);
10   }
11 }
原文地址:https://www.cnblogs.com/mannixiang/p/3430379.html