In, Rock-Paper-Scissor games, it is easy to decide if move a wins against move b using their index at a cycle. So you don't have to manually decide in your code the result of every combination as other answers here suggest.
For the Rock-Paper-Scissor-Spock-Lizard version:
Let's assign a number to each move (0, 1, 2, 3, 4).
Notice that every move beats two moves:
- The move previous to it in the cycle (or four cases ahead)
- The move two cases ahead in the cycle
So let d = (5 + a - b) % 5
. Then:
- d = 1 or d = 3 => a wins
- d = 2 or d = 4 => b wins
- d = 0 => tie
For the Rock-Paper-Scissor version:
let d = (3 + a - b) % 3
. Then:
- d = 1 => a wins
- d = 2 => b wins
- d = 0 => tie
Generalization For n >= 3 and n odd:
Let d = (n + a - b) % n
. Then:
- If d = 0 => tie
- If d % 2 = 1 => a wins
- If d % 2 = 0 => b wins
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…