m0_65749592 2024-07-02 16:55 采纳率: 34.4%
浏览 1

我这部分代码我想优化一下可以怎么优化 帮我改一下

    我这部分代码我想优化一下可以怎么优化 帮我改一下List<SlotGameLineData> list = getAdjacentMaterialList(matrix);
    if (list.size() > 0) {
        for (SlotGameLineData data : list) {
            dataList = new ArrayList<SlotGameLineData>(list);
            String[] posArr = data.getMaterialPos().split(",");
            int countOnes = 0; // 记录一条连线有多少个  万能
            List<String> winningPositions = new ArrayList<String>();
            for (String position : posArr) {
                String[] str = position.split("_");
                int r = Integer.parseInt(str[0]);
                int c = Integer.parseInt(str[1]);
                if (matrix[r][c] == 1) {
                    String pos = r + "_" + c;
                    winningPositions.add(pos);
                    countOnes++;
                }
            }
            logger.info("中奖的万能位置是········" + winningPositions);
            if (countOnes == 0 && winningPositions.size()==0 ) {// 没有万能的情况 
                 List<String> randomPosition = Arrays.asList(getRanPositions(posArr)); //随机取一个位置先存储 之后 变成万能素材
                 
                 for (int k = 0; k < posArr.length; k++) {
                     String[] str = posArr[k].split("_");
                        int r = Integer.parseInt(str[0]);
                        int c = Integer.parseInt(str[1]);
                        matrix[r][c] = 0;
                }
                 if (randomPosition != null) {
                        for (String newPos : randomPosition) {
                            String[] newPosArr = newPos.split("_");
                            int newRow = Integer.parseInt(newPosArr[0]);
                            int newCol = Integer.parseInt(newPosArr[1]);
                            int bonusNum = 1; 
                            String newBonus = newRow + "_" + (newCol + matrixsBonus.size() * col) + "_" + bonusNum;//存储万能位置
                            if (!wildList.contains(newBonus)) {
                                if (!bonusList.contains(newBonus)) { 
                                    bonusList.add(newBonus);
                                    wildList.add(newBonus); // 添加万能位置
                                 //   logger.info("中奖列表 没有 万能素材 的连线情况1····wildList" + wildList +"```bonusList"+bonusList);
                                }
                            }
                        }
                    }
            }  
            else if (countOnes == 1 && winningPositions.size()==1) {
                for (String position : posArr) {
                    String[] str = position.split("_");
                    int r = Integer.parseInt(str[0]);
                    int c = Integer.parseInt(str[1]);
                    matrix[r][c] = 0;
                }
                
                 String winningPosition = winningPositions.get(0);//中奖的那个值
                    String[] winPosSplit = winningPosition.split("_");
                    int winR = Integer.parseInt(winPosSplit[0]);
                    int winC = Integer.parseInt(winPosSplit[1]);
                    
                 List<String> randomPosition = Arrays.asList(getRanPositions(posArr));//随机出来的那个新值
                 String randomPositionStr;
                 do {
                     randomPositionStr = randomPosition.get(0);
                 } while (randomPositionStr.equals(winningPosition));//防止随到同样的位置
                 
                    String[] randPosSplit = randomPositionStr.split("_");
                    int newR = Integer.parseInt(randPosSplit[0]);
                    int newC = Integer.parseInt(randPosSplit[1]);
                    
                    List<String> updatedWildList = new ArrayList<String>();
                    List<String> updatedBonusList = new ArrayList<String>(bonusList);
                    
                    for (int i = 0; i < wildList.size(); i++) {
                        String wString = wildList.get(i);
                        String[] item = wString.split("_");
                        int a1 = Integer.parseInt(item[0]);
                        int b2 = Integer.parseInt(item[1]);
                        int oldValue = Integer.parseInt(item[2]);
                        int c3 = b2 % 7;
                        if (b2 >= 7) {
                            int colOffset = b2 / 7;
                            c3 = b2 - 7 * colOffset;
                        }  
                        if (winR == a1 && winC == c3) {
                            int countMatching = 0; // 记录与中奖位置相匹配的数量
                            int newValue=0;
                            for (SlotGameLineData string : list) { 
                                String[] mapPosrr = data.getMaterialPos().split(","); 
                                for (String pos : mapPosrr) {
                                    String[] posSplit = pos.split("_");
                                    int posR = Integer.parseInt(posSplit[0]);
                                    int posC = Integer.parseInt(posSplit[1]);
                                    if (posR == winR && posC == winC) {
                                        countMatching++; //所有中奖的连线万能参与了几条
                                    }
                                }
                                newValue = oldValue + countMatching; 
                                 data.setSpecialOdds(newValue-countMatching);//生成新倍率之前的  实际倍率
                            }
                            
                            String newWildValue = newR + "_" + (newC + matrixsBonus.size() * col) + "_" + newValue;
                            
                            // 新值和旧值之间带逗号的字符串
                            String combinedValues = newWildValue + "," + wString;
                            wildList.set(i, newWildValue);
                            if (bonusList.contains(wString)) {
                                 bonusList.add(combinedValues);  // 插入新的带逗号的字符串
                            } else {
                                bonusList.add(combinedValues);
                            }
                           // logger.info("中奖列表 有 万能素材 的连线情况2····wildList" + wildList +"```bonusList"+bonusList);
                            break;
                        } 
                }
            }
             //当中奖    列表里面有   两个或者多个   万能素材的时候
            else  if (countOnes >= 2) {
                    for (String position : posArr) {
                        String[] str = position.split("_");
                        int r = Integer.parseInt(str[0]);
                        int c = Integer.parseInt(str[1]);
                        matrix[r][c] = 0; // 将posArr的所有中奖位置清零
                    }
                    
                    List<String> randomPosition;
                    String randomPositionStr;
                    do {
                        randomPosition = Arrays.asList(getRanPositions(posArr)); // 随机出来的那个新值
                        randomPositionStr = randomPosition.get(0);
                    } while (winningPositions.contains(randomPositionStr));//防止随机到同样的位置
                    
                        String[] randPosSplit = randomPositionStr.split("_");
                        int newR = Integer.parseInt(randPosSplit[0]);
                        int newC = Integer.parseInt(randPosSplit[1]);
                        
                        
                        List<String> updatedWildList = new ArrayList<String>();// 存储翻倍值+1之后的值
                        List<String> updatedList = new ArrayList<String>();//存储新值
                        List<String> yuanshiList = new ArrayList<String>();//存储原始值
                        
                        for (SlotGameLineData dLineData : list) {
                            String[] materialPos = dLineData.getMaterialPos().split(",");
                            Map<String, Integer> posCountMap = new HashMap<String, Integer>();
                            for (String wp : winningPositions) {
                                for (String pos : materialPos) {
                                    if (wp.equals(pos)) {
                                        int count = posCountMap.containsKey(pos) ? posCountMap.get(pos) : 0;
                                        posCountMap.put(pos, count + 1);
                                    }
                                }
                            }
                            for (String w : wildList) {
                                String[] item = w.split("_");
                                int a = Integer.parseInt(item[0]);
                                int b = Integer.parseInt(item[1]);
                                int oldValue = Integer.parseInt(item[2]);
                                int c3 = b % 7;
                                
                                if (b >= 7) {
                                    int colOffset = b / 7;
                                    c3 = b - 7 * colOffset;
                                }
                                
                                for (String wp : winningPositions) {
                                    String[] wpSplit = wp.split("_");
                                    int wpRow = Integer.parseInt(wpSplit[0]);
                                    int wpCol = Integer.parseInt(wpSplit[1]);
                                    if (a == wpRow && c3 == wpCol) {
                                        yuanshiList.add(a + "_" + b + "_" + oldValue);
                                        int count = posCountMap.containsKey(wp) ? posCountMap.get(wp) : 0;
                                        int newWildValue = oldValue + count; 
                                        data.setSpecialOdds(newWildValue-count);
                                        updatedWildList.add(a + "_" + b + "_" + newWildValue);
                                    }
                                }
                            }
                        }

                        
                        int odds=1;
                        for (String uString : updatedWildList) {
                            String[] item = uString.split("_");
                            int a = Integer.parseInt(item[0]);
                            int b = Integer.parseInt(item[1]);
                            int oldValue = Integer.parseInt(item[2]);
                            odds=odds*oldValue;
                            logger.info("相乘······"+odds);
                        }
                        updatedList.add(newR +  "_" + (newC + matrixsBonus.size() * col) + "_" + odds);
                        // 删除wildList中和yuanshiList相同的元素  
                        wildList.removeAll(yuanshiList);
                        wildList.addAll(0, updatedList);
                        
                        // 新值和旧值之间带逗号的字符串
                        StringBuilder combinedValues = new StringBuilder();
                        for (String value : updatedList) {
                            combinedValues.append(value).append(",");
                        }
                        for (String value : updatedWildList) {//yuanshiList
                            combinedValues.append(value).append(",");
                        }
                        if (combinedValues.length() > 0) {
                            combinedValues.setLength(combinedValues.length() - 1); 
                            bonusList.add(combinedValues.toString());
                        }
                        //logger.info("中奖列表 有  合并万能素材 的连线情况3····wildList" + wildList +"```bonusList"+bonusList);
                }
    }
        matrixCHUANSONMEN(matrix, noBingo);
}
  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-07-02 16:55
    关注

    引自免费微信小程序:皆我百晓生

    从你的代码来看,这是一段Java代码,主要是关于一个插槽游戏的一部分,其中包含很多嵌套逻辑,可以进行很多优化来提高效率和代码的可读性。首先我会列举几个大的方向来进行优化建议。以下是我给出的具体建议:

    精简逻辑和提高代码可读性

    • 提取函数:将复杂的逻辑块提取成单独的函数,例如处理中奖位置的逻辑、处理万能素材的逻辑等。这样可以让主函数更加清晰,同时每个函数有更明确的职责。
    • 减少重复代码:减少重复的代码块,可以通过参数传递或者函数复用实现。
    • 使用更具描述性的变量名:变量名应该清晰地表达其用途,这样其他开发者阅读代码时更容易理解。

    性能优化

    • 避免重复计算:在计算过程中避免重复计算已经计算过的值,可以将这些值存储起来,避免重复计算带来的性能损耗。
    • 减少不必要的对象创建:避免不必要的对象创建和销毁,尤其是在循环中。比如使用StringBuilder来构建字符串时,只有在最后需要时才创建字符串。

    优化后的伪代码(简化版)示例:

    由于你的代码涉及多个逻辑分支和大量细节,完全重写可能不现实。我会提供一个简化版的伪代码框架供你参考,你可以在此基础上进一步细化逻辑。

    List<SlotGameLineData> list = getAdjacentMaterialList(matrix);
    if (!list.isEmpty()) {
        for (SlotGameLineData data : list) {
            String[] posArr = data.getMaterialPos().split(",");
            List<String> winningPositions = findWinningPositions(matrix, posArr); // 找到中奖位置函数
            int countOnes = winningPositions.size(); // 记录中奖数量
            if (countOnes > 0) { // 中奖情况处理逻辑重构为一个函数
                handleWinningPositions(matrix, posArr, winningPositions, countOnes); // 优化处理中奖逻辑的外部调用函数
            } else { // 没有万能的情况处理逻辑重构为一个函数,精简逻辑流程,减少重复操作等优化手段等
                handleNoWinningPositions(matrix, posArr); 
            }
        }
        matrixCHUANSONMEN(matrix, noBingo); // 在简化之后进行优化之后的函数替换原始的矩阵操作逻辑等优化手段等
    }
    

    在此基础上你可以进一步细化每个函数的具体实现,按照上述的优化方向进行操作。对于复杂的业务逻辑可能需要详细讨论每个函数的职责和功能细分。这只是一个初步的优化建议,具体细节需要根据实际需求进一步细化。希望对你有所帮助!

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月2日