继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

实现模拟斗地主的功能

斗胆请问大侠
关注TA
已关注
手记 2
粉丝 0
获赞 24

/组合牌/

//放扑克牌的集合
    Map<Integer, String> poker = new HashMap<Integer, String>();
    String[] count=new String[]{"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
    String[] color=new String[]{"","","",""};
    //键位从2索引开始,除去大小王
    int index=2;

    for(String i:color){
        for(String j:count){
         poker.put(index,i+j);

         index++;

        }

    }   

    //单独存储大小王
    poker.put(0, "大王");
    poker.put(1, "小王");

/洗牌/

//获取poker的键
    ArrayList<Integer> array=new ArrayList<Integer>();
    Set<Integer> set=poker.keySet();
    for(Integer i:set){
        array.add(i);

    }
    //打乱牌
    Collections.shuffle(array);

/发牌/


    //玩家1
    ArrayList<Integer> play1=new ArrayList<Integer>();
    //玩家2
    ArrayList<Integer> play2=new ArrayList<Integer>();
    //玩家3
    ArrayList<Integer> play3=new ArrayList<Integer>();
    //底牌
    ArrayList<Integer> dp=new ArrayList<Integer>();

    //定义三个变量
    int a=0;int b=0;int c=0;

    //每个玩家17张牌
    int cout=51;

    //发牌先发1,在2,然后3,如此循环
    for(int i:array){

        if(a<=b && a<=c && cout>0){
            play1.add(i);
            a++;
            cout--;
            continue;
        }
        if(b<a && b<=c &&cout>0){
            play2.add(i);
            b++;
            cout--;
            continue;
        }
        if(c<a && c<b &&cout>0){
            play3.add(i);
            c++;
            cout--;
            continue;
        }

        dp.add(i);

    }

    //对玩家手中牌排序
    Collections.sort(play1);
    Collections.sort(play2);
    Collections.sort(play3);

/看牌/
//变量
String s ="";
String st="";
String str="";
String stri="";
//玩家一手中牌

for(Integer i:play1){
    s=s+poker.get(i)+"  ";

}
//玩家二手中牌
for(Integer j:play2){
    st=st+poker.get(j)+"  ";

}
//玩家三手中牌
for(Integer m:play3){
    str=str+poker.get(m)+"  ";
}

for(Integer n:dp){
   stri=stri+poker.get(n)+"  ";
}

System.out.println("玩家一手中牌:"+s);
System.out.println("玩家二手中牌:"+st);
System.out.println("玩家三手中牌:"+str);
System.out.println("三张底牌:"+stri);

}
}
输出结果:
图片描述

打开App,阅读手记
11人推荐
发表评论
随时随地看视频慕课网APP