import java.util.LinkedList;
import java.util.Random;
//扑克类
class Poker{
String color;//花色
String num;//点数
public Poker (String color,String num) {
this.color=color;
this.num =num;
}
public String toString() {
return "{"+color+num+"}";
}
}
public class Demo{
public static void main(String[] args) {
LinkedList<Poker> pokers=createPoker();
showPoker(pokers);
System.out.println("洗牌,洗牌。。。");
sufflePoker(pokers);
showPoker(pokers);
}
//生成一副扑克
public static LinkedList<Poker> createPoker() {
LinkedList<Poker> list=new LinkedList<Poker>();
String []color={"黑桃","红桃","梅花","方块"};
String []num={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
for (int i = 0; i < color.length; i++) {
for (int j = 0; j < num.length; j++) {
list.add(new Poker(color[i], num[j]));
}
}
return list;
}
//更好展示扑克
public static void showPoker(LinkedList<Poker> pokers){
for (int i = 0; i < pokers.size(); i++) {
System.out.print(pokers.get(i));
if (i%13==12) {
System.out.println();//换行
}
}
}
//洗扑克功能
public static void sufflePoker(LinkedList<Poker> pokers) {
Random random=new Random();//创建随机数对象
for (int i = 0; i < 100; i++) {//实现多次洗牌
//随机产生两个索引值
int index1=random.nextInt(pokers.size());
int index2=random.nextInt(pokers.size());
//根据索引值取出两张牌,然后交换两张牌的顺序
Poker poker1=pokers.get(index1);
Poker poker2=pokers.get(index2);
pokers.set(index1, poker2);
pokers.set(index2, poker1);
}
}
}