这里写代码片 import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
public class Poker {
/*
* 用Hashmap创建牌盒
* 1存储发牌
* 2 装牌
* 3洗牌
* 4 发牌
* 5看牌
*
*/
public static void main(String[] args) {
HashMap<Integer, String> hezi=new HashMap<>();
ArrayList<Integer> list=new ArrayList<>();
//数字
String[] shu= {"3","4","5","6","7","8","9","10","J","Q","K""A","2"};
//花色
String[] se= {"♠","♡","♢","♣"};
int index=0;
//装牌
for(String s1:se) {
for(String s2:shu) {
hezi.put(index, s2.concat(s1));
list.add(index);
index++;
}
}
hezi.put(index,"小王");
list.add(index);
index++;
hezi.put(index, "大王");
list.add(index);
//洗牌
Collections.shuffle(list);//洗索引即可
//发牌
TreeSet<Integer> p1=new TreeSet<>();
TreeSet<Integer> p2=new TreeSet<>();
TreeSet<Integer> p3=new TreeSet<>();
TreeSet<Integer> dipai=new TreeSet<>();
for(int i=0;i<list.size();i++) {
if(i>=list.size()-3) {
dipai.add(list.get(i));
}else if(i%3==0) {
p1.add(list.get(i));
}else if(i%3==1) {
p2.add(list.get(i));
}else {
p3.add(list.get(i));
}
}
method("赵六", hezi, p1);
method("张三", hezi, p2);
method("王小二", hezi, p3);
method("底牌", hezi, dipai);
}
public static void method(String name,HashMap<Integer, String> hezi,TreeSet<Integer>list) {
System.out.println(name+"的牌为:");
for (Integer integer : list) {
String value=hezi.get(integer);
System.out.print(value+" ");
}
System.out.println();
}
}
java实现模拟斗地主洗牌和发牌并对牌进行排序
最新推荐文章于 2022-04-23 16:44:14 发布