介绍
小编给大家分享一下Java高级应用之斗地主游戏的实现示例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!
斗地主综合案例,供大家参考,具体内容如下
运用HashMap, ArrayList,列表类实现斗地主综合案例,模拟斗地主游戏的随机发牌,并按照牌的大小和花色进行排列。
斗地主玩家每轮都有三个玩家,运用集合类中的洗牌()方法打乱一整幅扑克牌,利用取余原理将凑乱的牌发放给三个玩家,整副牌发完后的最后三张永一个ArrayList存储作为底牌。具体代码实现如下:
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import 并不知道;/* * *斗地主综合案例:有序版本 * 1 .准备牌 * 2 .洗牌 * 3 .发牌 * 4 .排序 * 5 .看牌 * */public class  DouDizhu02 { ,,,public static void main (String [], args), { ,,,,,,,//1。准备牌 ,,,,,,,//创建一个映射集合,存储牌的索引和组装好的牌 ,,,,,,,HashMap<整数,,String>, poker =, new HashMap<在(); ,,,,,,,//列表集合,存储牌的索引 ,,,,,,,ArrayList, pokerIndex =, new ArrayList<在(); ,,,,,,,//定义两个集合,存储花色和序号 ,,,,,,,List , colors =, List.of (“?“,“?“,,”?“,,”?“); ,,,,,,,List , numbers =, List.of (“2“,“A",,“K",,“Q",,“J",,“10“,,“9”,,“8”,,“7”,,“6”,,“5”,,“4”,“3”); ,,,,,,,//把大王和小王存储到集合中 ,,,,,,,int index =, 0; ,,,,,,,poker.put(指数,“大王“); ,,,,,,,pokerIndex.add(指数); ,,,,,,,指数+ +; ,,,,,,,poker.put(指数,“小王“); ,,,,,,,pokerIndex.add(指数); ,,,,,,,指数+ +; ,,,,,,,//循环嵌套52张牌 ,,,,,,,,(数量:String 数字){ ,,,,,,,,,,,,(String color :颜色){ ,,,,,,,,,,,,,,,poker.put(指数,color +,数字); ,,,,,,,,,,,,,,,pokerIndex.add(指数); ,,,,,,,,,,,,,,,指数+ +; ,,,,,,,,,,,} ,,,,,,,}//,,,,,,,,System.out.println(扑克牌);//,,,,,,,,System.out.println (pokerIndex); ,,,,,,/* ,,,,,,,2。洗牌 ,,,,,,,使用中集合的方法shuffle(列表) ,,,,,,,*/,,,,,,,Collections.shuffle (pokerIndex); ,,,,,,/* ,,,,,,,3。发牌 ,,,,,,,*/,,,,,,,//定义四个集合,存储玩家牌的索引,和底牌的索引 ,,,,,,,ArrayList , player01 =, new ArrayList<在(); ,,,,,,,ArrayList , player02 =, new ArrayList<在(); ,,,,,,,ArrayList , player03 =, new ArrayList<在(); ,,,,,,,ArrayList , dipai =, new ArrayList<在(); ,,,,,,,//遍历存储牌索引的列表集合,获取每一个牌的索引 ,,,,,,,for (int 小姐:=,0;,小姐:& lt;, pokerIndex.size();,我+ +),{ ,,,,,,,,,,,Integer 拷贝=,pokerIndex.get(我); ,,,,,,,,,,,如果(拷贝祝辞=,51){ ,,,,,,,,,,,,,,,dipai.add(的); ,,,,,,,,,,,}else 如果(小姐:%,3,==,0){ ,,,,,,,,,,,,,,,player01.add(的); null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null Java高级应用之斗地主游戏的实现示例