import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Test {
public static void main(String[] args) {
// 创建一幅牌
DeckOfCards cards = new DeckOfCards();
// 未洗牌前,先看看牌是什么样子的
cards.deal();
// 洗牌
cards.shuffle();
// 洗牌后,先看看牌是什么样子的
cards.deal();
}}class Card {
// 这里用整型来代表花色与点数,point(0-12代表点数A-K),suit(0-3代表花色),表示再说
private int point, suit;
public Card(int point, int suit) {
this.point = point;
this.suit = suit;
}
// 点数对应的英文
public static final String[] POINTS = { "ace", "deuce", "three", "four",
"five", "six", "severn", "eight", "nine", "ten", "jack", "queen",
"king" };
// 花色对应的英文
public static final String[] SUITS = { "heart", "spades", "diamonds",
"club" }; // 0123分别对应红黑方梅
// 打印出这张扑克,包括花色与点数
public String toString() {
return SUITS[suit] + " " + POINTS[point];
}}// 一幅牌class DeckOfCards {
List cards = new ArrayList();
public DeckOfCards() {
// 创建52张牌
for (int suit = 0; suit < 4; suit++) {// 花色
for (int point = 0; point < 13; point++) {
cards.add(new Card(point, suit));
}
}
}
/**
* 洗牌
*/
public void shuffle() {
Collections.shuffle(cards);
}
/**
* 发牌,其实就是显示
*/
public void deal() {
for (Card card : cards) {
System.out.println(card);
}
}}
|