0
希望这不是一个没有背景的问题,但这里没有任何问题。所以,我从某人那里继承了这段代码,而且我似乎无法让它起作用!ES6中的递归约简方法/不可变
我们正在做一个围棋游戏。我们想扫描棋盘上的一组棋子,看看它们是否为空。一个空的广场被称为“自由”。现在,在函数的底部,我们创建了一个新的2D数组'visitedBoard',用于跟踪我们到目前为止扫描的位置。
问题,目前的实现允许自由被扫描两次!当它是空的或另一种颜色(0)而不是1的时候,它似乎只是在板上标记为“已访问”的东西。
顺便说一句,在底部 - 我们通过邻居迭代,一个4行的对象数组{row:2,col:3},然后通过这个函数递归地运行它。
任何帮助是有帮助的。我是新来的这个功能/不可变的业务。
const getLiberties = function (board, point, color) {
if (board.get(point.row).get(point.col) === C.VISITED) {
return 0; // we already counted this point
} else if (board.get(point.row).get(point.col) === C.EMPTY) {
return 1; // point is a liberty
} else if (board.get(point.row).get(point.col) !== color) {
return 0; // point has an opposing stone in it
}
const neighbours = getNeighbours(board, point)
const visitedBoard = board.setIn([point.row, point.col], C.VISITED)
return neighbours.reduce(
(liberties, neighbour) => liberties + getLiberties(visitedBoard,
neighbour, color), 0)}