refactor getNeighbors in initBoard for array index lookup instead of boardState.find(...)

This commit is contained in:
Sorrel Bri 2020-04-20 17:39:52 -07:00
parent bdfb6ebe85
commit b1f29f3d2d

View file

@ -85,12 +85,13 @@ const getBoardState = (Game) => {
return pipeMap(getLegal)(Game.boardState); return pipeMap(getLegal)(Game.boardState);
} }
const getNeighbors = (point, _, boardState) => { const getNeighbors = boardSize => (point, i, boardState) => {
const { top, btm, lft, rgt} = point.neighbors; const { top, btm, lft, rgt} = point.neighbors;
point.neighbors.top = top ? boardState.find(val => val[0] === top)[1] : top; // boardState[0] = [ '1-1', Point({x:1, y:1, boardSize}) ]
point.neighbors.btm = btm ? boardState.find(val => val[0] === btm)[1] : btm; point.neighbors.top = top ? boardState[i - boardSize][1] : top;
point.neighbors.lft = lft ? boardState.find(val => val[0] === lft)[1] : lft; point.neighbors.btm = btm ? boardState[i + boardSize][1] : btm;
point.neighbors.rgt = rgt ? boardState.find(val => val[0] === rgt)[1] : rgt; point.neighbors.lft = lft ? boardState[i - 1][1] : lft;
point.neighbors.rgt = rgt ? boardState[i + 1][1] : rgt;
return point; return point;
} }
@ -109,7 +110,7 @@ const initBoard = ({ boardSize, handicap }) => {
boardState[pt].stone = 1; boardState[pt].stone = 1;
}); });
} }
const boardStateWithNeighbors = pipeMap(getNeighbors)(boardState) const boardStateWithNeighbors = pipeMap(getNeighbors(boardSize))(boardState)
return boardStateWithNeighbors; return boardStateWithNeighbors;
} }