Skip to content

Instantly share code, notes, and snippets.

@JeffML
Created July 16, 2017 23:33
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 JeffML/a44d20e88767df03581b264e50b5a99d to your computer and use it in GitHub Desktop.
Save JeffML/a44d20e88767df03581b264e50b5a99d to your computer and use it in GitHub Desktop.
service to determine chess square controlled by one side
module.exports = function squareControl() {
const getSquaresControlledBy = (board, pieceColor) => {
var pieces = board.boardPieces[pieceColor];
const promises = pieces.map(p =>
new Promise(resolve => {
this.act({
role: "movement",
cmd: "legalMoves",
piece: p,
board: board
}, (err, msg) => {
if (err) throw new Error("ack!")
resolve({
piece: p,
moves: msg
})
});
})
);
return Promise.all(promises)
}
this.add({
role: "board",
cmd: "squaresControlledBy",
}, (msg, reply) => {
getSquaresControlledBy(msg.board, msg.color)
.then(pieceMoves => {
const allMoves = pieceMoves.map(pms => pms.moves.moves)
const response = {
pieceMoves,
controlled: Array.prototype.concat(...allMoves)
.filter((move, i, self) =>
self.findIndex(m => m.file === move.file && m.rank === move.rank) === i)
}
reply(null, response)
})
.catch(err => console.error(err));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment