• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Java高级应用之斗地主游戏

java 搞代码 4年前 (2022-01-05) 116次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了Java高级应用之斗地主游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

斗地主综合案例,供大家参考,具体内容如下

运用HashMap、ArrayList、List类实现斗地主综合案例,模拟斗地主游戏的随机发牌,并按照牌的大小和花色进行排列。

斗地主玩家每轮都有三个玩家,运用Collections类中的shuffle()方法打乱一整幅扑克牌,利用取余原理将凑乱的牌发放给三个玩家,整副牌发完后的最后三张永一个ArrayList存储作为底牌。具体代码实现如下:

 import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; /** *斗地主综合案例:有序版本 *1.准备牌 *2.洗牌 *3.发牌 *4.排序 *5.看牌 **/ public class DouDizhu02 { public static void main(String[] args) { //1。准备牌 //创建一个Map集合,存储牌的索引和组装好的牌 HashMap poker = new HashMap(); //List集合,存储牌的索引 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(index, "大王"); pokerIndex.add(index); index++; poker.put(index, "小王"); pokerIndex.add(index); index++; //循环嵌套52张牌 for(String number: numbers){ for(String color : colors){ poker.put(index, color + number); pokerIndex.add(index); index++; } } //        System.out.println(poker); //        System.out.println(pokerIndex); /* 2.洗牌 使用Collections中的方法shuffle(list) */ Collections.shuffle(pokerIndex); /* 3.发牌 */ //定义四个集合,存储玩家牌的索引,和底牌的索引 ArrayList player01 = new ArrayList(); ArrayList player02 = new ArrayList(); ArrayList player03 = new ArrayList(); ArrayList dipai = new ArrayList(); //遍历存储牌索引的list集合,获取每一个牌的索引 for (int i = 0; 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) */ Collections.sort(player01); Collections.sort(player02); Collections.sort(player03); Collections.sort(dipai); /* 5.看牌 调用看牌的方法 */ lookPoker("刘德华", poker, player01); lookPoker("周星驰", poker, player<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码*网</strong>02); lookPoker("周润发", poker, player03); lookPoker("底牌", poker, dipai); } /* 定义一个看牌的方法,提高代码的复用性 参数: String name:玩家名称 HashMap poker:存储牌的poker集合 ArrayList list:存储玩家和底牌的list集合 查表法: 遍历玩家或者底牌集合,获取牌的索引 使用牌的索引,去Map集合中找到对应的牌 */ public static void lookPoker(String name, HashMap poker, ArrayList list){ //输出玩家名称,不换行 System.out.print(name + " :"); for(int key : list){ String value = poker.get(key); System.out.print(value + " "); } System.out.println(); } }

运行结果如下

牌为随机打乱,每次运行结果都不一样。

 刘德华 :♥2 ♦2 ♠A ♦K ♠Q ♣Q ♥9 ♣9 ♥8 ♣8 ♦7 ♠7 ♣7 ♦6 ♣6 ♥5 ♠5 ♥3 周星驰 :♠2 ♥A ♦A ♣K ♦Q ♥J ♦J ♣J ♥10 ♦10 ♠10 ♦9 ♠9 ♦8 ♥7 ♠4 ♣4 周润发 :大王 小王 ♣2 ♣A ♥K ♠K ♥Q ♠J ♣10 ♠8 ♥6 ♠6 ♦5 ♣5 ♥4 ♦4 底牌 :♦3 ♠3 ♣3

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是Java高级应用之斗地主游戏的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Java高级应用之斗地主游戏

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址