a761485716 2018-01-05 08:53 采纳率: 100%
浏览 1589
已采纳

java斗地主把手中的牌的牌型显示出来

public static void main(String[] args) {
String[] pai = new String[] { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
// String[] hs = new String[] { "♥", "♠", "♦", "♣" };
HashMap hj = new HashMap<>();
HashMap hy = new HashMap<>();
HashMap hb = new HashMap<>();

List<String> a = new ArrayList<String>();
for (int i = 0; i < pai.length; i++) {
    a.add(pai[i]);
    a.add(pai[i]);
    a.add(pai[i]);
    a.add(pai[i]);
}
a.add("大王");
a.add("小王");

Random rand = new Random();
String temp = null;
for (int k = 0; k < 100; k++) {
    int p = a.size();
    int l = rand.nextInt(p);
    int m = rand.nextInt(p);
    if (l == m)
        continue;
    {
        temp = a.get(l);
        a.set(l, a.get(m));
        a.set(m, temp);
    }
}
// Collections.shuffle(a);

List<String> j = new ArrayList<String>();
List<String> y = new ArrayList<String>();
List<String> b = new ArrayList<String>();
List<String> d = new ArrayList<String>();
for (int i = 0; i < a.size(); i++) {
    if (i >= a.size() - 3) {
        d.add(a.get(i));
    } else if (i % 3 == 0) {
        j.add(a.get(i));
    } else if (i % 3 == 1) {
        y.add(a.get(i));
    } else if (i % 3 == 2) {
        b.add(a.get(i));
    }
}
System.out.println(d);
System.out.println("甲" + j);
System.out.println("乙" + y);
System.out.println("丙" + b);
for (String str : j) {
    if (hj.containsKey(str)) {
        hj.put(str, hj.get(str) + 1);
    } else
        hj.put(str, 1);
}
System.out.println("j" + hj);

for (String str : y) {
    if (hy.containsKey(str)) {
        hy.put(str, hy.get(str) + 1);
    } else
        hy.put(str, 1);
}
System.out.println("y" + hy);

for (String str : b) {
    if (hb.containsKey(str)) {
        hb.put(str, hb.get(str) + 1);
    } else
        hb.put(str, 1);
}
System.out.println("b" + hb);

最好在我已经写出的代码上加代码,要显示出甲、乙、丙手牌的对子、三带、炸弹之类的牌型,求具体的代码!!!!!!!

  • 写回答

7条回答 默认 最新

  • 路漫漫兮其修远兮 2018-01-08 02:29
    关注

    @Test
    public void testFunction4(){
    /*数字*/
    String[] pai = new String[] { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2" };
    /*花色*/
    String[] hs = new String[] { "♥", "♠", "♦", "♣" };
    HashMap hj = new HashMap<>();
    HashMap hy = new HashMap<>();
    HashMap hb = new HashMap<>();
    List a = new ArrayList();
    for (int i = 0; i < pai.length; i++) {
    a.add(hs[0]+pai[i]);
    a.add(hs[1]+pai[i]);
    a.add(hs[2]+pai[i]);
    a.add(hs[3]+pai[i]);
    }
    a.add("大王");
    a.add("小王");

        Random rand = new Random();
        String temp = null;
        for (int k = 0; k < 100; k++) {
            int p = a.size();
            int l = rand.nextInt(p);
            int m = rand.nextInt(p);
            if (l == m) {
                continue;
            }
            {
                temp = a.get(l);
                a.set(l, a.get(m));
                a.set(m, temp);
            }
        }
    

    // Collections.shuffle(a);

        List<String> j = new ArrayList<String>();
        List<String> y = new ArrayList<String>();
        List<String> b = new ArrayList<String>();
        List<String> d = new ArrayList<String>();
        for (int i = 0; i < a.size(); i++) {
            if (i >= a.size() - 3) {
                d.add(a.get(i));
            } else if (i % 3 == 0) {
                j.add(a.get(i));
            } else if (i % 3 == 1) {
                y.add(a.get(i));
            } else if (i % 3 == 2) {
                b.add(a.get(i));
            }
        }
        System.out.println(d);
        System.out.println("甲" + j.toString());
        /*获得有三个的*/
        printPai(j,3,null);
        System.out.println("乙" + y.toString());
        System.out.println("丙" + b.toString());
        for (String str : j) {
            if (hj.containsKey(str)) {
                hj.put(str, hj.get(str) + 1);
            } else {
                hj.put(str, 1);
            }
        }
        System.out.println("j" + hj);
    
        for (String str : y) {
            if (hy.containsKey(str)) {
                hy.put(str, hy.get(str) + 1);
            } else {
    
                hy.put(str, 1);
            }
        }
        System.out.println("y" + hy);
    
        for (String str : b) {
            if (hb.containsKey(str)) {
                hb.put(str, hb.get(str) + 1);
            } else {
                hb.put(str, 1);
            }
        }
        System.out.println("b" + hb);
    }
    
    public void printPai(List<String> pai, Integer count ,String name) {
        if (count == null) {
            /*去掉花色,输出手里面的排*/
            pai.forEach(p ->p=p.substring(1,2));
            System.out.println(pai.toString());
        }else if (name !=null && name !=""){
            /*name为大王的时候输出大王,按名字查找,忽略大小写,忽略花色*/
            pai.stream().filter(p -> p.substring(1,2).equalsIgnoreCase(name)).collect(toList());
            System.out.println(name+":"+ pai.size()+"个");
        } else if (count!=null && name ==null || name !=""  ){
            /*查找指定数量的pai,2对子,3三个,4炸等,忽略花色*/
            HashMap<String, Integer> paiCount = new HashMap<>();
            pai.forEach(p->{
                String p2 =  p.substring(1,2);
                if (paiCount.containsKey(p2)) {
                    paiCount.put(p2, paiCount.get(p2)+1);
                } else {
                    paiCount.put(p2, 1);
                }
            });
            /*输出指定个数的*/
            ArrayList<String> resultPai = new ArrayList<String>();
            paiCount.forEach((k,v)->{
                if (v.equals(count)) {
                    resultPai.add(k);
                }
            });
            System.out.println(count+"个的牌有:"+resultPai.toString());
        } else {
            /*如果count是2输出手里面的对子,3数据三个,4输出炸弹,配合name,如name=大王,输出手里面是否有大王*/
            /*由于不能使用int类型变量来自增统计个数,就使用集合来统计个数一样的*/
            ArrayList list = new ArrayList();
            pai.stream().forEach(p ->{
                if (p.substring(1, 2).equalsIgnoreCase(name)) {
                    list.add(p);
                }
            });
            System.out.println("是否有"+count+"个"+name+":"+ (list.size()>=count?"是":"否"));
        }
    }
        printPai是我写的方法,具体注释在里面,输出想要的,具体可以更具你的需求对代买进行必要的修改,我只是按照我的想法写了一个,下面给出一个输出的结果:
        [♠A, ♦5, ♥9]
    

    甲[♣3, ♥5, ♦6, ♥7, ♦9, ♠2, ♦3, ♠Q, ♣5, ♠9, ♥10, ♦10, ♣K, ♠6, 小王, ♣9, ♥J]
    3个的牌有:[9]
    乙[♦2, ♣10, ♠4, ♥8, ♥4, ♣J, ♣4, ♥6, ♦J, ♠10, ♣A, ♥2, ♥A, ♦7, 大王, ♦8, ♣2]
    丙[♠7, ♣7, ♠5, ♠3, ♦Q, ♣6, ♦4, ♦A, ♥K, ♣Q, ♣8, ♠K, ♥3, ♦K, ♥Q, ♠J, ♠8]
    j{♥J=1, ♦3=1, ♣K=1, ♠6=1, ♣9=1, ♣3=1, ♥5=1, ♦6=1, ♥10=1, ♠Q=1, ♦10=1, ♥7=1, ♠2=1, ♣5=1, ♦9=1, 小王=1, ♠9=1}
    y{♦J=1, ♣J=1, ♦2=1, ♥2=1, ♥4=1, ♣2=1, ♠10=1, 大王=1, ♠4=1, ♣10=1, ♣4=1, ♥6=1, ♦7=1, ♦8=1, ♥8=1, ♥A=1, ♣A=1}
    b{♦K=1, ♥K=1, ♥Q=1, ♦4=1, ♣Q=1, ♥3=1, ♠J=1, ♦Q=1, ♠K=1, ♣7=1, ♠5=1, ♣8=1, ♠7=1, ♠3=1, ♣6=1, ♠8=1, ♦A=1}

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化