Skip to content

Instantly share code, notes, and snippets.

@kyleshevlin
Created July 8, 2020 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleshevlin/9587252e1c54050aa4deb1d11319f339 to your computer and use it in GitHub Desktop.
Save kyleshevlin/9587252e1c54050aa4deb1d11319f339 to your computer and use it in GitHub Desktop.
recursiveReducer
// Part of my Othello code, no idea if this is the _best_ way to solve this,
// but it's part of my solution
// `grid` is in the parent scope how I write/use this function
function findPiecesToChange(acc, x, y, run, rise) {
const nextX = x + run
const nextY = y + rise
const cell = grid[nextY] && grid[nextY][nextX]
if (!cell) {
return null
}
if (cell === turn) {
return acc
}
return findPiecesToChange(
[...acc, { x: nextX, y: nextY }],
nextX,
nextY,
run,
rise
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment