【问题标题】:How to reduce overly-redundant for loops如何减少过度冗余的for循环
【发布时间】:2021-10-16 01:47:49
【问题描述】:

我目前正在开发一个国际象棋游戏,但在我的 Bishop 课程中遇到了代码冗余问题。我正在尝试创建一个函数,该函数将获得主教的所有可能动作。为此,我将创建一个 for 循环来评估主教的所有对角线,并在它碰到棋盘或棋子的末端时打破循环。这段代码的问题是它是多余的,因为要评估 Bishop 可以走的所有对角线,我必须有 4 个 for 循环。代码的简化版本如下所示

var list = [1,0,0,0,1,0,0,1]; // 1d list
var index = 5; // position of "bishop"
for (let i = index, j = list.length; i < j; i++) {
    if (list[i] === 1) { // if true, return instance of 1 and break loop
        console.log("first instance of 1 going right, found at " + i);
        break;
    }
}
for (let i = index; i >= 0; i--) {
    if (list[i] === 1) { // if true, return instance of 1 and break loop
        console.log("first instance of 1 going left, found at " + i);
        break;
    }
}

虽然此代码有效,但在处理可以向 4 个方向移动的主教时,这是相当重复的,并且可能会导致将来出现问题。有没有一种方法可以在不降低效率的情况下将 4 个 for 循环(或上例中的 2 个)减少到 1 个?因为答案需要正确的概念,所以没有太多可以展示我在这个问题上的尝试。

【问题讨论】:

  • 因为两个循环有不同的退出条件,将它合并到一个循环中可能会很尴尬。我认为你已经拥有的其实很好
  • @AresStavropoulos,有关棋盘内部表示的另一种替代方案,请参阅stackoverflow.com/questions/68976802/…

标签: javascript dry


【解决方案1】:

如何使用左右迭代器来组合两个循环并同时查看两个方向。

var list = [1,0,0,0,1,0,0,1];
var index = 5;
var left = index - 1;
var right = index + 1;
while(left >= 0 || right < list.length)
{
  if(list[left] === 1) {
    console.log("first instance of 1 going left, found at " + left);
    break;
  }

  if(list[right] === 1) {
    console.log("first instance of 1 going right, found at " + right);
    break;
  }

  if(left >= 0)
    left--;

  if(right < list.length)
    right++;
}

【讨论】:

    【解决方案2】:

    我建议使用函数来替换循环。 这样可以重复使用相同的循环而无需复制粘贴。

    这是一个代码示例:

    // Board width and height.
    const boardSize = 8;
    // The chess board (2d array, x index first).
    const board = [[0,0,1, ...],[1,0,1, ...], ...];
    
    // Get the maximum distance a piece can travel in a direction.
    function findTravelDistance(x,y,dirX,dirY) {
        for (let n = 0;; n++) {
            // Calculate the position from moving n spaces.
            var newX = x + n*dirX,
                newY = y + n*dirY;
    
            // Return if the piece is off the board.
            if (newX < 0 || newX >= boardSize || newY < 0 || newY >= boardSize)
                return n - 1;
    
            // Return if the piece hits a one.
            if (board[newX][newY] === 1)
                return n;
        }
    }
    
    // Array containing all directions a bishop can move.
    const bishopDirections = [[1,1], [1,-1], [-1,1], [-1,-1]];
    
    // Take x and y as starting position.
    // Return an array of distances corresponding to directions in bishopDirections
    function findBishopTravelDistances(x,y) {
        var distances = [0,0,0,0];
        // Calculate distances for all directions.
        for (let i = 0; i < bishopDirections.length; i++)
            distances[i] = findTravelDistance()
        return distances;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多