編碼也快樂!撲克牌排序JAVA

Don_Shawn發表於2012-11-29

一個Card類,一個PrintCards類負責輸出,首先初始化52張牌,然後洗牌,最後對每一張牌判斷花色,加入相應的ArrayList中,對ArrayList排序,再輸出即可,程式碼如下: Update:程式碼部分更新,已自己實現排序

import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.Iterator; import java.util.Random;

public class PrintCards {

public static void Print() {
    ArrayList<Card> Cards = new ArrayList<Card>(13 * 4);
    init(Cards);
    for (int i = 0; i < 5; i++) {
        shuffle(Cards);
    }
    System.out.println("before");
    for (Card card : Cards) {
        System.out.print(card.toString());
    }
    System.out.println("");
    ArrayList<Card> heitao = new ArrayList<Card>(13);
    ArrayList<Card> hongtao = new ArrayList<Card>(13);
    ArrayList<Card> meihua = new ArrayList<Card>(13);
    ArrayList<Card> fangpian = new ArrayList<Card>(13);
    for (Card card : Cards) {
        if (card.color == 3) {
            heitao.add(card);
        }
        if (card.color == 2) {
            hongtao.add(card);
        }
        if (card.color == 1) {
            meihua.add(card);
        }
        if (card.color == 0) {
            fangpian.add(card);
        }
    }

    System.out.println("after:");
    for (Card card : sort(heitao)) {
        System.out.print(card);
    }
    for (Card card : sort(hongtao)) {
        System.out.print(card);
    }
    for (Card card : sort(meihua)) {
        System.out.print(card);
    }
    for (Card card : sort(fangpian)) {
        System.out.print(card);
    }
}

private static ArrayList<Card> sort(ArrayList<Card> cardArrayList) {
    ArrayList<Card> temp = new ArrayList<Card>(13);
    for (int i = 0; i < 13; i++) {
        temp.add(new Card());
    }
    for (Card card : cardArrayList) {
        temp.set(card.number, card);
    }
    return temp;
}

public static ArrayList<Card> init(ArrayList<Card> Cards) {
    for (int i = 0; i < 52; i++) {
        Card card = new Card();
        card.number = i % 13;
        card.color = i / 13;
        Cards.add(card);
    }

    return Cards;
}

public static ArrayList<Card> shuffle(ArrayList<Card> Cards) {
    Date date = new Date();
    Random random = new Random(date.getTime());
    for (int i = 0; i < 52; i++) {
        swap(Cards.get(i), Cards.get(random.nextInt(42)));
    }
    return Cards;
}

private static void swap(Card card, Card card2) {
    int tempnumber = card.number;
    card.number = card2.number;
    card2.number = tempnumber;
    int tempcolor = card.color;
    card.color = card2.color;
    card2.color = tempcolor;
}

public static void main(String[] args) {
    Print();
}

}

Card.java類

public class Card{ public int number; public int color;

@Override
public String toString() {
    String ColorResult = null;
    String NumberResult;
    switch (color) {
    case 3:
        ColorResult = "黑桃";
        break;
    case 2:
        ColorResult = "紅桃 ";
        break;
    case 1:
        ColorResult = "梅花";
        break;
    case 0:
        ColorResult = "方片";
        break;
    }
    switch (number) {
    case 9:
        NumberResult = "J";
        break;
    case 10:
        NumberResult = "Q";
        break;
    case 11:
        NumberResult = "K";
        break;
    case 12:
        NumberResult = "A";
        break;

    default:
        NumberResult = "" + (number + 2);
        break;
    }
    return ColorResult + ":" + NumberResult + " ";
}

} 結果: 結果圖

相關文章