模拟斗地主洗牌和发牌
public class MyTest {
public static void main(String[] args) {
//案例演示:
//模拟斗地主洗牌和发牌,牌没有排序
//得有一副牌
ArrayList<String> pokerBox = new ArrayList<>();
//生成54张牌放到牌盒里面
String[] colors = {"♠", "♥", "♦", "♣"};
String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String color : colors) {
for (String num : nums) {
pokerBox.add(color.concat(num));
}
}
//手动添加大小王
pokerBox.add("★");
pokerBox.add("☆");
//洗牌
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
Collections.shuffle(pokerBox);
//发牌
ArrayList<String> 高进 = new ArrayList<>();
ArrayList<String> 刀仔 = new ArrayList<>();
ArrayList<String> 星仔 = new ArrayList<>();
ArrayList<String> 底牌 = new ArrayList<>();
// 高进 = (ArrayList<String>) pokerBox.subList(0,17);
// 高进 0 3 6 9
//刀仔 1 4 7 10
// 星仔 2 5 8 11
for (int i = 0; i < pokerBox.size(); i++) {
if (i >= pokerBox.size() - 3) {
底牌.add(pokerBox.get(i));
} else if (i % 3 == 0) {
高进.add(pokerBox.get(i));
} else if (i % 3 == 1) {
刀仔.add(pokerBox.get(i));
} else {
星仔.add(pokerBox.get(i));
}
}
//看牌
lookPoker("高进",高进);
lookPoker("刀仔", 刀仔);
lookPoker("星仔", 星仔);
lookPoker("底牌", 底牌);
}
private static void lookPoker(String name, ArrayList<String> list) {
System.out.println(name);
for (String s : list) {
System.out.print(s+" ");
}
System.out.println();
}
}
添加洗牌发牌后,按顺序整理牌
public class MyTest {
public static void main(String[] args) {
//选用双列集合
HashMap<Integer, String> pokerBox = new HashMap<>();
//创建一个索引集合
ArrayList<Integer> indexs = new ArrayList<>();
int index = 0;
String[] colors = {"♠", "♥", "♦", "♣"};
String[] nums = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
for (String num : nums) {
for (String color : colors) {
pokerBox.put(index, num.concat(color));
indexs.add(index);
index++;
}
}
//手动添加大小王
pokerBox.put(index, "★");//
indexs.add(index);
index++;
pokerBox.put(index, "☆");
indexs.add(index);
//洗牌
Collections.shuffle(indexs);
//发牌
TreeSet<Integer> 高进 = new TreeSet<Integer>();
TreeSet<Integer> 刀仔 = new TreeSet<Integer>();
TreeSet<Integer> 星仔 = new TreeSet<Integer>();
TreeSet<Integer> 底牌 = new TreeSet<Integer>();
for (int i = 0; i < indexs.size(); i++) {
if (i >= indexs.size() - 3) {
底牌.add(indexs.get(i));
} else if (i % 3 == 0) {
高进.add(indexs.get(i));
} else if (i % 3 == 1) {
刀仔.add(indexs.get(i));
} else {
星仔.add(indexs.get(i));
}
}
//看牌
lookPoker("高进", 高进, pokerBox);
lookPoker("刀仔", 刀仔, pokerBox);
lookPoker("星仔", 星仔, pokerBox);
lookPoker("底牌", 底牌, pokerBox);
}
private static void lookPoker(String name, TreeSet<Integer> set, HashMap<Integer, String> pokerBox) {
System.out.println(name);
for (Integer key : set) {
System.out.print(pokerBox.get(key) + " ");
}
System.out.println();
}
}