import java.util.LinkedList;
import java.util.Random;
public class PokerDemo {
public static void main(String[] args){
LinkedList<Poker> poker= creatPoker();
sufferPoker(poker);
priviewPoker(poker);
}
// 通过交换值洗牌,思路利用随机数取出两张扑克牌交换位置,重复这个操作达到洗牌目的。
public static void sufferPoker(LinkedList<Poker> poker){
Random a=new Random();
for (int i = 0; i < 100; i++) {
int index1=a.nextInt(52);
int index2=a.nextInt(52);
Poker poker2=(Poker)poker.get(index1);
Poker poker3=(Poker)poker.get(index2);
poker.set(index1,poker3);
poker.set(index2, poker2);
}
}
// 查看现在扑克排列
public static void priviewPoker(LinkedList<Poker> poker){
for (int i = 0; i < poker.size(); i++) {
System.out.print(poker.get(i)+"\t");
if(i%10==9){
System.out.println(); //每10张换行方便显示
}
}
}
//生成扑克 利用循环匹配生成扑克牌,再使用集合接收
public static LinkedList<Poker> creatPoker() {
String number[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String color[]={"红心","方块","梅花","黑桃"};
LinkedList<Poker> a=new LinkedList<Poker>();
for (int i = 0; i < number.length; i++) {
String n =number[i];
for (int j = 0; j < color.length; j++) {
String c = color[j];
a.add(new Poker(n,c));
}
}
return a;
}
}
//定义扑克类属性有花色数字,重写toSting方法方便显示。
class Poker{
String color;
public Poker(String color, String number) {
this.color = color;
this.Number = number;
}
String Number;
@Override
public String toString() {
return this.Number+this.color;
}
}
|
|