a761485716
2018-01-05 05:45
采纳率: 90.9%
浏览 1.4k
已采纳

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个 
    
    
    
    已采纳该答案
    打赏 评论
  • 熊人族无所畏惧 2018-01-05 06:38

    ?枚举类 要新建类的啊 在源代码加不了 不如用二维数组或者 map

    打赏 评论
  • wangjin9805 2018-01-05 07:01

    http://blog.csdn.net/u012749709/article/details/43577203
    看看这个分享的代码,他这个是一副牌的,你在再加个数据,三维实现多副牌

    打赏 评论
  • 熊人族无所畏惧 2018-01-05 07:16

    public class Pai {
    private String paise;
    private String pamian;

    public Pai(String paise, String pamian) {
        super();
        this.paise = paise;
        this.pamian = pamian;
    }
    
    public String getPaise() {
        return paise;
    }
    
    public void setPaise(String paise) {
        this.paise = paise;
    }
    
    public String getPamian() {
        return pamian;
    }
    
    public void setPamian(String pamian) {
        this.pamian = pamian;
    }
    
    @Override
    public String toString() {
        return paise + pamian;
    }
    
    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[] { "♥", "♠", "♦", "♣" };
        // 组牌
        List<Pai> pais = new ArrayList<>(54);
        Pai p_ = null;
        for (int i = 0; i < pai.length; i++) {
            for (int j = 0; j < hs.length; j++) {
                p_ = new Pai(hs[j], pai[i]);
                pais.add(p_);
            }
        }
        pais.add(new Pai("", "大王"));
        pais.add(new Pai("", "小王"));
        System.out.println("共有牌:  " + pais.size() + "张");
        // 洗牌
        Collections.shuffle(pais);
        System.out.println(pais);
        // 留最后三张作为底牌
        List<Pai> dipai = new ArrayList<>(3);
        dipai.add(pais.get(pais.size() - 1));
        pais.remove(pais.size() - 1);
        dipai.add(pais.get(pais.size() - 1));
        pais.remove(pais.size() - 1);
        dipai.add(pais.get(pais.size() - 1));
        pais.remove(pais.size() - 1);
        System.out.println(dipai);
        // 发牌
        List<Pai> p1 = new ArrayList<>();// 玩家1
        List<Pai> p2 = new ArrayList<>();// 玩家2
        List<Pai> p3 = new ArrayList<>();// 玩家3
        int index = 0;
        do {
            p1.add(pais.get(index));
            p2.add(pais.get(index + 1));
            p3.add(pais.get(index + 2));
            index += 3;
        } while (index <= pais.size()-3);
        System.out.println("玩家1牌:  " + p1);
        System.out.println("玩家2牌:  " + p2);
        System.out.println("玩家3牌:  " + p3);
        // 开始抢地主
        int dizhu = new Random().nextInt(3) + 1;
        switch (dizhu) {
        case 1:
            p1.addAll(dipai);
            break;
        case 2:
            p2.addAll(dipai);
            break;
        case 3:
            p3.addAll(dipai);
            break;
        }
        System.out.println("玩家" + dizhu + "抢到了地主!");
    
        System.out.println("玩家1最终牌面:  " + p1);
        System.out.println("玩家2最终牌面:  " + p2);
        System.out.println("玩家3最终牌面:  " + p3);
    }
    

    }

    打赏 评论
  • wangjin9805 2018-01-05 09:10

    private static int getDataType(List 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 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;
    }

    打赏 评论
  • cqwq77 2018-01-05 09:44

    就加了一个统计顺子的方法 如果要算顺子 就要根据flag 值排列组合了 =5,1个 =6 3个

    打赏 评论

相关推荐 更多相似问题