JAVASE-22:Collections工具类,斗地主洗牌发牌、整理牌

Collections工具类

针对集合操作 的工具类
Collections成员方法
public static < T > void sort (List < T > list):排序, 默认按照自然顺序
public static < T > int binarySearch (List < ? > list, T key):二分查找
public static < T > T max(Collection < ? > coll):获取最大值
public static void reverse (List < ? > list):反转
public static void shuffle (List < ? > list):随机置换

public static void main(String[] args) {
    ArrayList<Integer> list = new ArrayList<>();
    list.add(20);
    list.add(24);
    list.add(10);
    list.add(202);
    list.add(205);
    list.add(209);
    // 排序
    Collections.sort(list, new Comparator<Integer>() {
        @Override
        public int compare(Integer a, Integer b) {
            return a-b;
        }
    });
    System.out.println(list);//[10, 20, 24, 202, 205, 209]

    int i = Collections.binarySearch(list, 20);
    System.out.println(i);//1
    Integer max = Collections.max(list);//获取最大值209
    Integer min = Collections.min(list);//获取最小值10
    System.out.println(max);
    System.out.println(min);

    Collections.reverse(list);//倒序
    System.out.println(list);//[209, 205, 202, 24, 20, 10]
    Collections.shuffle(list);//随机置换
    System.out.println(list);//[24, 20, 209, 10, 202, 205]
}

练习:斗地主洗牌发牌

先创建牌盒用来存牌—ArrayList< String >,生成牌可以用两个数组循环拼接,再放入“牌盒”;

洗牌Collections用工具类.shuffle()随机置换;

最后循环发牌,一人一张发(也可以一人一叠17张),剩余三张,作为底牌。

public class MyTest {
    public static void main(String[] args) {
        // 案例演示:模拟斗地主洗牌和发牌,牌没有排序
        // 1.创建一个集合,来充当这个牌盒子
        ArrayList<String> pokerBox = new ArrayList<>();
        // 2.生成 牌、花色 和 数字
        String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        String[] colors = {"♣", "♥", "♦", "♠"};
        for (String color : colors) {
            for (String num : nums) {
                String pai = color.concat(num);//拼接字符串
                pokerBox.add(pai);
            }
        }
        // 手动添加大小王
        pokerBox.add("☀");
        pokerBox.add("⭐");
        // 洗牌:
        Collections.shuffle(pokerBox);
        Collections.shuffle(pokerBox);
        Collections.shuffle(pokerBox);

        // 发牌:
        /*
        List<String> aList = pokerBox.subList(0, 17);
        List<String> bList = pokerBox.subList(17, 34);
        List<String> cList = pokerBox.subList(34, 51);*/

        // 一人一张来发
        // 定义三个人的集合,在有一个底牌集合
        ArrayList<String> 星仔 = new ArrayList<>();
        ArrayList<String> 刀仔= new ArrayList<>();
        ArrayList<String> 高进 = new ArrayList<>();
        ArrayList<String> 底牌 = new ArrayList<>();
        // 星仔 0 3 6  9
        // 刀仔 1 4 7  10
        // 高进 2 5 8  11
        // 对3取余
        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<String> list) {
        System.out.println(name);
        for (String s : list) {
            System.out.print(s+"\t");
        }
        System.out.println();
    }
}

作业:斗地主排序

牌盒可以用HashMap来做,但是循环存入后是有序的,洗牌阶段可以创建一个ArrayList,打乱它的顺序,在发牌时就可以利用里面的乱序随机的发牌,但是要求最后玩家手里的牌有序,可以用TreeSet创建有序的集合表示玩家得到的牌,最后打印出来就是已经整理好的牌了。

public class HomeWork {
    public static void main(String[] args) {

        HashMap<Integer, String> Pai = new HashMap<>();
        String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        String[] colors = {"♣", "♥", "♦", "♠"};
        int i=0;
        for (String color : colors) {
            for (String num : nums) {
                String pai = num.concat(color);//拼接字符串
                Pai.put(i,pai);
                i++;
            }
        }
        // 手动添加大小王
        Pai.put(52,"☀");
        Pai.put(53,"⭐");
        System.out.println(Pai);

        // 洗牌
        ArrayList<Integer> integers = new ArrayList<>();
        for (int j = 0; j < 54; j++) {
            integers.add(j);
        }
        
        Collections.shuffle(integers);
        Collections.shuffle(integers);
        Collections.shuffle(integers);
        System.out.println(integers);
        
        // 发牌:给三个人,再留三张底牌
        TreeSet<String> PlayerOne = new TreeSet<>();
        TreeSet<String> PlayerTwo = new TreeSet<>();
        TreeSet<String> PlayerThree = new TreeSet<>();
        TreeSet<String> DiPai = new TreeSet<>();
        for (int k = 0; k < Pai.size(); k++) {
            if(k>=Pai.size()-3){
                DiPai.add(Pai.get(integers.get(k)));
            }else if(k%3==0){
                PlayerOne.add(Pai.get(integers.get(k)));
            }else if(k%3==1){
                PlayerTwo.add(Pai.get(integers.get(k)));
            }else {
                PlayerThree.add(Pai.get(integers.get(k)));
            }
        }
        
        // 看牌
        LookPai("PlayerOne",PlayerOne);
        LookPai("PlayerTwo",PlayerTwo);
        LookPai("PlayerThree",PlayerThree);
        LookPai("底牌",DiPai);
    }

    private static void LookPai(String name,TreeSet<String> set) {
        System.out.println(name);
        for (String s : set) {
            System.out.print(s + "\t");
        }
        System.out.println();
    }
}
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页