Skip to content

Instantly share code, notes, and snippets.

@barfi
Last active March 7, 2023 00:31
Show Gist options
  • Save barfi/fa165dfcfd3ea1ac92a00709551c0af7 to your computer and use it in GitHub Desktop.
Save barfi/fa165dfcfd3ea1ac92a00709551c0af7 to your computer and use it in GitHub Desktop.
Rock paper scissors
// Case with 3 objects
const abc = (p1, p2, map = { a:0, c:1, b:2 }) => {
  return ['DRAW', 'P1', 'P2'][(map[p1] - map[p2] + 3) % 3]
}
// Case with 4 objects
const abcd = (p1, p2, map = { a:0, d:1, c:2, b:3 }) => {
  return ['DRAW', 'P1', 'P2'][(map[p1] - map[p2]) % 2 && (4 - ((map[p1] - map[p2] + 4) % 4)) % 3 + 1]
}
// Case with 5 objects
const abcde = (p1, p2, map = { a:0, e:1, d:2, c:3, b:4 }) => {
  return ['DRAW', 'P1', 'P2'][((map[p1] - map[p2] + 5) % 5 - 1) % 2 + 1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment