1、分析:首先应准备牌:完成数字与纸牌的映射关系:使用双列Map(HashMap)集合,完成一个数字与字符串纸牌的对应关系。
洗牌:通过数字完成洗牌发牌(借助工具类)
发牌:将每个人以及底牌设计,将最后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌给3个玩家。存放的过程中要求数字大小与斗地主规则的大小对应排序。将代表不同纸牌的数字分配给不同的玩家与底牌。
看牌:通过Map集合找到对应字符展示。通过查询纸牌与数字的对应关系,由数字转成纸牌字符串再进行展示。
public static void main(String[] args) {
//创建集合
HashMap<Integer, String> hashMap = new HashMap<>();
ArrayList array = new ArrayList<>();
//创建花色
String[] color = {"♦", "♣", "♥", "♠"};
//创建点数
String[] poker = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
int index = 0;
//拼接
for (String colo : color) {
for (String poke : poker) {
hashMap.put(index, colo + poke);
array.add(index);
index++;
}
}
//添加大小王
hashMap.put(index, "大王");
array.add(index);
index++;
hashMap.put(index, "小王");
array.add(index);
//洗牌
Collections.shuffle(array);
//创建三人和底牌的集合
TreeSet<Integer> zhangsan = new TreeSet<>();
TreeSet<Integer> lisi = new TreeSet<>();
TreeSet<Integer> wangwu = new TreeSet<>();
TreeSet<Integer> pipoker = new TreeSet<>();
//给编号
for (int i = 0; i < array.size(); i++) {
int integer = array.get(i);
if (i >= array.size() - 3) {
pipoker.add(integer);
} else if (i % 3 == 0) {
zhangsan.add(integer);
} else if (i % 3 == 1) {
lisi.add(integer);
} else if (i % 3 == 2) {
wangwu.add(integer);
}
}
System.out.println(zhangsan);
//看牌
main("张三", hashMap, zhangsan);
main("李四", hashMap, lisi);
main("王五", hashMap, wangwu);
main("底牌", hashMap, pipoker);
}
public static void main(String name, HashMap<Integer, String> hm, TreeSet<Integer> integers) {
System.out.print(name + "的牌是:");
for (Integer shu : integers) {
String s = hm.get(shu);
System.out.print(s + " ");
}
System.out.println();
}