public class Card {//扑克类
private String face;
private String suit;
// 牌面值和花色初始化
public Card(String face, String suit) {
this.face = face;
this.suit = suit;
}
// 输出牌面值和花色
public String toString() {
return suit + " " + face;
}
public String getFace() {
return face;
}
public String getSuit() {
return suit;
}
// 洗牌的方法
public static void xipai(Card[] deck) {
for (int first = 0; first < deck.length; first++) {
int second = (int) (Math.random() * 52);// 随机生成0-51的数字
Card temp = deck[first]; // 表示此类的变量是自己本身,也就是自己的一个对象。
deck[first] = deck[second];
deck[second] = temp;
}
}
// 发牌
public static void fapai(Card[] deck) {
Card A[] = new Card[52];
Card B[] = new Card[52];
Card C[] = new Card[52];
Card D[] = new Card[52];
for (int i = 0; i < deck.length; i += 4) {
A[i] = deck[i];
B[i] = deck[i + 1];
C[i] = deck[i + 2];
D[i] = deck[i + 3];
}
int count = 0;
System.out.print("A玩家持牌\t");
for (int i = 0; i < deck.length; i += 4) {
System.out.print(A[i] + "\t");
count++;
if (count % 13 == 0) {
System.out.println("");
}
}
System.out.print("B玩家持牌\t");
for (int i = 0; i < deck.length; i += 4) {
System.out.print(B[i] + "\t");
count++;
if (count % 13 == 0) {
System.out.println("");
}
}
System.out.print("C玩家持牌\t");
for (int i = 0; i < deck.length; i += 4) {
System.out.print(C[i] + "\t");
count++;
if (count % 13 == 0) {
System.out.println("");
}
}
System.out.print("D玩家持牌\t");
for (int i = 0; i < deck.length; i += 4) {
System.out.print(D[i] + "\t");
count++;
if (count % 13 == 0) {
System.out.println("");
}
}
}
}
//测试类
public class Demo {
public static void main(String[] args) {
String [] f={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String s[]={"黑桃","红桃","梅花","方块"};
Card deck[]=new Card[52]; //对象数组
for ( int i = 0; i < deck.length; i++ ) {
deck[ i ] = new Card( f[ i % 13 ], s[ i / 13 ] );//数组中的每一个对象必须分别进行实例化操作
//System.out.println(deck[i].toString());
}
//********************************——洗牌——********************************
Card gina[]=new Card[52];
Card.xipai(deck);
//洗牌后的序列
for ( int i = 0; i < deck.length; i++ ){
System.out.println(deck[i].toString());
}
System.out.println("***************************************************");
for ( int i = 0; i < gina.length; i++ ){
gina[i]=deck[i];
}
//********************************——发牌——********************************
Card.fapai(gina);
//发牌后的序列
for ( int i = 0; i < deck.length; i++ ){
System.out.println(gina[i].toString());
}
}
}