Java学习总结--037模拟斗地主洗牌、发牌、按顺序整理牌面

模拟斗地主洗牌和发牌

public class MyTest {
    public static void main(String[] args) {
        //案例演示:
        //模拟斗地主洗牌和发牌,牌没有排序
        //得有一副牌
        ArrayList pokerBox = new ArrayList<>();
        //生成54张牌放到牌盒里面
        String[] colors = {"♠", "♥", "♦", "♣"};
        String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        for (String color : colors) {
            for (String num : nums) {
                pokerBox.add(color.concat(num));
            }
        }
        //手动添加大小王
        pokerBox.add("★");
        pokerBox.add("☆");
        //洗牌
        Collections.shuffle(pokerBox);
        Collections.shuffle(pokerBox);
        Collections.shuffle(pokerBox);
        //发牌
        ArrayList 高进 = new ArrayList<>();
        ArrayList 刀仔 = new ArrayList<>();
        ArrayList 星仔 = new ArrayList<>();
        ArrayList 底牌 = new ArrayList<>();
        // 高进 = (ArrayList) pokerBox.subList(0,17);
        // 高进 0  3 6 9
        //刀仔 1 4 7 10
        //  星仔 2 5 8 11
        for (int i = 0; i < pokerBox.size(); i++) {
            if (i >= pokerBox.size() - 3) {
                底牌.add(pokerBox.get(i));
            } else if (i % 3 == 0) {
                高进.add(pokerBox.get(i));
            } else if (i % 3 == 1) {
                刀仔.add(pokerBox.get(i));
            } else {
                星仔.add(pokerBox.get(i));
            }
        }
        //看牌
        lookPoker("高进",高进);
        lookPoker("刀仔", 刀仔);
        lookPoker("星仔", 星仔);
        lookPoker("底牌", 底牌);
    }
    private static void lookPoker(String name, ArrayList list) {
        System.out.println(name);
        for (String s : list) {
            System.out.print(s+"   ");
        }
        System.out.println();
    }
}

添加洗牌发牌后,按顺序整理牌

public class MyTest {
    public static void main(String[] args) {
        //选用双列集合
        HashMap pokerBox = new HashMap<>();
        //创建一个索引集合
        ArrayList indexs = new ArrayList<>();
        int index = 0;
        String[] colors = {"♠", "♥", "♦", "♣"};
        String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        for (String num : nums) {
            for (String color : colors) {
                pokerBox.put(index, num.concat(color));
                indexs.add(index);
                index++;
            }
        }
        //手动添加大小王
        pokerBox.put(index, "★");//
        indexs.add(index);
        index++;
        pokerBox.put(index, "☆");
        indexs.add(index);
        //洗牌
        Collections.shuffle(indexs);
        //发牌
        TreeSet 高进 = new TreeSet();
        TreeSet 刀仔 = new TreeSet();
        TreeSet 星仔 = new TreeSet();
        TreeSet 底牌 = new TreeSet();
        for (int i = 0; i < indexs.size(); i++) {
            if (i >= indexs.size() - 3) {
                底牌.add(indexs.get(i));
            } else if (i % 3 == 0) {
                高进.add(indexs.get(i));
            } else if (i % 3 == 1) {
                刀仔.add(indexs.get(i));
            } else {
                星仔.add(indexs.get(i));
            }
        }
        //看牌
        lookPoker("高进", 高进, pokerBox);
        lookPoker("刀仔", 刀仔, pokerBox);
        lookPoker("星仔", 星仔, pokerBox);
        lookPoker("底牌", 底牌, pokerBox);
    }
    private static void lookPoker(String name, TreeSet set, HashMap pokerBox) {
        System.out.println(name);
        for (Integer key : set) {
            System.out.print(pokerBox.get(key) + "  ");
        }
        System.out.println();
    }
}

你可能感兴趣的:(JAVA学习总结,Java,模拟斗地主洗牌发牌看牌)