Last active
April 17, 2024 12:02
-
-
Save feelfreetofee/cc7939707037084ed7d1feb2622bdbcd to your computer and use it in GitHub Desktop.
Infinite Rock Paper Scissors
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
| function rps(game, a, b) { | |
| if (a == b) return 0 // Tie | |
| const r = game.indexOf(a) - game.indexOf(b) | |
| // An even number will decide "A" winner for positive numbers, odd for negative | |
| return r % 2 == (r < 0 ? 0 : 1) | |
| } | |
| // Game elements lenght must be an odd number | |
| const games = [ | |
| [ // Original | |
| 'rock', | |
| 'paper', | |
| 'scissors' | |
| ], | |
| [ // http://samkass.com/theories/RPSSL.html | |
| 'rock', | |
| 'paper', | |
| 'scissors', | |
| 'spock', | |
| 'lizard' | |
| ], | |
| [ // https://www.facebook.com/RockPaperScissorLizardSpockCrocSnake/ | |
| 'rock', | |
| 'paper', | |
| 'scissors', | |
| 'spock', | |
| 'lizard', | |
| 'croc', | |
| 'snake' | |
| ], | |
| [ // https://www.mithrandir.com/RockPaperScissors/RockPaperScissors.shtml | |
| 'rock', | |
| 'paper', | |
| 'scissors', | |
| 'spock', | |
| 'match', | |
| 'phoenix', | |
| 'lizard' | |
| ], | |
| [ // https://www.johndcook.com/blog/2018/08/07/rock-paper-scissors-lizard-spock/ | |
| 'rock', | |
| 'paper', | |
| 'scissors', | |
| 'spock', | |
| 'hawking', | |
| 'velociraptor', | |
| 'lizard' | |
| ], | |
| [ // Spiderman Batman + Wizard Glock | |
| 'rock', | |
| 'paper', | |
| 'scissors', | |
| 'spock', | |
| 'spiderman', | |
| 'batman', | |
| 'wizard', | |
| 'glock', | |
| 'lizard' | |
| ] | |
| ] | |
| // This is a test, not the actual code. | |
| // The keys of this object also works as game. | |
| const elements = { | |
| rock: ['scissors', 'spiderman', 'wizard', 'match', 'hawking', 'lizard', 'snake'], | |
| paper: ['rock', 'spock', 'batman', 'glock', 'phoenix', 'velociraptor', 'croc'], | |
| scissors: ['paper', 'spiderman', 'wizard', 'match', 'hawking', 'lizard', 'snake'], | |
| spock: ['rock', 'scissors', 'batman', 'glock', 'phoenix', 'velociraptor', 'croc'], | |
| spiderman: ['paper', 'spock', 'wizard', 'match', 'hawking', 'lizard', 'snake'], | |
| batman: ['rock', 'scissors', 'spiderman', 'glock', 'phoenix', 'velociraptor', 'croc'], | |
| wizard: ['paper', 'spock', 'batman', 'match', 'hawking', 'lizard', 'snake'], | |
| glock: ['rock', 'scissors', 'spiderman', 'wizard', 'phoenix', 'velociraptor', 'croc'], | |
| match: ['paper', 'spock', 'batman', 'glock', 'hawking', 'lizard', 'snake'], | |
| phoenix: ['rock', 'scissors', 'spiderman', 'wizard', 'match', 'velociraptor', 'croc'], | |
| hawking: ['paper', 'spock', 'batman', 'glock', 'phoenix', 'lizard', 'snake'], | |
| velociraptor: ['rock', 'scissors', 'spiderman', 'wizard', 'match', 'hawking', 'croc'], | |
| lizard: ['paper', 'spock', 'batman', 'glock', 'phoenix', 'velociraptor', 'snake'], | |
| croc: ['rock', 'scissors', 'spiderman', 'wizard', 'match', 'hawking', 'lizard'], | |
| snake: ['paper', 'spock', 'batman', 'glock', 'phoenix', 'velociraptor', 'croc'] | |
| } | |
| function test() { | |
| // Test for the test | |
| const keys = Object.keys(elements) | |
| if (keys.length % 2 == 0) | |
| return `❌ Elements length must be odd` | |
| const length = ~~(keys.length / 2), count = {} | |
| for (const element in elements) | |
| if (elements[element].length != length) | |
| return `❌ ${element} must beat ${length} element${length == 1 ? '' : 's'}` | |
| else if (elements[element].includes(element)) | |
| return `❌ ${element} can't beat itself` | |
| else | |
| for (const key of elements[element]) | |
| if (!elements.hasOwnProperty(key)) | |
| return `❌ ${key} is not a valid element` | |
| else if (elements[key].includes(element)) | |
| return `❌ ${element} and ${key} can't beat eachother` | |
| else if (count.hasOwnProperty(key)) | |
| count[key] += 1 | |
| else | |
| count[key] = 1 | |
| for (const key in count) | |
| if (count[key] != length) | |
| return `❌ ${key} must be defeated ${length} time${length == 1 ? '' : 's'}` | |
| for (const a of keys) | |
| for (const b of keys) | |
| if (rps(keys, a, b) != elements[a].includes(b)) | |
| return `❌ ${a} must ${elements[a].includes(b) ? 'win' : 'lose'} against ${b}` | |
| // The actual test | |
| for (const game of games) | |
| if (game.length % 2 == 0) | |
| return `❌ Game #${games.indexOf(game)} length must be odd` | |
| else | |
| for (const a of game) | |
| for (const b of game) | |
| if (rps(game, a, b) != elements[a].includes(b)) | |
| return `❌ Game #${games.indexOf(game)} > ${a} must ${elements[a].includes(b) ? 'win' : 'lose'} against ${b}` | |
| return `☑️ OK` | |
| } | |
| console.log(test()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment