通过模拟斗地主案例来练习集合的使用

结果预览:

每次发牌后,三位玩家的手牌是随机的并且已经按照手牌大小排完序,运行两次验证手牌的随机性。

马老师的牌还不错,芜湖~起飞

思路:

1.创建hashmap,键是编号,值是牌。
2.创建arraylist,存储编号。
3.创建花色数组和点数数组。
4.从0开始往hashmap里面存储编号,并存储对应的牌。同时往arraylist里面存储编号。
5.洗牌(洗的是编号),用collections的shuffl()方法实现。
6.发牌(发的也是编号,为了保证编号是排序的,创建treeset集合。
7.定义方法看牌(遍历treeset集合,获取编号,到hashmap集合找对应的牌)
8.调用看牌方法

为了方便理解,我用图形的方式来描述下过程:

具体代码实现:

1.创建集合装扑克牌

 
//创建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.洗牌和发牌

   //洗牌(洗的是编号),用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.定义看牌方法

      //定义看牌的方法(遍历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();
        }

原码:

    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斗地主的资料请关注www.887551.com其它相关文章!