Skip to content

Instantly share code, notes, and snippets.

@JeffML
Created June 14, 2017 19:16
Show Gist options
  • Save JeffML/c7fd3fb6549b71f5f3c65157bdef5493 to your computer and use it in GitHub Desktop.
Save JeffML/c7fd3fb6549b71f5f3c65157bdef5493 to your computer and use it in GitHub Desktop.
calculating diagonal moves with friendly blockers
// m: proposed move
// blockers: blocking pieces
// pp: current piece position
function diagonalChecks(m, blockers, pp) {
let isGood = true;
for (const b of blockers) {
if (b.rank > pp.rank && b.file > pp.file) {
if (m.rank > pp.rank && m.file > pp.file) {
isGood = isGood && (m.rank < b.rank && m.file < b.file);
}
}
if (b.rank > pp.rank && b.file < pp.file) {
if (m.rank > pp.rank && m.file < pp.file) {
isGood = isGood && (m.rank < b.rank && m.file > b.file)
}
}
if (b.rank < pp.rank && b.file > pp.file) {
if (m.rank < pp.rank && m.file > pp.file) {
isGood = isGood && (m.rank > b.rank && m.file < b.file)
}
}
if (b.rank < pp.rank && b.file < pp.file) {
if (m.rank < pp.rank && m.file < pp.file) {
isGood = isGood && (m.rank > b.rank && m.file > b.file)
}
}
}
return isGood;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment