3

I wanted to create a rock-paper-scissors game that didn't use a lot of conditionals, and I was wondering if there were any mathematical way of representing the cycle of rock-paper-scissors. So Rock beats Paper beats Scissors beats Rock, or Rock > Paper > Scissors > Rock. If you assigned numbers to these, it wouldn't work: 0 > 1 > 2 > 0. But I was wondering if there were some way you could use modulus function to make this work? I understand that if you had a series of integers and modded them by 3 you would get a repeating cycle of 0, 1, 2, e.g. 60 % 3 = 0; 61 % 3 = 1; 62 % 3 = 2, 63 % 3 = 0, etc.

Do you think there could be any way to make this programmable using mods? I'm interested to know if there's an algorithm or some way to use this to create a cycle where 0>1>2>0. Just a warning, though, I don't study maths or know a lot about complex maths, so if you know of a way to do this, I'd really appreciate it if you tried not to overwhelm me with maths terms!

Lou
  • 257

2 Answers2

4

Since there are only three options the simplest thing to do is an equality check rather than a less than sort of comparison, which is possibly what long tom meant.

Specifically, $x\equiv y+1$ modulo three if and only if $x$ beats $y$. Swap $x,y$ to check the other way round. If neither was true, it was a tie.

not all wrong
  • 16,178
  • 2
  • 35
  • 57
2

Codify Rock by 0, Scissors by 1, and Paper by 2. Notice that, modulo $3$, option $k$ beats option $k+1$ and is beaten by option $k-1$. So, if $x$ and $y$ are the choices of player 1 and 2, respectively, then compute $k=x-y$ modulo $3$. Then player 2 wins iff $k=1$, player 1 wins iff $k=-1$, and they tie iff $k=0$. This easily generalizes to cycles of any length. Thus, after correctly codifying the options by numbers (actually elements of the group $\mathbb Z_n$), all you need to do is compute the difference, compute modulo $n$, check whether the result is $0$ (resulting in a tie), or is it in the range 1 to floor(n/2) (player 2 wins), or else player 1 wins (some care needed according to the parity of $n$ and the rules of the game).

Ittay Weiss
  • 79,840
  • 7
  • 141
  • 236
  • Thank you, this looks like just what I need! I just need to work through it to try and understand it properly :). – Lou May 14 '13 at 08:59
  • you're welcome. – Ittay Weiss May 14 '13 at 09:00
  • Okay, so I tried this out but I may have gone wrong somewhere. It still works, but the way I've done it I've arrived at player 1 wins if k = 1, player 2 wins if k = 2, and a tie if k = 0.

    I've assigned Rock, Scissors and Paper to 0, 1, 2 in that order, and when I set k to (x - y) mod 3 it turns out as I've described. As I say though, this will still do what I want it to do, I just don't understand why I've got that and you've got 1, -1 and 0.

    – Lou May 14 '13 at 16:13
  • Wait, never mind, I got it - it was a bracketing problem. I thought you meant (x - y) mod 3 when you meant x - (y mod 3)/ – Lou May 16 '13 at 16:31
  • oh no no, I meant (x-y) modulo 3. – Ittay Weiss May 16 '13 at 19:49
  • ...In that case I don't get it. If X picks Rock = 0 and Y picks Scissors = 1, then the result is -1 mod 3, which is 2, so X wins iff the result = 2. But you said that player 1 wins iff the result = -1. That works if you do x - (y mod 3), because 1 mod 3 = 1 and 0 - 1 = -1, but not if you do (x-y) mod 3. So if that's the equation you were using, how did you get -1, 0 and 1 instead of 0, 1 and 2? – Lou May 17 '13 at 05:58
  • -1 is equal to 2 modulo 3. So, it is all ok. 0 means tie, 1 means player 2 wins, and 2 (which is the same as -1 modulo 3) means player 1 wins. When you code it, usually when you mod something by n, you get a number between 0 and n-1. Your code needs to know how mod n works in the computer language you're using. – Ittay Weiss May 17 '13 at 06:05
  • Oh okay, just so long as it's returning discrete values I guess it all works the same. Thanks a bunch, this all made sense :). – Lou May 17 '13 at 17:03