package cn.tedu;
// 三个方法 1.创建牌 2.洗牌 3. 发牌
// 1.嵌套for循环创建整幅牌 String[]
// 2.打乱 new Random()
// 3.不用留底拍 ABC ABC ABC 三个人 %
import java.util.Arrays;
import java.util.Random;
public class 扑克牌 {
public static void main (String[] args) {
String[] pokers = createPokers ();
String[] pokers2 = shuffle (pokers);
deal (pokers2);
}
private static void deal (String[] pokers2) {
String[] player1 = new String[pokers2.length / 3];
String[] player2 = new String[pokers2.length / 3];
String[] player3 = new String[pokers2.length / 3];
int a = 0;
int b = 0;
int c = 0;
for (int i = 0; i < pokers2.length; i++) {
if (i % 3 == 0) {
player1[a] = pokers2[i];
a++;
} else if (i % 3 == 1) {
player2[b] = pokers2[i];
b++;
} else if (i % 3 == 2) {
player3[c] = pokers2[i];
c++;
}
}
System.out.println ("1号玩家"+Arrays.toString (player1));
System.out.println ("2号玩家"+Arrays.toString (player2));
System.out.println ("3号玩家"+Arrays.toString (player3));
}
private static String[] shuffle (String[] pokers) {
String[] pokers2 = new String[54]; //洗好的牌
boolean[] kaiguan = new boolean[54];
Random random = new Random ();
int index = 0;
while (true) {
int i = random.nextInt (54); //23
if (kaiguan[i] == false) {
pokers2[index] = pokers[i];
index++;
kaiguan[i] = true;
}
if (index == 54) {
break;
}
}
return pokers2;
}
private static String[] createPokers () {
String[] huase = {"红桃", "黑桃", "方片", "梅花"};
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
String[] pokers = new String[54];
int index = 0;
for (int i = 0; i < huase.length; i++) {
for (int j = 0; j < a.length; j++) {
pokers[index] = huase[i] + a[j];
index++;
}
}
pokers[52] = "小王";
pokers[53] = "大王";
System.out.println (Arrays.toString (pokers));
return pokers;
}
}
Java代码扑克牌
最新推荐文章于 2022-05-17 21:34:43 发布