Skip to content

Instantly share code, notes, and snippets.

@jannesiera
Last active August 7, 2019 08:31
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 jannesiera/f0b5af5a001bcd12cf7906fcf16f2137 to your computer and use it in GitHub Desktop.
Save jannesiera/f0b5af5a001bcd12cf7906fcf16f2137 to your computer and use it in GitHub Desktop.
// TODO use .flat() or .flatten() instead, but not in this version of JS yet?
flatten(arrays) {
return [].concat.apply([], arrays);
}
isScorable(posAllStars: number [] [], colors: number []) {
const starColor = colors[0];
// Check for every star and collect the results
return this.flatten(
posAllStars.map(posStar => this.isStarScorable(posStar, starColor))
);
}
isStarScorable(posStar: number [], color: number) {
// From the star position, check in every direction if we have a block of the same color
// If we do, remember it and check the next position in the direction
const inBounds = ([x, y]) => x !== undefined && y !== undefined && x > 0 && x < this.rows && y > 0 && y < this.cols;
const gridValue = ([x, y]) => inBounds([x, y]) ? this.grid[x][y] : undefined;
const goTop = ([x, y]) => [x - 1, y]
const goBottom = ([x, y]) => [x + 1, y]
const goLeft = ([x, y]) => [x, y - 1]
const goRight = ([x, y]) => [x, y + 1]
const check = (matched: number [], current: number [], color: number, move) => {
const next = move(current);
if(gridValue(next) === color)
return check([...matched, next], next, color, move);
else
return matched;
}
return this.flatten(
[
check([], posStar, color, goTop),
check([], posStar, color, goBottom),
check([], posStar, color, goLeft),
check([], posStar, color, goRight),
]
);
}
@Q-ro
Copy link

Q-ro commented Aug 7, 2019

Thanks dude, this helped me a lot, but there is one thing, this only checks in a cross like pattern, i was trying to find all connected gems :P, still, this thought me a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment