【代碼練習3】撲克洗牌發牌升級版

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;

/*
 * 思路:
 *      A:創建一個HashMap集合
 *      B:創建一個ArrayList集合
 *      C:創建花色數組和點數數組
 *      D:從0開始往HashMap裏面存儲編號,並存儲對應的牌
 *        同時往ArrayList裏面存儲編號即可。
 *      E:洗牌(洗的是編號)
 *      F:發牌(發的也是編號,爲了保證編號是排序的,就創建TreeSet集合接收)
 *      G:看牌(遍歷TreeSet集合,獲取編號,到HashMap集合找對應的牌)
 */
public class PokerDemo {
    public static void main(String[] args) {
        // 創建一個HashMap集合
        HashMap<Integer, String> hm = new HashMap<Integer, String>();

        // 創建一個ArrayList集合
        ArrayList<Integer> array = new ArrayList<Integer>();

        // 創建花色數組和點數數組
        // 定義一個花色數組
        String[] colors = { "♠", "♥", "♣", "♦" };
        // 定義一個點數數組
        String[] numbers = { "3", "4", "5", "6", "7","8","9", "10", "J", "Q","K", "A", "2", };

        // 從0開始往HashMap裏面存儲編號,並存儲對應的牌,同時往ArrayList裏面存儲編號即可。
        int index = 0;

        for (String number : numbers) {
            for (String color : colors) {
                String poker = color.concat(number);
                hm.put(index, poker);
                array.add(index);
                index++;
            }
        }
        hm.put(index, "小王");
        array.add(index);
        index++;
        hm.put(index, "大王");
        array.add(index);

        // 洗牌(洗的是編號)
        Collections.shuffle(array);

        // 發牌(發的也是編號,爲了保證編號是排序的,就創建TreeSet集合接收)
        TreeSet<Integer> fengQingYang = new TreeSet<Integer>();
        TreeSet<Integer> linQingXia = new TreeSet<Integer>();
        TreeSet<Integer> liuYi = new TreeSet<Integer>();
        TreeSet<Integer> diPai = new TreeSet<Integer>();

        for (int x = 0; x < array.size(); x++) {
            if (x >= array.size() - 3) {
                diPai.add(array.get(x));
            } else if (x % 3 == 0) {
                fengQingYang.add(array.get(x));
            } else if (x % 3 == 1) {
                linQingXia.add(array.get(x));
            } else if (x % 3 == 2) {
                liuYi.add(array.get(x));
            }
        }

        // 看牌(遍歷TreeSet集合,獲取編號,到HashMap集合找對應的牌)
        lookPoker("風清揚", fengQingYang, hm);
        lookPoker("林青霞", linQingXia, hm);
        lookPoker("劉意", liuYi, hm);
        lookPoker("底牌", diPai, hm);
    }

    // 寫看牌的功能
    public static void lookPoker(String name, TreeSet<Integer> ts,
            HashMap<Integer, String> hm) {
        System.out.print(name + "的牌是:");
        for (Integer key : ts) {
            String value = hm.get(key);
            System.out.print(value + " ");
        }
        System.out.println();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章