写这个代码的思路是
1.先创建54张牌的牌库,并给每张牌一个索引(使用map)
2.洗牌
3.取3张底牌,然后把牌发给3个玩家
4.根据索引排序,来实现玩家手牌的排序
代码如下
package com.zhongruan;import java.util.*;public class DouDiZhu1 {public static Map<Integer,String> qupai(){Map<Integer,String> map=new HashMap<>();String[] s1={"♣","♠","♥","◇"};String[] s2={"3","4","5","6","7","8","9","10","J","Q","k","A","2"};int index=-1;for (String i:s2){for (String j:s1){map.put(++index,i+j);}}map.put(++index,"dawang");map.put(++index,"xiaowang");return map;}public static List<List> fapai(List<Integer> a1,List<Integer> a2,List<Integer> a3,List<Integer> dipai,List<Integer> paiindex){List<List> list=new ArrayList<>();list.add(a1);list.add(a2);list.add(a3);list.add(dipai);dipai.add(paiindex.get(0));dipai.add(paiindex.get(1));dipai.add(paiindex.get(2));for (int i=3;i<54;i++){if (i%3==0){a1.add(paiindex.get(i));}else if (i%3==1){a2.add(paiindex.get(i));}else {a3.add(paiindex.get(i));}}return list;}public static void kanpai(List<Integer> list,Map<Integer,String> indexmap){for (Integer i:list){System.out.print(indexmap.get(i));}System.out.println();}public static void main(String[] args) {Map<Integer,String> indexmap=DouDiZhu1.qupai();List<Integer> paiindex=new ArrayList<>();paiindex.addAll(indexmap.keySet());Collections.shuffle(paiindex);List<Integer> a1=new ArrayList<>();List<Integer> a2=new ArrayList<>();List<Integer> a3=new ArrayList<>();List<Integer> dipai=new ArrayList<>();List<List> wanjia=DouDiZhu1.fapai(a1,a2,a3,dipai,paiindex);Collections.sort(a1);Collections.sort(a2);Collections.sort(a3);Collections.sort(dipai);DouDiZhu1.kanpai(a1,indexmap);DouDiZhu1.kanpai(a2,indexmap);DouDiZhu1.kanpai(a3,indexmap);DouDiZhu1.kanpai(dipai,indexmap);}
}