基于集合实现斗地主发牌功能
基于单列集合实现
package com.demo03; import java.util.ArrayList; import java.util.Collections; public class doudizhu { public static void main(String[] args) { // System.out.println(createPoker()); ArrayList poker = createPoker();//先创建扑克牌 shufflePoker(poker);// 洗牌 // System.out.println(poker); sendPoker(poker); } /* 1 创建扑克牌 * */ public static ArrayList createPoker() { String[] mode = new String[]{"红桃", "方块", "梅花", "黑桃"}; String[] num = new String[]{"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"}; ArrayList<String> poker = new ArrayList<>(); for (String s : mode) { for (String s1 : num) { poker.add(s + ":" + s1); } } poker.add("小王"); poker.add("大王"); return poker; } /* * 2 洗牌 * */ public static void shufflePoker(ArrayList<String> poker) { Collections.shuffle(poker); } /* * 3 发牌 * */ public static void sendPoker(ArrayList<String> poker) { ArrayList<String> play01 = new ArrayList<>(); ArrayList<String> play02 = new ArrayList<>(); ArrayList<String> play03 = new ArrayList<>(); ArrayList<String> dipai = new ArrayList<>(); for (int i = 0; i < poker.size(); i++) { if (i >= 51) { dipai.add(poker.get(i)); } else if (i % 3 == 0) { play01.add(poker.get(i)); } else if (i % 3 == 1) { play02.add(poker.get(i)); } else if (i % 3 == 2) { play03.add(poker.get(i)); } } System.out.println("玩家1:" + play01); System.out.println("玩家2:" + play02); System.out.println("玩家3:" + play03); System.out.println("底牌:" + dipai); } }
基于双列集合实现
package com.demo03; import java.util.*; public class doudizhuplus { public static void main(String[] args) { //1 准备扑克牌 HashMap<Integer, String> map = new HashMap<Integer, String>();// 存储扑克牌的容器 ArrayList<Integer> pokerIndex = new ArrayList<>(); // 存储扑克牌索引的容器 List<String> colors = List.of("红桃", "方块", "梅花", "黑桃"); List<String> nums = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"); // 2 生成扑克 int count = 0; pokerIndex.add(count); map.put(count++, "大王"); pokerIndex.add(count); map.put(count++, "小王"); for (String num : nums) { for (String color : colors) { pokerIndex.add(count); map.put(count++, color + ":" + num); } } // 3 洗牌 Collections.shuffle(pokerIndex); // 4 发牌 ArrayList<Integer> play01 = new ArrayList<>(); ArrayList<Integer> play02 = new ArrayList<>(); ArrayList<Integer> play03 = new ArrayList<>(); ArrayList<Integer> dipai = new ArrayList<>(); for (int index = 0; index < pokerIndex.size(); index++) { if (index >= 51) { dipai.add(pokerIndex.get(index)); } else if (index % 3 == 0) { play01.add(pokerIndex.get(index)); } else if (index % 3 == 1) { play02.add(pokerIndex.get(index)); } else if (index % 3 == 2) { play03.add(pokerIndex.get(index)); } } // 5 看牌 ArrayList<String> poker_play01 = show(play01, map); ArrayList<String> poker_play02 = show(play02, map); ArrayList<String> poker_play03 = show(play03, map); ArrayList<String> poker_dipai = show(dipai, map); System.out.println("玩家1:" + poker_play01); System.out.println("玩家2:" + poker_play02); System.out.println("玩家3:" + poker_play03); System.out.println("底牌:" + poker_dipai); } public static ArrayList show(ArrayList<Integer> play, Map<Integer, String> map) { ArrayList<String> poker_play = new ArrayList<>(); Collections.sort(play); for (Integer index : play) { String item = map.get(index); poker_play.add(item); } return poker_play; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~