分析:

  1. 初始化一副扑克,放到list里面
  2. Collections中的shuffle方法对list中的数据进行打乱,实现洗牌功能
  3. 将牌分发给三个玩家,另外需要留下三张底牌,从list中取出三张牌,之后将剩下的分给三个玩家

代码:

 public class pkp {

public static void main(String[] args) {
    String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    String[] color = {"黑桃","红桃","方片","梅花"};
    
    ArrayList<String> poker = new ArrayList<>();
    
    //循环拼接扑克牌
    for(String s1 : color) {
        for(String s2 : num) {
            poker.add(s1 + s2);
        }
    }
    
    poker.add("大王");
    poker.add("小王");
    
    //洗牌
    Collections.shuffle(poker);
    
    ArrayList<String> dipai = new ArrayList<>();
    ArrayList<String> pony = new ArrayList<>();
    ArrayList<String> jack = new ArrayList<>();
    ArrayList<String> domain = new ArrayList<>();
    
    for(int i=0; i<poker.size(); i++) {
        if(i >= poker.size()-3) {
            dipai.add(poker.get(i));
        }else if(i%3 == 0) {
            pony.add(poker.get(i));
        }else if(i%3 == 1) {
            jack.add(poker.get(i));
        }else {
            domain.add(poker.get(i));
        }
    }
    
    System.out.println("底牌:" + dipai);
    System.out.println("pony:" + pony);
    System.out.println("jack:" + jack);
    System.out.println("domain:" + domain);
}

}
最后修改:2020 年 03 月 17 日 10 : 54 AM