用Map集合实现斗地主发牌洗牌的动作 - - 有序版本

知识点:

HashMap< Key,Value>集合:键唯一,值可以重复!
Map集合的添加方法:map.put(key,value);
Map集合的获取方法:map.get(key);通过键找到值.
Collections.sort(list) ; 默认进行升序排序.

/*
斗地主综合案例_有序版本
    实现步骤:
        1.准备牌(Map<key,Value>集合)
        2.洗牌(shuffer方法)
        3.发牌()
        4.排序(sort()方法)
        5.看牌(遍历 - 键值对)
    前4步都是通过牌的索引来操作 . 
    看牌 -- 将索引对应的扑克牌拿出来 .
 */
public class DouDiZhu01 {
    public static void main(String[] args) {
        getPoker();
    }
    public static void getPoker(){
        // 1.准备牌 , 创建一个Map集合 , key为牌的索引 , value为拼装好的牌 .
        HashMap<Integer , String> poker = new HashMap<>();
        // 创建一个集合 , 用来装牌的索引 .
        ArrayList<Integer> pokerIndex = new ArrayList<>();

        // 先装大王 , 小王 .
        int index = 0;
        pokerIndex.add(index);
        poker.put(index ++, "大王"); // 0 - 大王 , 完了index+1;
        pokerIndex.add(index);
        poker.put(index ++, "小王"); // 1 - 小王 , 完了index+1;

        // 创建两个数组 , 一个存放花色 , 一个存放点数 .
        String[] colors = {"♠","♥","♣","♦"};
        String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};

        // 将其余的52张牌拼接好 , 放到集合中.
        for (String number : numbers) {  // 使用增强for循环 .
            for (String color : colors) { //嵌套for,拼装牌
                pokerIndex.add(index);
                poker.put(index++ , color+number); // 放入索引 , 跟组装好的牌 。index = 2 ...
            }
        }

        // 2. 洗牌 -- 打乱顺序 .
        Collections.shuffle(pokerIndex);
        // System.out.println(pokerIndex);

        // 3. 发牌 -- 创建四个集合分别装三个玩家和底牌 .
        ArrayList<Integer> person01 = new ArrayList<>();
        ArrayList<Integer> person02 = new ArrayList<>();
        ArrayList<Integer> person03 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();

        // 按照顺序依次发牌 , 最后留三张作为底牌 .
        for (int i = 0; i < pokerIndex.size(); i++) {
            Integer it = pokerIndex.get(i);  // 获取牌的索引值.
            if(i >= 51){     // 最后三张 , 放入底牌 .
                dipai.add(it);
            }else if(i%3 == 0){ // 第一张给了玩家1 .
                person01.add(it);
            }else if(i%3 == 1){ // 第二张给了玩家2 .
                person02.add(it);
            }else if(i%3 == 2){ // 第三张给了玩家3 .
                person03.add(it);
            }
        }

        // 4. 排序 -- 升序排序 , 就是将大牌在前,小牌在后.
        Collections.sort(person01);
        Collections.sort(person02);
        Collections.sort(person03);
        Collections.sort(dipai);

        // 5. 看牌 -- 通过索引拿到对应的牌 .
        lookPai("周润发",person01,poker);
        lookPai("刘德华",person02,poker);
        lookPai("周润发",person03,poker);
        lookPai("底牌",dipai,poker);
    }
    // 看牌
    public static void lookPai(String name , ArrayList<Integer> index , HashMap<Integer , String> map ){
        // s输出姓名不换行 .
        System.out.print(name+":");
        // 遍历牌的索引 , 拿到索引 .
        for (Integer integer : index) {
            String paiName = map.get(integer);
            System.out.print(paiName+" ");
        }
        // 输出完一个玩家 , 换行 .
        System.out.println();
    }
}

运行结果如下:
牌是按照存放顺序的大小展示的.
这里写图片描述
注意:

开始都是通过牌的键(索引)来操作,最后通过键获取对应的值(牌).

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值