zoukankan      html  css  js  c++  java
  • 斗地主案例

    public class Demo01 {
    
        public static void main(String[] args) {
        //创建装有牌的Map集合
            HashMap<Integer,String> pooker=new HashMap<Integer,String>();
            //创建装有牌号的ArrayList集合
            ArrayList<Integer> pookerNum=new ArrayList<Integer>();
            //封装花色和号码
            String color[]={"黑桃","红心","菱形","梅花"};
            String number[]={"2","A","K","Q","J","10","9","8"
                    ,"7","6","5","4","3"        };
            int index=2;
            for(String num:number){
                for(String col:color){
                    //封装Map集合
                    pooker.put(index,col+num);
                    //将key值封装到list集合中
                    pookerNum.add(index);
                    index++;
                }
            }
            //封装大小王
            pooker.put(0, "大王");
            pooker.put(1, "小王");
            pookerNum.add(0);
            pookerNum.add(1);
            //洗牌
            Collections.shuffle(pookerNum);
            //创建三个玩家一个底牌
            ArrayList<Integer> player1=new ArrayList<Integer>();
            ArrayList<Integer> player2=new ArrayList<Integer>();
            ArrayList<Integer> player3=new ArrayList<Integer>();
            ArrayList<Integer> bottom=new ArrayList<Integer>();
            //发牌
            for(int i=0;i<pookerNum.size();i++){
                //先将前三张牌发给底牌
                if(i<3){
                    bottom.add(pookerNum.get(i));
                }else if(i%3==0){//发给玩家1
                    player1.add(pookerNum.get(i));
                }else if(i%3==1){
                    player2.add(pookerNum.get(i));
                }else if(i%3==2){
                    player3.add(pookerNum.get(i));
                }
            }
            //排序
            Collections.sort(player1);
            Collections.sort(player2);
            Collections.sort(player3);
            Collections.sort(bottom);
            //看牌
            look("渣渣灰",pooker,player1);
            look("刘嘉玲",pooker,player2);
            look("林志炫",pooker,player3);
            look("底牌",pooker,bottom);
        }
    //写一个方法遍历集合看牌
        public static void look(String name,HashMap<Integer,String> pooker
                ,ArrayList<Integer> player){
            System.out.print(name+":");
            //遍历玩家集合牌号
            for(int key:player){
                //根据每一个牌号取map集合中找对应的值
                System.out.print(pooker.get(key)+" ");
            }
            System.out.println();
        }



  • 相关阅读:
    浏览器渲染原理
    React Router
    链式 add 函数
    函数防抖和函数节流
    242. 有效的字母异位词
    faker 生成模拟数据
    A 第五课 二叉树与图
    使用递归解决问题
    A 第四课 递归_回溯_分治
    A 第三课 贪心算法
  • 原文地址:https://www.cnblogs.com/maxuefeng/p/13828368.html
Copyright © 2011-2022 走看看