斗地主有序发牌案例(Java)

效果图

 源代码

import javax.swing.plaf.ListUI;
import java.util.*;

/**
 * 斗地主发牌有序案例:
 * 1.准备牌
 * 2.洗牌
 * 3.发牌
 * 4.排序
 * 5.看牌
 */
public class DouDiZhu02 {
    public static void main(String[] args) {
        //1.准备牌
        //创建一个Map集合,存储牌的索引和组装好的牌
        HashMap<Integer,String> poker = new HashMap<>();

        //创建一个List集合,存储牌的索引
        ArrayList<Integer> pokerIndex = new ArrayList<>();

        //定义两个数组,存储花色和牌的序号
        String[] colors = {"♠","♥","♣","♦"};
        String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};

        //从0开始存索引,并往Map里存储对应的牌,同时往ArrayList里存牌的索引
        int index = 0;

        //存大小王
        poker.put(index,"大王");
        pokerIndex.add(index);
        index++;
        poker.put(index,"小王");
        pokerIndex.add(index);
        index++;

        //将牌存进相应的集合里
        for(String number : numbers){
            for(String color : colors){
                poker.put(index,color+number);
                pokerIndex.add(index);
                index++;
            }
        }

        //洗牌
        Collections.shuffle(pokerIndex);

        //发牌,发的是索引,为了让索引是排序的,创建TreeSet接收
        TreeSet<Integer> player01 = new TreeSet<>();
        TreeSet<Integer> player02 = new TreeSet<>();
        TreeSet<Integer> player03 = new TreeSet<>();
        TreeSet<Integer> dipai = new TreeSet<>();

        for(int i = 0;i < pokerIndex.size(); i++) {
            int sum = pokerIndex.get(i);
            if (i >= pokerIndex.size() - 3) {
                dipai.add(sum);
            } else if (i % 3 == 0) {
                player01.add(sum);
            } else if (i % 3 == 1) {
                player02.add(sum);
            } else if (i % 3 == 2) {
                player03.add(sum);
            }
        }

        lookPoker("玩家一",player01,poker );
        lookPoker("玩家二",player02,poker);
        lookPoker("玩家三",player03,poker);
        lookPoker("底牌",dipai,poker);
    }
    //看牌的方法
    public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){
        System.out.println(name+"的牌是-> ");
        for(Integer key : ts){
            String pooker = hm.get(key);
            System.out.print(pooker+" ");
        }
        System.out.println();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

T何必当初

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值