斗地主实现洗牌,发牌,看牌功能

public class poker {
    public static void main(String[] args) {
        //键是编号,值是牌
        HashMap<Integer, String> hm = new HashMap<>();
        //创建ArrayList存储编号
        ArrayList<Integer> array = new ArrayList<>();

        //创建花色和点数
        String[] huase = {"♠", "♥", "♦", "♣"};
        String[] num = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "k", "A", "2"};
        int index = 0;//编号的索引

        for (String s : num) {
            for (String value : huase) {
                hm.put(index, s + value);
                array.add(index);
                index++;
            }
        }
        hm.put(index, "小王");
        array.add(index);
        index++;
        array.add(index);
        hm.put(index, "大王");

        //使用shuffle()洗牌
        Collections.shuffle(array);
        System.out.println(hm.keySet());
        //创建三个玩家和底牌,使用Treeset可以对牌自然排序
        TreeSet<Integer> hjj = new TreeSet<>();
        TreeSet<Integer> hst = new TreeSet<>();
        TreeSet<Integer> hjh = new TreeSet<>();
        TreeSet<Integer> dipai = new TreeSet<>();

        //发牌,将编号发给Treeset
        for (int i = 0; i < array.size(); i++) {
            int x=array.get(i);
            if (i >= array.size() - 3) {
                dipai.add(x);
            } else if (i % 3 == 0) {
                hjj.add(x);
            } else if (i % 3 == 1) {
                hst.add(x);
            } else {
                hjh.add(x);
            }
        }

        lookpoker("hjj", hjj, hm);
        lookpoker("hst", hst, hm);
        lookpoker("hjh", hjh, hm);
        lookpoker("底牌", dipai, hm);

    }

    public static void lookpoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
        System.out.println(name + "的牌是:");
        for (Integer key : ts) {
            String value = hm.get(key);
            System.out.print(value + " ");
        }
        System.out.println();
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值