/**
* 斗地主综合案例:有序版本
* 1.准备牌
* 2.洗牌
* 3.发牌
* 4.排序
* 5.看牌
*/
public class DouDiZhuDemo {
public static void main(String[] args) {
//@准备牌
String[] strA = {"♥", "♠", "♣", "♦"};
String[] strB = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//创建一个集合用来存放组装好的牌
ArrayList<String> poker = new ArrayList<>();
//创建一个集合用来记录每一张牌的索引
ArrayList<Integer> indexs = new ArrayList<>();
//嵌套for循环遍历数组,得到组装好的牌和牌的索引
int index = 0;
for (String s : strB) {
for (String s1 : strA) {
poker.add(s1 + s);
indexs.add(index++);
}
}
//添加小王,大王到poker集合中
poker.add("小王");
indexs.add(index++);
poker.add("大王");
indexs.add(index);
//System.out.println(poker);
//System.out.println(indexs);
//@洗牌
Collections.shuffle(indexs);
//System.out.println(indexs);
//@发牌
//准备四个集合用来存储3个玩家手中的牌和地主牌
ArrayList<Integer> PA = new ArrayList<>();
ArrayList<Integer> PB = new ArrayList<>();
ArrayList<Integer> PC = new ArrayList<>();
ArrayList<Integer> DiZhuP = new ArrayList<>();
//给玩家发牌
for (int i = 0; i < indexs.size(); i++) {
if (i >= 51) {
DiZhuP.add(indexs.get(i));
} else if (i % 3 == 0) {
PA.add(indexs.get(i));
} else if (i % 3 == 1) {
PB.add(indexs.get(i));
} else {
PC.add(indexs.get(i));
}
}
//@给牌排序
Collections.sort(PA);
Collections.sort(PB);
Collections.sort(PC);
//System.out.println(PA);
//System.out.println(PB);
//System.out.println(PC);
//System.out.println(DiZhuP);
//@让玩家查看手中的牌
System.out.print("赌神: ");
for (int i = 0; i < PA.size(); i++) {
System.out.print(poker.get(PA.get(i)) + " ");
}
System.out.println();
System.out.print("赌圣: ");
for (int i = 0; i < PB.size(); i++) {
System.out.print(poker.get(PB.get(i)) + " ");
}
System.out.println();
System.out.print("赌侠: ");
for (int i = 0; i < PC.size(); i++) {
System.out.print(poker.get(PC.get(i)) + " ");
}
System.out.println();
System.out.print("地主牌: ");
for (int i = 0; i < DiZhuP.size(); i++) {
System.out.print(poker.get(DiZhuP.get(i)) + " ");
}
System.out.println();
}
} |
|