a761485716 2018-01-05 05:45 采纳率: 100%
浏览 1440
已采纳

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<String, Integer> hj = new HashMap<>();
    HashMap<String, Integer> hy = new HashMap<>();
    HashMap<String, Integer> 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);

我已经实现的代码,请问用枚举法可以实现得出手中牌的牌型吗,可以的话代码是什么急求,就在我这个代码上面加!!!

  • 写回答

6条回答 默认 最新

  • wangjin9805 2018-01-05 09:16
    关注

    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));
            }
        }
        Collections.sort(j);
        Collections.sort(y);
        Collections.sort(b);
        Collections.sort(d);
        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);
    
        System.out.println(getDataType(j));
        System.out.println(getDataType(y));
        System.out.println(getDataType(b));
    }
    
    private static int getDataType(List<String> list){
        String[] sortlist = {"3","4","5","6","7","8","9","10","J","Q","K","A"};
        int typenum=0;
        int flag = 0;
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<sortlist.length-1;){
            if(list.contains(sortlist[i]) ){
                sb.append(sortlist[i]+",");
                flag +=1;
            }else{
                if(flag>=5){
                    typenum++;
                    System.out.print("顺子->"+sb.toString()+"\n");
                    sb.setLength(0);
                }
                flag =0;
            }
            i++;
        }
        return typenum;
    }
    
        就加了一个统计顺子的方法 如果要算顺子 就要根据flag 值排列组合了  =5,1个 =6  3个 
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值
  • ¥20 数据排序,可选择排序方向
  • ¥15 修改一下代码,考虑进程到达时间不同的情况
  • ¥15 帮我看看这是个啥题,带解题过程和结果,条件如下FCF = 290471.33 g1 = 15% r = 8% g2 = 4% n = 5