In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章给大家分享的是有关如何使用java集合模拟实现斗地主洗牌和发牌的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
1:用ArrayList集合实现
案例分析:
我们现在要做的是模拟斗地主小游戏来进行洗牌和发牌。
具体步骤:
A:首先,创建一个ArrayList集合来存储牌,将牌的花色和点数存储在俩个字符串数组中中,然后用for循环加强遍历将每张牌的花色点数及大王小王存储在集合中。
B:进行洗牌,调用collectionas的shuffle方法。
C:进行发牌,创建四个ArrayList集合,分别用来存储玩家一,玩家二,玩家三,和底牌对应发到手上的牌。然后用for循环遍历这幅扑克牌用If语句判断,然后发牌。
D:创建一个看牌的方法,调用即可。
具体代码:
package com.study_01;import java.util.ArrayList;import java.util.Collections;import java.util.List;/* * 模拟斗地主洗牌和发牌 * 分析步骤: * 1:创建一个集合来存储牌 * 2:洗牌 * 3:发牌 * 4:看牌 * */public class Poker1 { public static void main(String[] args) { //创建一个集合来存牌 ArrayList Pokers=new ArrayList(); String[] colors={"♠","♥","♦","♣"}; String[] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; for(String color:colors){ for(String number:numbers){ Pokers.add(color+number); } Pokers.add("大王"); Pokers.add("小王"); }//洗牌 Collections.shuffle(Pokers); //发牌 /*创建四个集合来存储 * 1:张三 * 2:李四 * 3:王麻 * 4:底牌 */ ArrayList Zhangsan=new ArrayList(); ArrayList Lisi=new ArrayList(); ArrayList Wangma=new ArrayList(); ArrayList Dipai=new ArrayList(); for(int i=0;i=Pokers.size()-3){ Dipai.add(Pokers.get(i)); }else if(i%3==0){ Zhangsan.add(Pokers.get(i)); }else if(i%3==1){ Lisi.add(Pokers.get(i)); }else if(i%3==2){ Wangma.add(Pokers.get(i)); } }//看牌 lookPokers("张三",Zhangsan); lookPokers("李四",Lisi); lookPokers("王麻",Wangma); lookPokers("底牌",Dipai); } private static void lookPokers(String name, ArrayList Pokers){ System.out.println(name+"的牌是:"); for(String poker:Pokers){ System.out.print(poker+" "); }System.out.println(); } }
运行结果:
2:用TreeMap集合实现
具体步骤:
A:创建TreeMap集合存储牌,并且大的键对应大的牌,小的键对应小的牌,同上创建俩个数组,一个用来存储花色,一个用来存储点数,另外创建一个ArrayList集合,用来存储没有任意一张牌对应的索引。
B:.洗牌,洗的是索引(map集合中每一张牌对应的键),我们这里用的是ArrayList来存储。
C:发牌,发的索引,并且对这个索引进行排序,这是利用了TreeSet的有序性。
D:看牌,根据发到的索引去map集合中找出对应的值。
具体代码:
package com.study_01;import java.util.ArrayList;import java.util.Collections;import java.util.TreeMap;import java.util.TreeSet;/** * 1.创建Map集合存储牌,并且大的键对应大的牌,小的键对应小的牌 * 2.洗牌,洗的是索引(map集合中每一张牌对应的键) * 3.发牌,发的索引,并且岁这个索引进行排序 * 4.看牌,根据发到的键去map集合中找出对应的值 */public class Poker2 { public static void main(String[] args) { //创建一个TreeMap集合 TreeMap Pokers=new TreeMap(); String[] colors={"♥","♠","♦","♣"}; String[] numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2",}; //创建一个ArrayList集合,用来存储没有一张牌对应的索引 ArrayList indexs=new ArrayList(); int index = 0; for(String color:colors){ for(String number:numbers){ Pokers.put(index, color+number); indexs.add(index); index++; }//存大王,小王 Pokers.put(index, "大王"); indexs.add(index); index++; Pokers.put(index, "小王"); indexs.add(index); index++; }//洗牌,洗的是索引(map集合中每一张牌对应的键) Collections.shuffle(indexs); //发牌,发的是索引根据索引来找它对应的值 TreeSet zhangsan=new TreeSet(); TreeSet lisi=new TreeSet(); TreeSet wangma=new TreeSet(); TreeSet dipai=new TreeSet(); for(int i=0;i=Pokers.size()-3){ dipai.add(indexs.get(i)); }else if(i%3==0){ zhangsan.add(indexs.get(i)); }else if(i%3==1){ lisi.add(indexs.get(i)); }else if(i%3==2){ wangma.add(indexs.get(i)); } }//看牌 lookPoker("张三",zhangsan,Pokers); lookPoker("李四",lisi,Pokers); lookPoker("王麻",wangma,Pokers); lookPoker("底牌",dipai,Pokers); } private static void lookPoker(String name, TreeSet indexs, TreeMap Pokers) { System.out.println(name+"的牌是:"); for(Integer index:indexs){ System.out.print(Pokers.get(index)); }System.out.println(); }}
运行结果:
感谢各位的阅读!关于"如何使用java集合模拟实现斗地主洗牌和发牌"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.