286/287斗地主案例需求分析和代码实现
斗地主案例的需求分析
这个斗地主的案例和之前的斗地主案例多加了细节上的优化,
之前的斗地主,洗牌发牌,发到手里之后,牌很乱,顺序都是乱七八糟的
现在的斗地主,洗完牌,发完牌,发到手里之后,会自动帮你整理牌,变得有顺序
这是没顺序的,看着很难受
public static void main(String[] args) { //[1]创建三个集合:牌盒、花色、数字,直接用list.of(); String[] colors = {"♠","♣","♦","♥"}; String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"}; List<String> pokerBox = new ArrayList<>(); for (String color : colors) { for (String number : numbers) { pokerBox.add(color + number); } } pokerBox.add("大王"); pokerBox.add("小王"); Collections.shuffle(pokerBox);//洗牌操作 //[2]创建三个玩家和一个底牌集合。发牌操作 List<String> playOne = new ArrayList<>(); List<String> playTwo = new ArrayList<>(); List<String> playThree = new ArrayList<>(); List<String> cards = new ArrayList<>(); for (int i = 0; i < 54; i++) { if (i >= 51) { cards.add(pokerBox.get(i)); } else if (i % 3 == 0) { playOne.add(pokerBox.get(i)); } else if (i % 3 == 1) { playTwo.add(pokerBox.get(i)); } else { playThree.add(pokerBox.get(i)); } } System.out.println("底牌:" + cards); System.out.println("卢本伟:" + playOne); System.out.println("梁志超:" + playTwo); System.out.println("六舅:" + playThree); }
斗地主案例实现
public static void main(String[] args) {
//1.创造牌盒子,创建一个Map集合,存储牌的索引和组装好的牌
HashMap<Integer,String> poker = new HashMap<>();
//准备牌的索引盒子
ArrayList<Integer> pokerindex = new ArrayList<>();
//花色集合
ArrayList<String> colors = new ArrayList<>();
//数字集合
ArrayList<String> numbers = new ArrayList<>();
Collections.addAll(colors,"♠","♥","♣","♦");
Collections.addAll(numbers,"3","4","5","6","7","8","9","10","J","Q","K","A","2");
int index = 0;
//循环遍历两个字符串数组,将一整副牌准备好
for(String number:numbers){
for(String color:colors){
poker.put(index,color+number);
pokerindex.add(index);
index++;
}
}
//手动添加大王
poker.put(index,"大王");
pokerindex.add(index);
index++;
//手动添加小王
poker.put(index,"小王");
pokerindex.add(index);
index++;
// System.out.println(index);
//2.洗牌,将牌的索引打乱顺序,使用Collections中的方法shuffle(List)
Collections.shuffle(pokerindex);
// System.out.println(pokerindex);
//3.发牌,分别创建三个玩家的集合与底牌集合,来存储索引
ArrayList<Integer> player01 = new ArrayList<>();
ArrayList<Integer> player02 = new ArrayList<>();
ArrayList<Integer> player03 = new ArrayList<>();
ArrayList<Integer> dipai = new ArrayList<>();
//遍历存储牌索引的List集合,获取每一个牌的索引
for (Integer i = 0;i<pokerindex.size();i++){
Integer in = pokerindex.get(i);
if(i>=51){
dipai.add(in);
}else if(i%3==0){
player01.add(in);
}else if(i%3==1){
player02.add(in);
}else if(i%3==2){
player03.add(in);
}
}
//4.排序,使用Collections中的方法sort(List<T> list,comparator<? super T>)
//因为斗地主真实游戏中是降序排序,所以使用Comparator自定义方法来降序排序
// Collections.sort(dipai);
// Collections.sort(player01);
// Collections.sort(player02);
// Collections.sort(player03);
Collections.sort(dipai, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
Collections.sort(player01, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
Collections.sort(player02, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
Collections.sort(player03, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
//5.看牌,写一个看牌的方法,在此方法中,利用每个玩家牌盒中存储的key,去poker中获取对应的value值
Lookpoker("底牌",poker,dipai);
Lookpoker("地主",poker,player01);
Lookpoker("农民1",poker,player02);
Lookpoker("农民2",poker,player03);
}
public static void Lookpoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
System.out.print(name+":");
for(Integer key:list){
String value = poker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
这个是有顺序的,从右往左

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端