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)}