Skip to content

Instantly share code, notes, and snippets.

@IegorT
Last active November 17, 2018 00:34
Show Gist options
  • Save IegorT/9dabc4168714a07a78f712a5cae26105 to your computer and use it in GitHub Desktop.
Save IegorT/9dabc4168714a07a78f712a5cae26105 to your computer and use it in GitHub Desktop.
UniLecs - Задача 140
const isHorseMove = str => {
if(!/^[A-H][1-8]-[A-H][1-8]$/.test(str)) throw new Error('wrong incoming data');
// (c1, r1) - start coodinate, (c2, r2) - coordinate after move a figure
const [c1, r1, _, c2, r2] = str.split('').map(ch => ch.charCodeAt(0));
return (
(Math.abs(c1 - c2) === 1 && Math.abs(r1 - r2) === 2)
||
(Math.abs(c1 - c2) === 2 && Math.abs(r1 - r2) === 1)
)
};
console.assert(isHorseMove('B1-C3') === true);
console.assert(isHorseMove('D4-C2') === true);
console.assert(isHorseMove('D4-B3') === true);
console.assert(isHorseMove('D4-B5') === true);
console.assert(isHorseMove('H1-G3') === true);
console.assert(isHorseMove('H1-F2') === true);
console.assert(isHorseMove('E2-E4') === false);
console.assert(isHorseMove('C2-C8') === false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment