Skip to content

Instantly share code, notes, and snippets.

@JeffML
Last active July 16, 2017 21:59
Show Gist options
  • Save JeffML/ef445ecf2bb5c5f0859dd3b372de7e38 to your computer and use it in GitHub Desktop.
Save JeffML/ef445ecf2bb5c5f0859dd3b372de7e38 to your computer and use it in GitHub Desktop.
Legal moves based on moveVectors
module.exports = function (boardAndPiece, candidateMoves) {
if (!boardAndPiece.board) return candidateMoves;
const rangeChecks = {
B: vectorChecks,
R: vectorChecks,
K: vectorChecks,
Q: vectorChecks,
P: pawnChecks,
N: knightChecks
};
var rangeCheck = rangeChecks[boardAndPiece.piece.piece];
return rangeCheck(boardAndPiece, candidateMoves)
}
//...
function vectorChecks(boardAndPiece, candidateMoves) {
for (const [j, v] of candidateMoves.moveVectors.entries()) {
for (const [i, m] of v.entries()) {
const p = boardAndPiece.board.pieceAt(m);
if (p) {
if (p.color === boardAndPiece.piece.color) {
candidateMoves.moveVectors[j] = v.slice(0, i);
break;
} else {
candidateMoves.moveVectors[j] = v.slice(0, i + 1);
Object.assign(candidateMoves.moveVectors[j].slice(-1)[0], {
hasCaptured: p
})
break;
}
}
}
}
return {
moveVectors: candidateMoves.moveVectors,
moves: Array.prototype.concat(...candidateMoves.moveVectors)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment