import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ZhiPai {
public static void main(String[] args) {
int x,y;
Card card1 = null;
Card card2 = null;
//先定义两个随机数,待会洗牌的时候会用到
Random x1 = new Random();
Random x2 = new Random();
//使用泛型,定义了四个容器,分别用来存放整幅牌玩家1、玩家2和自己的牌
List<Card> cards = new ArrayList<Card>();
List<Card> wanjia1 = new ArrayList<Card>();
List<Card> wanjia2 = new ArrayList<Card>();
List<Card> my = new ArrayList<Card>();
//添加一对王牌
cards.add(new Card(0,"红鬼"));
cards.add(new Card(0,"黑鬼"));
//初始化一副牌
for(int i=1; i<=13; i++) {
cards.add(new Card(i,"黑桃"));
cards.add(new Card(i,"红桃"));
cards.add(new Card(i,"梅花"));
cards.add(new Card(i,"方块"));
}
//洗牌
for(int i=0; i<100; i++) {
x = x1.nextInt(cards.size());
y = x2.nextInt(cards.size());
card1 = cards.get(x);
card2 = cards.get(y);
cards.set(x, card2);
cards.set(y, card1);
}
//发牌
for(int i=0; i<cards.size();) {
my.add(cards.get(i++));
wanjia1.add(cards.get(i++));
wanjia2.add(cards.get(i++));
}
//打印
for(int i=0,j=0; i<my.size(); i++) {
x = my.get(i).id;
switch(x){
case 11:
System.out.print(my.get(i).s + " " + "J" + "
");
break;
case 12:
System.out.print(my.get(i).s + " " + "J" + "
");
break;
case 13:
System.out.print(my.get(i).s + " " + "J" + "
");
break;
}
System.out.print(my.get(i).s + " " + my.get(i).id + "
");
j++;
if(j%6==0) System.out.println();
}
}
}
//将牌定义为一个单独的内。原本发牌洗牌这些都应该在这个内里面的,这样更符合面向对象的思想。不过为了个人写起来更简便,所以直接把它们全部写到main()里面去了
class Card {
int id;
String s;
Card(int id, String s) {
this.id = id;
this.s = s;
}
}
[
本帖最后由 vecomwa 于 2009-11-5 10:57 编辑 ]