基于集合实现斗地主发牌功能
2023-03-20 14:49:21 时间
基于单列集合实现
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; } }
相关文章
- 金融服务领域的大数据:即时分析
- 影响大数据、机器学习和人工智能未来发展的8个因素
- 从0开始构建一个属于你自己的PHP框架
- 如何将Hadoop集成到工作流程中?这6个优秀实践必看
- SEO公司使用大数据优化其模型的5种方法
- 关于Web Workers你需要了解的七件事
- 深入理解HTTPS原理、过程与实践
- 增强分析:数据和分析的未来
- PHP协程实现过程详解
- AI专家:大数据知识图谱——实战经验总结
- 关于PHP的错误机制总结
- 利用数据分析量化协同过滤算法的两大常见难题
- 怎么做大数据工作流调度系统?大厂架构师一语点破!
- 2019大数据处理必备的十大工具,从Linux到架构师必修
- OpenCV中的KMeans算法介绍与应用
- 教大家如果搭建一套phpstorm+wamp+xdebug调试PHP的环境
- CentOS下三种PHP拓展安装方法
- Go语言HTTP Server源码分析
- Go语言HTTP Server源码分析
- 2017年4月编程语言排行榜:Hack首次进入前五十