public static void main(String[] args) { //♥ ♠ ♦ ♣ //定义一个HashMap集合存储,索引和牌 HashMap<Integer,String> hm = new HashMap<Integer, String>(); //定义一个ArrayList存储索引 ArrayList<Integer> alist = new ArrayList<Integer>(); //定义牌的颜色 String[] color ={"♥","♠","♦","♣"}; //定义牌的number String[] number={"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; //存牌 定义索引index int index=0; for(int i=0;i<number.length;i++){ for (int j=0;j<color.length;j++){ String poker = color[j]+number[i]; hm.put(index,poker); alist.add(index); index++; } } hm.put(index,"大王"); alist.add(index); index++; hm.put(index,"小王"); alist.add(index); //洗牌,洗的是索引 Collections.shuffle(alist); //发牌 用TreeSet接受索引,用自然排序,看牌时根据索引去找牌 TreeSet<Integer> lqxSet = new TreeSet<Integer>(); TreeSet<Integer> lySet = new TreeSet<Integer>(); TreeSet<Integer> fqySet = new TreeSet<Integer>(); TreeSet<Integer> dpSet = new TreeSet<Integer>(); for (int j=0;j<alist.size();j++){ Integer x = alist.get(j); if(j>=alist.size()-3){ dpSet.add(x); }else if(j%3==0){ lqxSet.add(x); }else if(j%3==2){ fqySet.add(x); }else{ lySet.add(x); } } lookPoker("林青霞",lqxSet,hm); lookPoker("柳岩",lySet,hm); lookPoker("风清扬",fqySet,hm); lookPoker("底牌",dpSet,hm); } //看牌 根据索引去 HashMap中找牌 public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm){ System.out.print(name+"的牌是:"); for(Integer it : ts){ String poker = hm.get(it); System.out.print(poker+" "); } System.out.println(); }
java 集合模拟斗地主
最新推荐文章于 2022-10-12 15:56:48 发布