package palycard.falandordnew;
StanCard.java
package palycard.falandordnew;
public class StanCard {
public String color = null;
public String figure = null;
public String all = null;
public void allInitial(){
all = color+figure;
}
public StanCard objCopy(StanCard obj){
StanCard c = new StanCard();
c.all = obj.all+"";
c.color = obj.color+"";
c.figure = obj.figure+"";
return c;
}
}
Cards.java
package palycard.falandordnew;
public class Cards {
// 定义牌集合
/*
* private final String Card[] = { "红桃3", "黑桃3", "梅花3", "方块3", "红桃4", "黑桃4",
* "梅花4", "方块4", "红桃5", "黑桃5", "梅花5", "方块5", "红桃6", "黑桃6", "梅花6", "方块6",
* "红桃7", "黑桃7", "梅花7", "方块7", "红桃8", "黑桃8", "梅花8", "方块8", "红桃9", "黑桃9",
* "梅花9", "方块9", "红桃10", "黑桃10", "梅花10", "方块10", "红桃J", "黑桃J", "梅花J", "方块J",
* "红桃Q", "黑桃Q", "梅花Q", "方块Q", "红桃K", "黑桃K", "梅花K", "方块K", "红桃A", "黑桃A",
* "梅花A", "方块A", "红桃2", "黑桃2", "梅花2", "方块2", "小王", "大王" };
*/
private final String Colours[] = { "红桃", "黑桃", "方块", "梅花" };
private final String Figures[] = { "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K", "A", "2" };
private final String SpecialCards[] = { "小王", "大王" };
// 起到赋值克隆数组的作用
private StanCard[] sCardsCopy(StanCard[] X) {
StanCard[] Y = new StanCard[X.length];
for (int i = 0; i < X.length; i++) {
StanCard sc = new StanCard();
Y[i]=sc.objCopy(X[i]);
}
return Y;
}
// 生成一个随机的54张牌;
public StanCard[] rdCards() {
StanCard[] X = new StanCard[54];
X = this.sCardsCopy(this.initial());
StanCard[] rdCards = new StanCard[54];
boolean[] occupied = new boolean[54];// 新定义一个boolean型数组时,每一个值都初始化为false;
for (int i = 0; i < rdCards.length; i++) {
int r = (int) (Math.random() * 54);
if (!occupied[r]) {
StanCard sc = new StanCard();
rdCards[i]=sc.objCopy(X[r]);
occupied[r] = true;
} else {
i--;
}
}
return rdCards;
}
// 自动生成54张标准排列的对象数组;
private StanCard[] initial() {
StanCard[] AllCards = new StanCard[54];
for (int i = 0; i < Figures.length; i++) {
for (int j = 0; j < Colours.length; j++) {
StanCard c = new StanCard();
c.color = Colours[j].toString();
c.figure = Figures[i].toString();
c.allInitial();
AllCards[4 * i + j] = c;
}
}
StanCard c = new StanCard();
c.all = SpecialCards[0].toString();
c.color = "无";
c.figure = "无";
AllCards[52] = c;
c = new StanCard();
c.all = SpecialCards[1].toString();
c.color = "无";
c.figure = "无";
AllCards[53] = c;
return AllCards;
}
public static void main(String[] args) {
Cards c = new Cards();
StanCard[] x = c.sCardsCopy(c.rdCards());
for (int i = 0; i < x.length; i++) {
System.out.print(x[i].all+"\t");
if ((i+1)%18==0&&i!=0) {
System.out.println();
}
}
}
}
见笑见笑!!!