zoukankan      html  css  js  c++  java
  • 策略模式

    1.定义

      策略模式定义了一族算法,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。

    2.适用性

      1.许多相关的类仅仅是行为有异。“策略”提供了一种用多个行为中的一个行为来配置一个类的方法。

      2.需要使用一个算法的不同变体。

      3.算法使用客户不应该知道的数据。可使用策略模式以避免暴露复杂的、与算法相关的数据结构。

      4.一个类定义了多种行为,并且这些行为在这个类的操作中以多个条件语句的形式出现。将相关的条件分支移入它们各自的Strategy类中以代替这些条件语句。

    3.例子

    package com.handmobi.gameServer.majiang.guanganmj.jinbi;
    
    import com.handmobi.message.CommonMessage;
    import com.handmobi.message.majiangMessage.GuangAnMessage;
    
    import java.util.*;
    
    /**
     * Created by a on 2017/4/1.
     */
    public class GuanganMachine {
    
    
        public static CommonMessage.RobotAction machineTile(List<Integer> ownerlist, int missType,
                               List<CommonMessage.Group> groups, GuanganBattleRoom room,
                               int index, int ownIndex) {
            boolean flag = false;
            boolean pgflag = true;
            boolean qingQiDuiFlag = false;
            if(room.drawL == index) {
                flag = true;
            }
            if(index == -1) {
                pgflag = false;
                flag = true;
            }
            if(room.huMap.containsKey(index) && !checkOperator(room, index) && flag) {
                return setRobotAction(CommonMessage.Action.Discard, null, room.currDrawTile);
            }
    
            List<Integer> newList = new ArrayList<>();
            newList.addAll(ownerlist);
            Collections.sort(newList);
    
            List<CommonMessage.Group> newGroups = new ArrayList<>();
            if(groups != null) {
                newGroups.addAll(groups);
            }
    
            //手中的缺牌
            List<Integer> missList = getMiss(newList, missType);
    //        List<Integer> singleFeng = getSingleFeng(newList) ;
            if(!checkOperator(room, index)) {
                //先打缺牌
                if (missList != null && missList.size() > 0 && flag) {
                    return setRobotAction(CommonMessage.Action.Discard, null, missList.get(missList.size() - 1));
                }
    /*            if(singleFeng != null && singleFeng.size() > 0 && flag){
                    return setRobotAction(CommonMessage.Action.Discard, null, singleFeng.get(singleFeng.size() - 1));
                }*/
            }
            if(missList != null) {
                newList.removeAll(missList);
            }
            //字牌
            List<Integer> ziList = getZiList(newList);
            //获取字牌的数量
            Map<Integer, Integer> ziMap = getMap(ziList);
            //单张字牌的集合
            List<Integer> oneZi = getOneMJ(ziMap);
            //二张字牌的集合
            List<Integer> twoZi = getTwoMJ(ziMap);
            //三张字牌的集合
            List<Integer> threeZi = getThreeMJ(ziMap);
            newList.removeAll(ziList); //删掉字牌
    
            //获取每张牌的数量(除去字牌)
            Map<Integer, Integer> mapMJ = getMap(newList);
            //单张的集合
            List<Integer> oneMJ = getOneMJ(mapMJ);
            //两张的集合
            List<Integer> twoMJ = getTwoMJ(mapMJ);
            //三张的集合
            List<Integer> threeMJ = getThreeMJ(mapMJ);
            //顺子
            List<Integer> shunList = getShunList(newList);
    
            //除去顺子
            getAllShunList(newList, shunList);
            //半顺集合
            List<Integer> banShunList = getBanShunList(newList, shunList);
    
            List<Integer> firstOneMJ = new ArrayList<>();
            List<Integer> secondOneMJ = new ArrayList<>();
    
            List<Integer> firstTwoMJ = new ArrayList<>();
            List<Integer> secondTwoMJ = new ArrayList<>();
    
            List<Integer> firstThreeMJ = new ArrayList<>();
            List<Integer> secondThreeMJ = new ArrayList<>();
    
    
            int firstType = getType(oneMJ, twoMJ, threeMJ);
    
            if(oneMJ.size() > 0) {
                for(int i : oneMJ) {
                    if(i / 10 == firstType) {
                        firstOneMJ.add(i);
                    }else {
                        secondOneMJ.add(i);
                    }
                }
            }
    
            if(twoMJ.size() > 0) {
                for(int i : twoMJ) {
                    if(i / 10 == firstType) {
                        firstTwoMJ.add(i);
                    }else {
                        secondTwoMJ.add(i);
                    }
                }
            }
    
            if(threeMJ.size() > 0) {
                for(int i : threeMJ) {
                    if( i / 10 == firstType) {
                        firstThreeMJ.add(i);
                    }else {
                        secondThreeMJ.add(i);
                    }
                }
            }
    
            int type1 = getType(firstOneMJ, firstTwoMJ, firstThreeMJ);
            int type2 = getType(secondOneMJ, secondTwoMJ, secondThreeMJ);
    
    
            List<Integer> firstSingleMJ = getSingleMJ(firstOneMJ, shunList, banShunList);
            List<Integer> secondSingleMJ = getSingleMJ(secondOneMJ, shunList, banShunList);
    
            boolean huFlag = true;
    
            if(room.getTileNum() > 20) {
    
                if(newList.size() <= 4 && twoMJ.size() + twoZi.size() == 2
                        || !isAllSameType(newList) && isQingYiSe(newList) ) {
                    huFlag = false;
                }
                if(checkOtherAction(room, index)) {
                    CommonMessage.Group group = getHuGroup(room, index);
                    //int type = group.getHuType();
                    int tile = group.getTileUnitList().get(0).getTile(0);
                    int ke = GuanganmjServiceImpl.getKe(room.gaTingMap.get(index).get(tile), true);
                    List<Integer> list = new ArrayList<>();
                    list.addAll(newList);
                    list.addAll(ziList);
                    removeTile(list, tile, 2);
                    Map<Integer, Map<Integer,  Map<Integer, Integer>>> map1 = Util.judgeTing(list, newGroups, index, room);
                    if(map1 != null && map1.size() > 0) {
                        removeTile(list, sort(map1, room), 1);
                        Map<Integer,  Map<Integer, Integer>> map2 = HuUntil.judgeTing(list, newGroups, index, room);
                        if(map2 != null && map2.size() > 0) {
                            int ke2 = sortMap(map2);
                            if (ke2 > ke) {
                                huFlag = false;
                            }
                        }
                    }
                }
            }
    
            //只能胡
            if(checkOnlyHu(room, index)) {
                CommonMessage.RobotAction hu = checkHu(room, index, huFlag);
                if (hu != null) {
                    return hu;
                }
            }
    
    
            if(huFlag) {
                CommonMessage.RobotAction hu = checkHu(room, index, huFlag);
                if (hu != null) {
                    return hu;
                }
            }
    
    
            if(!checkOperator(room, index)) {
                if(isQingYiSe(newList) && !isAllSameType(newList)) {
    
                }else {
                    Map<Integer, Map<Integer,  Map<Integer, Integer>>> map = null;
                    if (ownIndex != 0) {
                        map = Util.judgeTing(newList, newGroups, ownIndex, room);
                    } else {
                        map = Util.judgeTing(newList, newGroups, index, room);
                    }
                    if (map != null && map.size() > 0) {
                        return setRobotAction(CommonMessage.Action.Discard, null, sort(map, room));
                    }
                }
            }
    
            ownIndex = 0;
    
            if(checkHunYiSe(newList, newGroups, type1, ziList)) {
                boolean cqys = checkQingYiSe(newList, newGroups, type1, ziList);
                if(!isAllSameType(newList, type1)) {
                    if(room.getTileNum() > 20) {
                        pgflag = false;
                    }
                    if(index != -1) {
                        if(check(room, index, type1)) {
                            pgflag = true;
                        }
                        if(firstTwoMJ.size() + firstThreeMJ.size() + twoZi.size()>= 5 && newGroups.size() == 0) {
                            pgflag = false;
                            qingQiDuiFlag = true;
                        }
                        CommonMessage.RobotAction gang = checkGang(room, index, pgflag);
                        if (gang != null) {
                            return gang;
                        }
                        CommonMessage.RobotAction peng = checkPeng(room, index, pgflag, newList, missType, newGroups, missList, ziList);
                        if (peng != null) {
                            return peng;
                        }
                    }
    
                    //先打单张的风牌
                    if(oneZi.size() >0 ){
                        return setRobotAction(CommonMessage.Action.Discard, null, oneZi.get(0));
                    }
    
                    if(secondSingleMJ.size() > 0) {
                        return setRobotAction(CommonMessage.Action.Discard, null, secondSingleMJ.get(0));
                    }
                    if(secondOneMJ.size() > 0) {
                        for(int i : secondOneMJ) {
                            if(!shunList.contains(i)) {
                                return setRobotAction(CommonMessage.Action.Discard, null, i);
                            }
                        }
                        for(int i : secondOneMJ) {
                            if(banShunList.contains(i) && secondTwoMJ.contains(i)) {
                                return setRobotAction(CommonMessage.Action.Discard, null, i);
                            }
                        }
                    }
                    if(secondTwoMJ.size() > 0) {
                        for(int i : secondTwoMJ) {
                            if(shunList.contains(i)) {
                                return setRobotAction(CommonMessage.Action.Discard, null, i);
                            }
                        }
                        return setRobotAction(CommonMessage.Action.Discard, null, secondTwoMJ.get(0));
                    }
                }else {
                    if(index != -1) {
                        pgflag = true;
                        if (firstTwoMJ.size() + firstThreeMJ.size() + twoZi.size()>= 5 && newGroups.size() == 0) {
                            pgflag = false;
                            qingQiDuiFlag = true;
                        }
                        CommonMessage.RobotAction gang = checkGang(room, index, pgflag);
                        if (gang != null) {
                            return gang;
                        }
                        CommonMessage.RobotAction peng = checkPeng(room, index, pgflag, newList, missType, newGroups, missList, ziList);
                        if (peng != null) {
                            return peng;
                        }
                    }
                    if(qingQiDuiFlag) {
                        if(oneZi.size() > 0) {
                            for(int i : oneZi) {
                                if(isJueMJ(i, room)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                        }
                        if(firstOneMJ.size() > 0) {
                            for(int i : firstOneMJ) {
                                if(isJueMJ(i, room)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                        }
                        if(threeZi.size() > 0) {
                            for(int i : threeZi) {
                                if(isJueMJ(i, room)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                        }
                        if(firstThreeMJ.size() > 0) {
                            for(int i : firstThreeMJ) {
                                if(isJueMJ(i, room)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                        }
                        if(oneZi.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, oneZi.get(0));
                        }
                        if(firstOneMJ.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, firstOneMJ.get(0));
                        }
                        if(threeZi.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, threeZi.get(0));
                        }
                        if(firstThreeMJ.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, firstThreeMJ.get(0));
                        }
                    }else {
                        if(cqys) {
                            if(oneZi.size() > 0) {
                                return setRobotAction(CommonMessage.Action.Discard, null, oneZi.get(0));
                            }
                            if(twoZi.size() > 0) {
                                return setRobotAction(CommonMessage.Action.Discard, null, twoZi.get(0));
                            }
                            if(threeZi.size() > 0) {
                                return setRobotAction(CommonMessage.Action.Discard, null, threeZi.get(0));
                            }
                        }
                        if(firstSingleMJ.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, firstSingleMJ.get(0));
                        }
                        if(oneZi.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, oneZi.get(0));
                        }
                        if(firstOneMJ.size() > 0) {
                            for(int i : firstOneMJ) {
                                if(!shunList.contains(i)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                            for(int i : firstOneMJ) {
                                if(banShunList.contains(i) && firstTwoMJ.contains(i)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                        }
                        if(twoZi.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, twoZi.get(0));
                        }
                        if(firstTwoMJ.size() > 0) {
                            for(int i : firstTwoMJ) {
                                if(shunList.contains(i)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                            return setRobotAction(CommonMessage.Action.Discard, null, firstTwoMJ.get(0));
                        }
                    }
                }
            }
    
            if(checkHunYiSe(newList, newGroups, type2, ziList)) {
                boolean cqys = checkQingYiSe(newList, newGroups, type2, ziList);
                if(!isAllSameType(newList, type2)) {
                    if(room.getTileNum() > 20) {
                        pgflag = false;
                    }
                    if(index != -1) {
                        if(check(room, index, type2)) {
                            pgflag = true;
                        }
                        if(secondTwoMJ.size() + secondThreeMJ.size() + twoZi.size() >= 5 && newGroups.size() == 0) {
                            pgflag = false;
                            qingQiDuiFlag = true;
                        }
                        CommonMessage.RobotAction gang = checkGang(room, index, pgflag);
                        if (gang != null) {
                            return gang;
                        }
                        CommonMessage.RobotAction peng = checkPeng(room, index, pgflag, newList, missType, newGroups, missList, ziList);
                        if (peng != null) {
                            return peng;
                        }
                    }
    
                    if(oneZi.size() >0 ){
                        return setRobotAction(CommonMessage.Action.Discard, null, oneZi.get(0));
                    }
    
                    if(firstSingleMJ.size() > 0) {
                        return setRobotAction(CommonMessage.Action.Discard, null, firstSingleMJ.get(0));
                    }
                    if(firstOneMJ.size() > 0) {
                        for(int i : firstOneMJ) {
                            if(!shunList.contains(i)) {
                                return setRobotAction(CommonMessage.Action.Discard, null, i);
                            }
                        }
                        for(int i : firstOneMJ) {
                            if(banShunList.contains(i) && firstTwoMJ.contains(i)) {
                                return setRobotAction(CommonMessage.Action.Discard, null, i);
                            }
                        }
                    }
                    if(firstTwoMJ.size() > 0) {
                        for(int i : firstTwoMJ) {
                            if(shunList.contains(i)) {
                                return setRobotAction(CommonMessage.Action.Discard, null, i);
                            }
                        }
                        return setRobotAction(CommonMessage.Action.Discard, null, firstTwoMJ.get(0));
                    }
                }else {
                    if(index != -1) {
                        pgflag = true;
                        if (secondTwoMJ.size() + secondThreeMJ.size() + twoZi.size() >= 5 && newGroups.size() == 0) {
                            pgflag = false;
                            qingQiDuiFlag = true;
                        }
                        CommonMessage.RobotAction gang = checkGang(room, index, pgflag);
                        if (gang != null) {
                            return gang;
                        }
                        CommonMessage.RobotAction peng = checkPeng(room, index, pgflag, newList, missType, newGroups, missList, ziList);
                        if (peng != null) {
                            return peng;
                        }
                    }
                    if(qingQiDuiFlag) {
                        if(oneZi.size() > 0) {
                            for(int i : oneZi) {
                                if(isJueMJ(i, room)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                        }
                        if(secondOneMJ.size() > 0) {
                            for(int i : secondOneMJ) {
                                if(isJueMJ(i, room)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                        }
                        if(threeZi.size() > 0) {
                            for(int i : threeZi) {
                                if(isJueMJ(i, room)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                        }
                        if(secondThreeMJ.size() > 0) {
                            for(int i : secondThreeMJ) {
                                if(isJueMJ(i, room)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                        }
                        if(oneZi.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, oneZi.get(0));
                        }
                        if(secondOneMJ.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, secondOneMJ.get(0));
                        }
                        if(threeZi.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, threeZi.get(0));
                        }
                        if(secondThreeMJ.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, secondThreeMJ.get(0));
                        }
                    }else {
                        if(cqys) {
                            if(oneZi.size() > 0) {
                                return setRobotAction(CommonMessage.Action.Discard, null, oneZi.get(0));
                            }
                            if(twoZi.size() > 0) {
                                return setRobotAction(CommonMessage.Action.Discard, null, twoZi.get(0));
                            }
                            if(threeZi.size() > 0) {
                                return setRobotAction(CommonMessage.Action.Discard, null, threeZi.get(0));
                            }
                        }
                        if(secondSingleMJ.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, secondSingleMJ.get(0));
                        }
                        if(oneZi.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, oneZi.get(0));
                        }
                        if(secondOneMJ.size() > 0) {
                            for(int i : secondOneMJ) {
                                if(!shunList.contains(i)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                            for(int i : secondOneMJ) {
                                if(banShunList.contains(i) && secondTwoMJ.contains(i)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                        }
                        if(twoZi.size() > 0) {
                            return setRobotAction(CommonMessage.Action.Discard, null, twoZi.get(0));
                        }
                        if(secondTwoMJ.size() > 0) {
                            for(int i : secondTwoMJ) {
                                if(shunList.contains(i)) {
                                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                                }
                            }
                            return setRobotAction(CommonMessage.Action.Discard, null, secondTwoMJ.get(0));
                        }
                    }
                }
            }
    
            if(firstTwoMJ.size() + secondTwoMJ.size() + twoZi.size() >= 5 && index != -1) {
                pgflag = false;
                CommonMessage.RobotAction gang = checkGang(room, index, pgflag);
                if(gang != null) {
                    return gang;
                }
                CommonMessage.RobotAction peng = checkPeng(room, index, pgflag, newList, missType, newGroups, missList, ziList);
                if(peng != null) {
                    return peng;
                }
                int discardTile = getDuiDiscardTile(firstOneMJ, secondOneMJ, firstThreeMJ, secondThreeMJ, room, oneZi, twoZi, threeZi);
                return setRobotAction(CommonMessage.Action.Discard, null, discardTile);
            }
    
            if(index != -1) {
                CommonMessage.RobotAction gang = checkGang(room, index, pgflag);
                if (gang != null) {
                    return gang;
                }
                CommonMessage.RobotAction peng = checkPeng(room, index, pgflag, newList, missType, newGroups, missList, ziList);
                if (peng != null) {
                    return peng;
                }
            }
    
            if(oneZi.size() > 0) {
                return setRobotAction(CommonMessage.Action.Discard, null, oneZi.get(0));
            }
    
            if (firstSingleMJ.size() > 0) {
                return setRobotAction(CommonMessage.Action.Discard, null, firstSingleMJ.get(0));
            }
    
            if (secondSingleMJ.size() > 0) {
                return setRobotAction(CommonMessage.Action.Discard, null, secondSingleMJ.get(0));
            }
    
            for (int i : firstOneMJ) {
                if (!shunList.contains(i)) {
                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                }
            }
    
            if(twoZi.size() > 0) {
                return setRobotAction(CommonMessage.Action.Discard, null, twoZi.get(0));
            }
    
            for (int i : secondOneMJ) {
                if (!shunList.contains(i)) {
                    return setRobotAction(CommonMessage.Action.Discard, null, i);
                }
            }
            if (firstTwoMJ.size() > 0) {
                for (int i : firstTwoMJ) {
                    if (shunList.contains(i)) {
                        return setRobotAction(CommonMessage.Action.Discard, null, i);
                    }
                    if (isJueMJ(i, room)) {
                        return setRobotAction(CommonMessage.Action.Discard, null, i);
                    }
                }
                return setRobotAction(CommonMessage.Action.Discard, null, firstTwoMJ.get(0));
            }
            if (secondTwoMJ.size() > 0) {
                for (int i : secondTwoMJ) {
                    if (shunList.contains(i)) {
                        return setRobotAction(CommonMessage.Action.Discard, null, i);
                    }
                    if (isJueMJ(i, room)) {
                        return setRobotAction(CommonMessage.Action.Discard, null, i);
                    }
                }
                return setRobotAction(CommonMessage.Action.Discard, null, secondTwoMJ.get(0));
            }
            if(firstOneMJ.size() > 0) {
                return setRobotAction(CommonMessage.Action.Discard, null, firstOneMJ.get(0));
            }
            if(secondOneMJ.size() > 0) {
                return setRobotAction(CommonMessage.Action.Discard, null, secondOneMJ.get(0));
            }
    
            if(threeZi.size() > 0) {
                return setRobotAction(CommonMessage.Action.Discard, null, threeZi.get(0));
            }
    
            return setRobotAction(CommonMessage.Action.Discard, null, newList.get(newList.size() - 1));
        }
    
        /**
         * 获取单张的风牌
         * @param newList 手牌
         * @return List<Integer>
         */
        private static List<Integer> getSingleFeng(List<Integer> newList) {
            List<Integer> result = new ArrayList<>() ;
            Map<Integer, Integer> tempMap = GuanganmjServiceImpl.getTempMap(newList);
            Set<Map.Entry<Integer, Integer>> entries = tempMap.entrySet();
            Iterator<Map.Entry<Integer, Integer>> it = entries.iterator();
            while(it.hasNext()){
                Map.Entry<Integer, Integer> entry = it.next();
                if(entry.getKey()>=35 && entry.getKey() <= 38 && entry.getValue() == 1){
                    result.add(entry.getKey()) ;
                }
                it.remove();
            }
            return result;
        }
    
        private static List<Integer> getMiss(List<Integer> newList, int missType) {
            List<Integer> missList = new ArrayList<>();
            for(int i = 0; i < newList.size(); i++) {
                if(newList.get(i) / 10 == missType - 1) {
                    missList.add(newList.get(i));
                }
            }
            if(missList.size() != 0) {
                return missList;
            }
            return null;
        }
    
        /**
         * 删除牌
         * @param list List
         * @param tile 要删除的牌
         * @param num 删除的数量
         */
        private static void removeTile(List<Integer> list, int tile, int num) {
            Iterator<Integer> iterator = list.iterator();
            while (iterator.hasNext() && num > 0) {
                if(iterator.next() == tile) {
                    iterator.remove();
                    num--;
                }
            }
        }
    
        private static boolean isSameType(int tile1, int tile2) {
            if(tile1 % 10 == tile2 % 10) {
                return true;
            }
            return false;
        }
    
        private static boolean isSameType(int tile1, int tile2, int tile3) {
            if(tile1 / 10 == tile2 / 10 && tile2 / 10 == tile3 / 10) {
                return true;
            }
            return false;
        }
    
        private static boolean isAllSameType(List<Integer> ownerList, int type) {
            for(int i : ownerList) {
                if(i / 10 != type) {
                    return false;
                }
            }
            return true;
        }
    
        /**
         * 获取顺子集合
         * @param ownerList
         * @return
         */
        private static List<Integer> getShunList(List<Integer> ownerList) {
            List<Integer> newList = new ArrayList<>();
            newList.addAll(ownerList);
            Set<Integer> set = new HashSet<>(newList);
            newList.clear();
            newList.addAll(set);
            Collections.sort(newList);
            List<Integer> shunList = new ArrayList<>();
            for(int i = 0; i < newList.size() - 2; i++) {
                if(newList.get(i) + 1 == newList.get(i + 1) && newList.get(i + 1) + 1 == newList.get(i + 2)
                        && isSameType(newList.get(i), newList.get(i + 1), newList.get(i + 2))) {
                    shunList.add(newList.get(i));
                    shunList.add(newList.get(i + 1));
                    shunList.add(newList.get(i + 2));
                    i += 2;
                }
            }
            return shunList;
        }
    
        /**
         * 半顺
         * @param ownerList
         * @param shunList
         * @return
         */
        private static List<Integer> getBanShunList(List<Integer> ownerList, List<Integer> shunList) {
            List<Integer> newList = new ArrayList<>();
            newList.addAll(ownerList);
            for(int i : shunList) {
                removeTile(newList, i, 1);
            }
    
            Set<Integer> set = new HashSet<>(newList);
            newList.clear();
            newList.addAll(set);
            Collections.sort(newList);
            List<Integer> banShunList = new ArrayList<>();
            for(int i = 0; i < newList.size() - 2; i++) {
                //如果构成半顺,并且是同一类型,并且不形成顺子
                if(newList.get(i) + 1 == newList.get(i + 1) && isSameType(newList.get(i), newList.get(i + 1))
                        && (newList.get(i + 1) + 1 != newList.get(i + 2)
                        || !isSameType(newList.get(i + 1), newList.get(i + 2))
                        || i + 2 >= newList.size())) {
                    banShunList.add(newList.get(i));
                    banShunList.add(newList.get(i + 1));
                    i += 1;
                }
            }
            if(newList.size() == 2) {
                if(newList.get(0) + 1 == newList.get(1) && isSameType(newList.get(0), newList.get(1))) {
                    banShunList.add(newList.get(0));
                    banShunList.add(newList.get(1));
                }
            }
            return banShunList;
        }
    
        private static Map<Integer, Integer> getMap(List<Integer> ownerList) {
            Map<Integer, Integer> map = new HashMap<>();
            for(int i : ownerList) {
                Integer count = map.get(i);
                if(count == null) {
                    count = 0;
                }
                count++;
                map.put(i, count);
            }
            return map;
        }
    
        private static List<Integer> getOneMJ(Map<Integer, Integer> map) {
            List<Integer> list = new ArrayList<>();
            for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
                if(entry.getValue() == 1) {
                    list.add(entry.getKey());
                }
            }
            return list;
        }
    
        private static List<Integer> getTwoMJ(Map<Integer, Integer> map) {
            List<Integer> list = new ArrayList<>();
            for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
                if(entry.getValue() == 2) {
                    list.add(entry.getKey());
                }
                if(entry.getValue() == 4) {
                    list.add(entry.getKey());
                    list.add(entry.getKey());
                }
            }
            return list;
        }
    
        private static List<Integer> getThreeMJ(Map<Integer, Integer> map) {
            List<Integer> list = new ArrayList<>();
            for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
                if(entry.getValue() == 3) {
                    list.add(entry.getKey());
                }
            }
            return list;
        }
    
        private static CommonMessage.RobotAction checkPeng(GuanganBattleRoom room, int index, boolean flag,
                                                           List<Integer> newList, int missType,
                                                           List<CommonMessage.Group> newGroups,
                                                           List<Integer> missList, List<Integer> ziList) {
            for(GuangAnMessage.GuangAnPlayerAction.Builder pa : room.actionQueue) {
                if (index == pa.getPlayerIndex()) {
                    CommonMessage.Group group = pa.getGroup();
                    if(group.getActionType() == CommonMessage.Action.Peng) {
                        CommonMessage.RobotAction.Builder builder = CommonMessage.RobotAction.newBuilder();
                        if(flag && room.getTileNum() != 0) {
                            builder.setActionType(CommonMessage.Action.Peng);
                            builder.setGroup(group);
                            if(ziList != null) {
                                newList.addAll(ziList);
                            }
                            removeTile(newList, group.getTileUnitList().get(0).getTile(0), 2);
                            newGroups.add(group);
                            if(missList != null) {
                                newList.addAll(missList);
                            }
                            int dTile = -1;
                            Map<Integer, Map<Integer, Map<Integer, Integer>>> map = null;
                            map = Util.judgeTing(newList, newGroups, index, room);
                            if(map != null && map.size() > 0) {
                                dTile = sort(map, room);
                            }else {
                                //ownIndex = index;
                                dTile = machineTile(newList, missType, newGroups, room, -1, index).getDiscardTile();
                            }
                            builder.setDiscardTile(dTile);
                            return builder.build();
                        }else {
                            builder.setActionType(CommonMessage.Action.Guo);
                            CommonMessage.Group.Builder gGroup = CommonMessage.Group.newBuilder();
                            gGroup.setActionType(CommonMessage.Action.Guo);
                            builder.setGroup(gGroup);
                            return builder.build();
                        }
                    }
                }
            }
            return null;
        }
    
        private static CommonMessage.RobotAction checkGang(GuanganBattleRoom room, int index, boolean flag) {
            for(GuangAnMessage.GuangAnPlayerAction.Builder pa : room.actionQueue) {
                if (index == pa.getPlayerIndex()) {
                    CommonMessage.Group group = pa.getGroup();
                    CommonMessage.RobotAction.Builder builder = CommonMessage.RobotAction.newBuilder();
                    if(group.getActionType() == CommonMessage.Action.AnGang || group.getActionType() ==
                            CommonMessage.Action.ZiGang || group.getActionType() == CommonMessage.Action.Gang) {
                        if (flag && room.getTileNum() != 0) {
                            builder.setActionType(group.getActionType());
                            builder.setGroup(group);
                            return builder.build();
                        } else {
                            builder.setActionType(CommonMessage.Action.Guo);
                            CommonMessage.Group.Builder gGroup = CommonMessage.Group.newBuilder();
                            gGroup.setActionType(CommonMessage.Action.Guo);
                            builder.setGroup(gGroup);
                            return builder.build();
                        }
                    }
                }
            }
            return null;
        }
    
        private static CommonMessage.RobotAction checkHu(GuanganBattleRoom room, int index, boolean flag) {
            for(GuangAnMessage.GuangAnPlayerAction.Builder pa : room.actionQueue) {
                if (index == pa.getPlayerIndex()) {
                    CommonMessage.Group group = pa.getGroup();
                    CommonMessage.RobotAction.Builder builder = CommonMessage.RobotAction.newBuilder();
                    if(group.getActionType() == CommonMessage.Action.Hu) {
                        if (flag) {
                            builder.setActionType(group.getActionType());
                            return builder.build();
                        }else {
                            builder.setActionType(CommonMessage.Action.Guo);
                            CommonMessage.Group.Builder gGroup = CommonMessage.Group.newBuilder();
                            gGroup.setActionType(CommonMessage.Action.Guo);
                            builder.setGroup(gGroup);
                            return builder.build();
                        }
                    }
                }
            }
            return null;
        }
    
        private static boolean check(GuanganBattleRoom room, int index, int type) {
            for(GuangAnMessage.GuangAnPlayerAction.Builder pa : room.actionQueue) {
                if (index == pa.getPlayerIndex()) {
                    CommonMessage.Group group = pa.getGroup();
                    if(group.getTileUnitList().get(0).getTile(0) / 10 == type ||
                            group.getTileUnitList().get(0).getTile(0) / 10 == 3) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        private static boolean checkZi(GuanganBattleRoom room, int index) {
            for(GuangAnMessage.GuangAnPlayerAction.Builder pa : room.actionQueue) {
                if (index == pa.getPlayerIndex()) {
                    CommonMessage.Group group = pa.getGroup();
                    if(group.getTileUnitList().get(0).getTile(0) / 10 == 3) {
                        return true;
                    }
                }
            }
            return false;
        }
    
        private static boolean checkOtherAction(GuanganBattleRoom room, int index) {
            List<Integer> list = new ArrayList<>();
            for(GuangAnMessage.GuangAnPlayerAction.Builder pa : room.actionQueue) {
                if (index == pa.getPlayerIndex()) {
                    CommonMessage.Group group = pa.getGroup();
                    if(group.getActionType() == CommonMessage.Action.Hu) {
                        list.add(1);
                    }
                    if(group.getActionType() == CommonMessage.Action.Peng) {
                        list.add(2);
                    }
                }
            }
            if(list.contains(1) && list.contains(2)) {
                return true;
            }
            return false;
        }
    
        private static boolean checkOnlyHu(GuanganBattleRoom room, int index) {
            int count = 0;
            CommonMessage.Group group = null;
            for(GuangAnMessage.GuangAnPlayerAction.Builder pa : room.actionQueue) {
                if(index == pa.getPlayerIndex()) {
                    count++;
                    group = pa.getGroup();
                }
            }
            if(count == 1 && group.getActionType() == CommonMessage.Action.Hu) {
                return true;
            }
            return false;
        }
    
        private static CommonMessage.Group getHuGroup(GuanganBattleRoom room, int index) {
            for(GuangAnMessage.GuangAnPlayerAction.Builder pa : room.actionQueue) {
                if (index == pa.getPlayerIndex()) {
                    CommonMessage.Group group = pa.getGroup();
                    if(group.getActionType() == CommonMessage.Action.Hu) {
                        return group;
                    }
                }
            }
            return null;
        }
    
        private static int getDuiDiscardTile(List<Integer> firstOneMJ, List<Integer> secondOneMJ,
                                             List<Integer> firstThreeMJ, List<Integer> secondThreeMJ,
                                             GuanganBattleRoom room, List<Integer> oneZi, List<Integer> twoZi,
                                             List<Integer> threeZi) {
            if(oneZi != null && oneZi.size() > 0) {
                for(int i : oneZi) {
                    if(isJueMJ(i, room)) {
                        return i;
                    }
                }
            }
            if(firstOneMJ != null && firstOneMJ.size() > 0 ) {
                for(int i : firstOneMJ) {
                    if(isJueMJ(i, room)) {
                        return i;
                    }
                }
            }
            if(secondOneMJ != null && secondOneMJ.size() > 0) {
                for(int i : secondOneMJ) {
                    if(isJueMJ(i, room)) {
                        return i;
                    }
                }
            }
            if(threeZi != null && threeZi.size() > 0) {
                for(int i : threeZi) {
                    if(isJueMJ(i, room)) {
                        return i;
                    }
                }
            }
            if(firstThreeMJ != null && firstThreeMJ.size() > 0) {
                for(int i : firstThreeMJ) {
                    if(isJueMJ(i, room)) {
                        return i;
                    }
                }
            }
            if(secondThreeMJ != null && secondThreeMJ.size() > 0) {
                for(int i : secondThreeMJ) {
                    if(isJueMJ(i, room)) {
                        return i;
                    }
                }
            }
            if(oneZi != null && oneZi.size() > 0) {
                return oneZi.get(0);
            }
            if(firstOneMJ != null && firstOneMJ.size() > 0) {
                return firstOneMJ.get(0);
            }
            if(secondOneMJ != null && secondOneMJ.size() > 0) {
                return secondOneMJ.get(0);
            }
            if(threeZi != null && threeZi.size() > 0) {
                return threeZi.get(0);
            }
            if(firstThreeMJ != null && firstThreeMJ.size() > 0) {
                return firstThreeMJ.get(0);
            }
            if(secondThreeMJ != null && secondThreeMJ.size() > 0) {
                return secondThreeMJ.get(0);
            }
            return -1;
        }
    
        /**
         * 是否绝张
         * @param tile 判断的牌
         * @param room 房间
         * @return true or false
         */
        private static boolean isJueMJ(int tile, GuanganBattleRoom room) {
            if(room.getSpecialTileNum(tile) == 0) {
                return true;
            }
            return false;
        }
    
        private static boolean checkQingYiSe(List<Integer> ownerList, List<CommonMessage.Group> groups, int type, List<Integer> ziList) {
            List<Integer> list = new ArrayList<>();
            list.addAll(ownerList);
            if(groups != null) {
                for (CommonMessage.Group group : groups) {
                    for (CommonMessage.TileUnit tileUnit : group.getTileUnitList()) {
                        list.addAll(tileUnit.getTileList());
                    }
                }
            }
            int count = 0;
            for(int i : list) {
                if(i / 10 == type) {
                    count++;
                }
            }
            if(count >= 9) {
                return true;
            }
            return false;
        }
    
        private static boolean checkHunYiSe(List<Integer> ownerList, List<CommonMessage.Group> groups, int type, List<Integer> ziList) {
            List<Integer> newTiles = new ArrayList<>();
            newTiles.addAll(ownerList);
            if(groups != null) {
                for (CommonMessage.Group group : groups) {
                    for (CommonMessage.TileUnit tileUnit : group.getTileUnitList()) {
                        newTiles.addAll(tileUnit.getTileList());
                    }
                }
            }
            int count = 0;
            for(int i : newTiles) {
                if(i / 10 == type) {
                    count++;
                }
            }
    
            //获取字牌的数量
            Map<Integer, Integer> ziMap = getMap(ziList);
            //二张字牌的集合
            List<Integer> twoZi = getTwoMJ(ziMap);
            //三张字牌的集合
            List<Integer> threeZi = getThreeMJ(ziMap);
            count =count + twoZi.size()*2 +threeZi.size()*3 ;
            if(count > 8) {
                return true;
            }
            return false;
        }
    
        private static int getType(List<Integer> oneList, List<Integer> twoList, List<Integer> threeList) {
            int type = -1;
            if(oneList.size() > 0) {
                return oneList.get(0) / 10;
            }
            if(twoList.size() > 0) {
                return twoList.get(0) / 10;
            }
            if(threeList.size() > 0) {
                return threeList.get(0) / 10;
            }
            return type;
        }
    
    
        private static List<Integer> getSingleMJ(List<Integer> oneMJ, List<Integer> shunList, List<Integer> banShunList) {
            List<Integer> list = new ArrayList<>();
            for(int i : oneMJ) {
                if(!shunList.contains(i) && !banShunList.contains(i)) {
                    list.add(i);
                }
            }
            return list;
        }
    
        /**
         * 获取所有的顺子并把顺子从牌中删除
         * @param list shoupai
         * @param shunList shunzi s
         */
        private static void getAllShunList(List<Integer> list, List<Integer> shunList) {
            List<Integer> newList = new ArrayList<>();
            newList.addAll(list);
            for(int i : shunList) {
                removeTile(newList, i, 1);
            }
            List<Integer> list1 = getShunList(newList);
            if(list1.size() > 0) {
                shunList.addAll(list1);
            }
            if(list1.size() != 0) {
                getAllShunList(list, shunList);
            }
        }
    
        private static CommonMessage.RobotAction setRobotAction(CommonMessage.Action action, CommonMessage.Group group, int tile) {
            CommonMessage.RobotAction.Builder builder = CommonMessage.RobotAction.newBuilder();
            builder.setActionType(action);
            if(group != null) {
                builder.setGroup(group);
            }
            builder.setDiscardTile(tile);
            return builder.build();
        }
    
        private static int sort(Map<Integer, Map<Integer, Map<Integer, Integer>>> map, GuanganBattleRoom room) {
            final GuanganBattleRoom tempRoom = room;
            List<Map.Entry<Integer, Map<Integer, Map<Integer, Integer>>>> list = new ArrayList<>(map.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<Integer, Map<Integer, Map<Integer, Integer>>>>() {
                @Override
                public int compare(Map.Entry<Integer, Map<Integer, Map<Integer, Integer>>> o1, Map.Entry<Integer, Map<Integer, Map<Integer, Integer>>> o2) {
                    int grade1 = 0;
                    int grade2 = 0;
                    for(Map.Entry<Integer, Map<Integer, Integer>> entry : o1.getValue().entrySet()) {
                        int ke = GuanganmjServiceImpl.getKe(entry.getValue(), true);
                        int num = tempRoom.getSpecialTileNum(entry.getKey());
                        grade1 += ke * num;
                    }
                    for(Map.Entry<Integer, Map<Integer, Integer>> entry : o2.getValue().entrySet()) {
                        int ke = GuanganmjServiceImpl.getKe(entry.getValue(), true);
                        int num = tempRoom.getSpecialTileNum(entry.getKey());
                        grade2 += ke * num;
                    }
                    return grade2 - grade1;
                }
            });
            return list.get(0).getKey();
        }
    
        private static boolean checkOperator(GuanganBattleRoom room, int index) {
            for(GuangAnMessage.GuangAnPlayerAction.Builder pa : room.actionQueue) {
                if (index == pa.getPlayerIndex()) {
                    return true;
                }
            }
            return false;
        }
    
        private static boolean isAllSameType(List<Integer> ownerList) {
            if(ownerList != null && ownerList.size() > 0) {
                int type = ownerList.get(0) / 10;
                for(int i : ownerList) {
                    if(i / 10 != type) {
                        return false;
                    }
                }
            }
            return true;
        }
    
        private static boolean isQingYiSe(List<Integer> ownerList) {
            if(ownerList.size() == 0) {
                return true;
            }
            int type = ownerList.get(0) / 10;
            int count1 = 0;
            int count2 = 0;
            for(int i : ownerList) {
                if(i / 10 == type) {
                    count1++;
                }else {
                    count2++;
                }
            }
            if(count1 >= 9 || count2 >= 9) {
                return true;
            }
            return false;
        }
    
        public static int sortMap(Map<Integer, Map<Integer, Integer>> oldMap) {
            ArrayList<Map.Entry<Integer, Map<Integer, Integer>>> list = new ArrayList<>(oldMap.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<Integer, Map<Integer, Integer>>>() {
                @Override
                public int compare(Map.Entry<Integer, Map<Integer, Integer>> o1, Map.Entry<Integer, Map<Integer, Integer>> o2) {
                    return GuanganmjServiceImpl.getKe(o1.getValue(), true) - GuanganmjServiceImpl.getKe(o2.getValue(), true);
                }
            });
            return GuanganmjServiceImpl.getKe(list.get(list.size() - 1).getValue(), true);
        }
    
        private static List<Integer> getZiList(List<Integer> ownerList) {
            List<Integer> list = new ArrayList<>();
            for(int i : ownerList) {
                if(i / 10 == 3) {
                    list.add(i);
                }
            }
            return list;
        }
    }
    

      

     

  • 相关阅读:
    等保2.0实施流程
    Python 使用xlsxwriter绘制Excel表格
    洞悉DAST、SAST、IAST -- Web应用安全测试技术对比浅谈
    CVE-2020-5398:Spring MVC_RFD
    Power Apps component framework (PCF) 手把手入门实例
    Kali: MSF meterpreter command
    Kali: VMware network disappear
    FlashFXP最新密钥-FlashFXP永久授权注册码分享
    简洁404页面源码 | 自适应404页面HTML好看的404源码下载
    mysql下的information与concat
  • 原文地址:https://www.cnblogs.com/dquery/p/7062093.html
Copyright © 2011-2022 走看看