package self.training; import java.util.Random; public class HomeWork { public static void main(String[] args) { /* * 创建一副扑克牌 */ String[] poker = new String[54];//创建扑克牌数组 String[] followColor = new String[]{"红心","梅花","黑桃","方块"};//创建花色数组 String[] numble = new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};//创建点数数组 for(int i=0; i< followColor.length; i++){ for(int j=0; j< numble.length; j++){ poker[i*13+j] = followColor[i]+numble[j]; } } poker[52] = "小王"; poker[53] = "大王"; /* * 打乱一副牌 */ Random random = new Random();//导入随机数API for(int i= poker.length-1; i>0; i--){//从后往前打乱 int r = random.nextInt(i); String a = poker[i];//取出最后一张牌,并赋值给a poker[i] = poker[r];//随机取出一张牌放到最后 poker[r] = a;//将之前取出的牌放入随机取出的牌的位置 } /* * 发牌 */ int underCard = random.nextInt(51);//生成一个0~50的随机数 String instructionCard = poker[underCard];//把随机数对应的那张牌作为关键牌,决定谁是地主 String[] play1 = new String[17];//创建玩家1的数组 String[] play2 = new String[17];//创建玩家2的数组 String[] play3 = new String[17];//创建玩家3的数组 String[][] card = {play1,play2,play3}; for (int i=0;i< poker.length-3;i++){ card[i%3][i/3] = poker[i]; } System.out.print("玩家1的牌为:"); for (int i=0; i< play1.length; i++){ System.out.print(play1[i]+" "); if(instructionCard == play1[i]){//检查玩家1手中是否有关键牌 System.out.print(poker[51]+" "+poker[52]+" "+poker[53]+" "); } } System.out.println();//换行 System.out.print("玩家2的牌为:"); for (int i=0; i< play2.length; i++){ if(instructionCard == play2[i]){//检查玩家2手中是否有关键牌 System.out.print(poker[51]+" "+poker[52]+" "+poker[53]+" "); } System.out.print(play2[i]+" "); } System.out.println();//换行 System.out.print("玩家3的牌为:"); for (int i=0; i< play3.length; i++){ if(instructionCard == play3[i]){//检查玩家3手中是否有关键牌 System.out.print(poker[51]+" "+poker[52]+" "+poker[53]+" "); } System.out.print(play3[i]+" "); } } }
扑克牌打乱发牌(初学一周)
于 2022-04-04 16:32:28 首次发布