集合模拟斗地主的洗牌发牌抢地主
import java.util.*;
public class LandlordsGame {
public static void main(String[] args) {
getBox();
}
private static void getBox() {
ArrayList<String> num = new ArrayList<>();
Collections.addAll(num, "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2");
ArrayList<String> color = new ArrayList<>();
Collections.addAll(color, "♣", "♥", "♠", "♦");
HashMap<Integer, String> box = new HashMap<>();
int i = 0;
for (String n : num) {
for (String c : color) {
String concat = c.concat(n);
box.put(i, concat);
i++;
}
}
box.put(52, "☪");
box.put(53, "☀");
ArrayList<Integer> boxNum = new ArrayList<>();
Set<Integer> set = box.keySet();
for (Integer integer : set) {
boxNum.add(integer);
}
Collections.shuffle(boxNum);
TreeSet<Integer> one = new TreeSet<>();
TreeSet<Integer> two = new TreeSet<>();
TreeSet<Integer> three = new TreeSet<>();
TreeSet<Integer> four = new TreeSet<>();
for (int j = 0; j < boxNum.size(); j++) {
if (j >= boxNum.size() - 3) {
four.add(boxNum.get(j));
} else if (j % 3 == 0) {
one.add(boxNum.get(j));
} else if ((j - 1) % 3 == 0) {
two.add(boxNum.get(j));
} else {
three.add(boxNum.get(j));
}
}
seeBox("Lee", one, box);
seeBox("ZZ", two, box);
seeBox("Jk", three, box);
seeBox("底牌", four, box);
Scanner scanner = new Scanner(System.in);
System.out.println("请输入抢牌者姓名");
String s = scanner.nextLine();
if (s.equalsIgnoreCase("lee")) {
getPoke(one, four);
seeBox("Lee", one, box);
seeBox("ZZ", two, box);
seeBox("Jk", three, box);
} else if (s.equalsIgnoreCase("zz")) {
getPoke(two, four);
seeBox("Lee", one, box);
seeBox("ZZ", two, box);
seeBox("Jk", three, box);
} else if (s.equalsIgnoreCase("jk")) {
getPoke(three, four);
seeBox("Lee", one, box);
seeBox("ZZ", two, box);
seeBox("Jk", three, box);
} else {
System.out.println("输入错误,游戏结束");
return;
}
}
public static void seeBox(String name, TreeSet<Integer> set, HashMap<Integer, String> box) {
System.out.print(name + "\t");
for (Integer data : set) {
System.out.print(box.get(data) + "\t");
}
System.out.println();
}
public static void getPoke(TreeSet<Integer> t1, TreeSet<Integer> t2) {
t1.addAll(t2);
}
}
Lee ♣4 ♦4 ♦6 ♠7 ♣8 ♦8 ♣9 ♥9 ♦9 ♦10 ♣J ♥Q ♦Q ♣K ♦K ♥A ♠A
ZZ ♣3 ♥3 ♠3 ♠4 ♣6 ♠6 ♥7 ♦7 ♠8 ♠9 ♠10 ♥J ♠J ♣A ♥2 ☪ ☀
Jk ♥4 ♣5 ♥5 ♠5 ♦5 ♥6 ♥8 ♣10 ♦J ♣Q ♠Q ♥K ♠K ♦A ♣2 ♠2 ♦2
底牌 ♦3 ♣7 ♥10
请输入抢牌者姓名
zz
Lee ♣4 ♦4 ♦6 ♠7 ♣8 ♦8 ♣9 ♥9 ♦9 ♦10 ♣J ♥Q ♦Q ♣K ♦K ♥A ♠A
ZZ ♣3 ♥3 ♠3 ♦3 ♠4 ♣6 ♠6 ♣7 ♥7 ♦7 ♠8 ♠9 ♥10 ♠10 ♥J ♠J ♣A ♥2 ☪ ☀
Jk ♥4 ♣5 ♥5 ♠5 ♦5 ♥6 ♥8 ♣10 ♦J ♣Q ♠Q ♥K ♠K ♦A ♣2 ♠2 ♦2