其他分享
首页 > 其他分享> > 斗地主案例实现和异常概念&异常体系

斗地主案例实现和异常概念&异常体系

作者:互联网

斗地主案例实现

 public static void main(String[] args) {
        //1.创造牌盒子,创建一个Map集合,存储牌的索引和组装好的牌
        HashMap<Integer,String> poker = new HashMap<>();
        //准备牌的索引盒子
        ArrayList<Integer> pokerindex = new ArrayList<>();
        //花色集合
        ArrayList<String> colors = new ArrayList<>();
        //数字集合
        ArrayList<String> numbers = new ArrayList<>();

        Collections.addAll(colors,"♠","♥","♣","♦");
        Collections.addAll(numbers,"3","4","5","6","7","8","9","10","J","Q","K","A","2");

        int index = 0;

        //循环遍历两个字符串数组,将一整副牌准备好
        for(String number:numbers){
            for(String color:colors){
                poker.put(index,color+number);
                pokerindex.add(index);
                index++;
            }
        }
        //手动添加大王
        poker.put(index,"大王");
        pokerindex.add(index);
        index++;
        //手动添加小王
        poker.put(index,"小王");
        pokerindex.add(index);
        index++;
//        System.out.println(index);

        //2.洗牌,将牌的索引打乱顺序,使用Collections中的方法shuffle(List)
        Collections.shuffle(pokerindex);

//        System.out.println(pokerindex);
        //3.发牌,分别创建三个玩家的集合与底牌集合,来存储索引
        ArrayList<Integer> player01 = new ArrayList<>();
        ArrayList<Integer> player02 = new ArrayList<>();
        ArrayList<Integer> player03 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();

        //遍历存储牌索引的List集合,获取每一个牌的索引
        for (Integer i = 0;i<pokerindex.size();i++){
            Integer in = pokerindex.get(i);
            if(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<T> list,comparator<? super T>)
        //因为斗地主真实游戏中是降序排序,所以使用Comparator自定义方法来降序排序
//        Collections.sort(dipai);
//        Collections.sort(player01);
//        Collections.sort(player02);
//        Collections.sort(player03);
        Collections.sort(dipai, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        Collections.sort(player01, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        Collections.sort(player02, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        Collections.sort(player03, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        //5.看牌,写一个看牌的方法,在此方法中,利用每个玩家牌盒中存储的key,去poker中获取对应的value值
        Lookpoker("底牌",poker,dipai);
        Lookpoker("地主",poker,player01);
        Lookpoker("农民1",poker,player02);
        Lookpoker("农民2",poker,player03);
    }
    public static void Lookpoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){

        System.out.print(name+":");
        for(Integer key:list){
            String value = poker.get(key);
            System.out.print(value+" ");
        }
        System.out.println();
    }

这个是有顺序的,从右往左

 

 

异常概念&异常体系

异常的概念:

  异常,就是不正常的意思。在生活中:医生说.你的身体某个部位有异常,该部位和正常相比有点不同,该部位的功能将受影响

在程序中的意思就是︰

  异常∶指的是程序在执行过程中,出现的非正常的情况,最终会导致JVM的非正常停止。

  在Java等面向对象的编程语言中,异常本身是一个类,产生异常就是创建异常对象并抛出了一个异常对象。 Java处理异常的方式是中断处理。

异常的体系:

异常机制其实是帮助我们找到程序中的问题,异常的根类是java.lang.Throwable,其下有两个子类

:java.lang.Error与java.lang . Exception,

平常所说的异常指java.lang.Exception 。

 

 

Throwable体系:
  Error:严重错误Error,无法通过处理的错误,只能事先避免,好比绝症。
  Exception:表示异常,异常产生后程序员可以通过代码的方式纠正,使程序继续运行,是必须要处理的。好比感冒、阑尾炎。

标签:index,poker,斗地主,ArrayList,案例,Collections,Integer,new,异常
来源: https://www.cnblogs.com/yuzong/p/16452386.html