JavaScript: 2D Array Minesweeper 访问邻居 += 1 他们

JavaScript: 2D Array Minesweeper accessing neighbors to += 1 them

所以我有一个输入二维数组,里面已经放置了地雷:

const input = [
  [0, 0, '*'],
  ['*', 0, 0],
  [0, '*', 0]
];

我需要做的是将更改后的二维数组输出给邻居,但我不知道如何优雅地访问它们。

const mineSweeper = (input) => {
  for (let row = 0; row < input.length; row++) {
    for (let col = 0; col < input[row].length; col++) {
      if (input[row][col] === '*') {
        // How can I access neighbors from here elegantly?
      }
    }
  }
}

输出应如下所示:

const output = [
  [1, 2, '*'],
  ['*', 3, 2],
  [2, '*', 1]
];

有什么建议吗?谢谢

const dx = [1, 1, 1, 0, 0, -1, -1, -1];
const dy = [1, 0, -1, 1, -1, 1, 0, -1];
const mineSweeper = (input) => {
  for (let row = 0; row < input.length; row++) {
    for (let col = 0; col < input[row].length; col++) {
      if (input[row][col] === '*') {
        for (let i = 0 ; i < 8 ; i++) {
           let nr = row + dy[i], nc = col + dx[i];
           //check that is within the limits
           if (nr >= 0 && nr < input.length && nc >= 0 && nc < input[row].length) {
             input[nr][nc]++; //Do what you need with this neighbor
           }
        }
      }
    }
  }
}

我简单解释一下逻辑:

在数组 dx 和 dy 上,您将距离存储在行和列中,您需要从给定单元移动到所有 8 个相邻单元。

例如,以 dx[2] = 1dy[2] = -1 这意味着要到达这个邻居 2 您需要向右移动 1 列 +1 并在下方移动一行-1.

if 是检查给定的邻居是否存在于数组的范围内。

不是最优雅的解决方案

const input = [
    [0, 0, '*'],
    ['*', 0, 0],
    [0, '*', 0],
];

const mineSweeper = input => {
    for (let row = 0; row < input.length; row++) {
        for (let col = 0; col < input[row].length; col++) {
            if (input[row][col] === '*') {
                Number.isInteger(input[row - 1] && input[row - 1][col]) && input[row - 1][col]++;
                Number.isInteger(input[row] && input[row][col - 1]) && input[row][col - 1]++;
                Number.isInteger(input[row + 1] && input[row + 1][col]) && input[row + 1][col]++;
                Number.isInteger(input[row] && input[row][col + 1]) && input[row][col + 1]++;
                Number.isInteger(input[row - 1] && input[row - 1][col - 1]) && input[row - 1][col - 1]++;
                Number.isInteger(input[row + 1] && input[row + 1][col + 1]) && input[row + 1][col + 1]++;
                Number.isInteger(input[row - 1] && input[row - 1][col + 1]) && input[row - 1][col + 1]++;
                Number.isInteger(input[row + 1] && input[row + 1][col - 1]) && input[row + 1][col - 1]++;
            }
        }
    }
};

mineSweeper(input);

console.log(input);

您可以使用双重嵌套 map 来获取每个元素,然后您可以创建另一个函数来获取行和列的当前索引并检查每个元素的关闭 *

const input = [
  [0, 0, '*'],
  ['*', 0, 0],
  [0, '*', 0]
]

function count(data, i, j) {
  let c = 0;

  const prevRow = data[i - 1];
  const currentRow = data[i]
  const nextRow = data[i + 1];

  [prevRow, currentRow, nextRow].forEach(row => {
    if (row) {
      if (row[j - 1] == '*') c++;
      if (row[j] == '*') c++;
      if (row[j + 1] == '*') c++;
    }
  })

  return c;
}

function update(data) {
  return data.map((a, i) => {
    return a.map((b, j) => {
      return b == '*' ? b : count(data, i, j)
    })
  })
}

const result = update(input)
console.log(result)

您可以获得一个偏移量数组以获取正确的索引并检查该值是否为星号。

const
    getCount = (array, x, y) => offsets
        .reduce((c, [i, j]) => c + (array[x + i]?.[y + j] === '*'), 0),
    offsets = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]],
    input = [[0, 0, '*'], ['*', 0, 0], [0, '*', 0]],
    result = input.map((row, i, a) => row.map((v, j) => v || getCount(a, i, j)));

result.forEach(a => console.log(a.join(' ')));