斗地主案例

斗地主案例实现

public class DouDiZhu {

public static void main(String[] args) {

    ArrayList<String> poker = new ArrayList<>();

    String[] colors = {"♠","♥","♣","♦"};

    String[] nums = {"2","A","k","j","q","3","4","5","6","7","8","9","10"};

    poker.add("大王");

    poker.add("小王");

    for(String number:nums){

        for (String color : colors) {

            //System.out.println(color+number);

            poker.add(color+number);

        }
    
    }
   
    System.out.println(poker);

    Collections.shuffle(poker);
    //System.out.println(poker);

    ArrayList<String> player01 = new ArrayList<>();
    ArrayList<String> player02 = new ArrayList<>();
    ArrayList<String> player03 = new ArrayList<>();
    ArrayList<String> diPai = new ArrayList<>();
    for (int i = 0; i < poker.size(); i++) {
        String p = poker.get(i);
        if (i>=51){
            diPai.add(p);
        }else if (i%3==0){
            player01.add(p);
        }
        else if (i%3==1){
            player02.add(p);
        }
        else if (i%3==2){
            player03.add(p);
        }
    }
    System.out.println("赌神"+player01);
    System.out.println("赌侠"+player02);
    System.out.println("赌圣"+player03);
    System.out.println("底牌"+diPai);
}

输出结果
在这里插入图片描述

斗地主进阶版

public class DouDiZhu2 {

public static void main(String[] args) {
    //创建一个map集合,存储牌的索引和组装好的牌
    HashMap<Integer,String> poker = new HashMap<>();
    //创建一个list集合,存储牌的索引
    ArrayList<Integer> pokerIndex = new ArrayList<>();
    //定义两个集合,存储牌的花色金额序号
    List<String> colors = List.of("♦", "♣", "♥", "♠");
    List<String> num = List.of("2", "A", "k", "q", "j", "10", "9", "8", "7", "6", "5", "4", "3");
    //把大王和小王存储到集合中
    //定义一个牌的索引
    int index = 0;
    poker.put(index,"大王");
    pokerIndex.add(index);
    index++;
    poker.put(index,"小王");
    pokerIndex.add(index);
    index++;
    //循环嵌套遍历两个集合,组装52张牌,存储到集合中
    for (String number : num) {
        for (String color : colors) {
            poker.put(index,color+number);
            pokerIndex.add(index);
            index++;
        }
    }
    //System.out.println(poker);
    //System.out.println(pokerIndex);

    //2.洗牌
    //使用Collections中的方法shuffle(list)
    Collections.shuffle(pokerIndex);
    //System.out.println(pokerIndex);

    //3.发牌
    //定义4个集合,存储玩家牌的索引和底牌的索引
    ArrayList<Integer> player01 = new ArrayList<>();
    ArrayList<Integer> player02 = new ArrayList<>();
    ArrayList<Integer> player03 = new ArrayList<>();
    ArrayList<Integer> DiPai = new ArrayList<>();
    //遍历存储牌索引的list集合,获取每一个牌的索引
    for (int i = 0; i < pokerIndex.size(); i++) {
        Integer in = pokerIndex.get(i);
        //先判断底牌
        if (i>=51){
            //给底牌发牌
            DiPai.add(in);
        }else if (i%3==0){
            //给玩家1发牌
            player01.add(in);
        }
        else if (i%3==1){
            //给玩家2发牌
            player02.add(in);
        }
        else if (i%3==2){
            //给玩家3发牌
            player03.add(in);
        }
    }
    //4.排序
    //使用Collections中的方法sort(list)
    Collections.sort(player01);
    Collections.sort(player02);
    Collections.sort(player03);
    Collections.sort(DiPai);

    //5.看牌
    //调用看牌的方法

    lookPoker("高凯",poker,player01);
    lookPoker("高进",poker,player02);
    lookPoker("周润发",poker,player03);
    lookPoker("底牌",poker,DiPai);
}
//定义一个看牌的方法。提高代码的复用性
//参数:
        //String name:玩家名称
        //HashMap<Integer,String> poker:存储牌的集合
        //ArrayList<Integer> list: 存储玩家和底牌list的集合
//查表法:
        //遍历玩家或者底牌集合,获取牌的索引
        //使用牌的索引,去map集合中,找到对应的牌

public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
    //输出玩家名称,不换行
    System.out.print(name+":");
    //遍历玩家或者底牌集合,获取牌的索引
    for (Integer key : list) {
        //使用牌的索引,去map集合中,找到对应的牌
        String value = poker.get(key);
        System.out.print(value+" ");
    }
    System.out.println();//打印完每一个玩家的牌,换行
}

输出结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值