Last active
August 7, 2019 08:31
-
-
Save jannesiera/f0b5af5a001bcd12cf7906fcf16f2137 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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), | |
] | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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