Last active
July 16, 2017 21:59
-
-
Save JeffML/ef445ecf2bb5c5f0859dd3b372de7e38 to your computer and use it in GitHub Desktop.
Legal moves based on moveVectors
This file contains hidden or 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
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