通过模拟斗地主案例来练习集合的使用
结果预览:
每次发牌后,三位玩家的手牌是随机的并且已经按照手牌大小排完序,运行两次验证手牌的随机性。
马老师的牌还不错,芜湖~起飞
思路:
1.创建HashMap,键是编号,值是牌。
2.创建ArrayList,存储编号。
3.创建花色数组和点数数组。
4.从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号。
5.洗牌(洗的是编号),用Collections的shuffl()方法实现。
6.发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合。
7.定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
8.调用看牌方法
为了方便理解,我用图形的方式来描述下过程:
具体代码实现:
1.创建集合装扑克牌
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//创建HashMap集合 key是编号用Integer value是牌用String HashMap<Integer,String> hm= new HashMap<>(); //创建ArrayList集合用来存储编号 ArrayList<Integer> list= new ArrayList<>(); //创建花色数组和点数数组 String [] color={ "♠" , "♦" , "♥" , "♣" }; String [] number={ "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" }; //从0开始往HashMap集合里面存储编号,并存储对应的牌。同时往ArrayList集合里面存储编号 int index= 0 ; //增强For循环存储花色和点数 for (String num:number){ for (String col:color){
hm.put(index,col+num); list.add(index); index++; } } //52张牌存完了 还剩大小王 现在添加进去 hm.put(index, "小王" ); list.add(index); index++; hm.put(index, "大王" ); list.add(index);
//以上的操作实现了把54张扑克牌放入一个集合容器。 |
2.洗牌和发牌
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//洗牌(洗的是编号),用collections的shuffle()方法实现。 Collections.shuffle(list); //发牌 用TreeSet接收 用三位玩家名字命名 TreeSet<Integer> PDD= new TreeSet<>(); TreeSet<Integer> DaSiMa= new TreeSet<>(); TreeSet<Integer> LuBenWei= new TreeSet<>(); //三张底牌 TreeSet<Integer> finalCard= new TreeSet<>(); for ( int x= 0 ;x<list.size();x++){ //定义一个变量接收索引 int a= list.get(x); //最后三个索引 if (x>=list.size()- 3 ){ finalCard.add(a); } else if (x% 3 == 0 ){ PDD.add(a); } else if (x% 3 == 1 ){ DaSiMa.add(a); } else { LuBenWei.add(a); } } |
3.定义看牌方法
1 2 3 4 5 6 7 8 9 10 11 |
//定义看牌的方法(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌) public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm ){ System.out.print(name+ "的手牌为:" ); //遍历牌 就是遍历索引 for (Integer key:ts){
String poker = hm.get(key); System.out.print(poker+ " " ); } System.out.println(); } |
原码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
package 模拟斗地主; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.TreeSet; /*需求: 通过程序实现 斗地主过程中的洗牌,发牌和看牌功能,并且为了方便看牌手牌要排序。 思路: 1:创建HashMap集合,键是编号,值是牌。 2:创建Arraylist集合用于存储编号。 3:创建花色数组和点数数组。 4:从0开始往HashMap集合里面存储编号,并存储对应的牌。同时往ArrayList集合里面存储编号。 5 :洗牌(洗的是编号),用collections的shuffle()方法实现。 6:发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收 7:定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌) 8:调用方法看牌 */ public class ChinesePoker { public static void main(String[] args) { //创建HashMap集合 key是编号用Integer value是牌用String HashMap<Integer,String> hm= new HashMap<>(); //创建ArrayList集合用来存储编号 ArrayList<Integer> list= new ArrayList<>(); //创建花色数组和点数数组 String [] color={ "♠" , "♦" , "♥" , "♣" }; String [] number={ "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "J" , "Q" , "K" , "A" , "2" }; //从0开始往HashMap集合里面存储编号,并存储对应的牌。同时往ArrayList集合里面存储编号 int index= 0 ; //增强For循环存储花色和点数 for (String num:number){ for (String col:color){
hm.put(index,col+num); list.add(index); index++; } } //52张牌存完了 还剩大小王 现在添加进去 hm.put(index, "小王" ); list.add(index); index++; hm.put(index, "大王" ); list.add(index); //洗牌(洗的是编号),用collections的shuffle()方法实现。 Collections.shuffle(list); //发牌 用TreeSet接收 用三位玩家名字命名 TreeSet<Integer> PDD= new TreeSet<>(); TreeSet<Integer> DaSiMa= new TreeSet<>(); TreeSet<Integer> LuBenWei= new TreeSet<>(); //三张底牌 TreeSet<Integer> finalCard= new TreeSet<>(); for ( int x= 0 ;x<list.size();x++){ //定义一个变量接收索引 int a= list.get(x); //最后三个索引 if (x>=list.size()- 3 ){ finalCard.add(a); } else if (x% 3 == 0 ){ PDD.add(a); } else if (x% 3 == 1 ){ DaSiMa.add(a); } else { LuBenWei.add(a); } } //调用看牌方法 lookPoker( "PDD" ,PDD,hm); lookPoker( "大司马" ,DaSiMa,hm); lookPoker( "卢本伟" ,LuBenWei,hm); lookPoker( "底牌" ,finalCard,hm); }
//定义看牌的方法(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌) public static void lookPoker(String name,TreeSet<Integer> ts,HashMap<Integer,String> hm ){ System.out.print(name+ "的手牌为:" ); //遍历牌 就是遍历索引 for (Integer key:ts){
String poker = hm.get(key); System.out.print(poker+ " " ); } System.out.println(); } } |
以上就是使用JavaSE来模拟斗地主的详细内容,更多关于JavaSE斗地主的资料请关注其它相关文章!
原文链接:https://blog.csdn.net/JiaMing11_27/article/details/115480083