Implementing a Simple Machine Learning Intelligence to Play Rock-Paper-Scissors in Javascript

Rock-paper-scissors is not a complicated game. However, there are some that not only enjoy the game, but take pride in professing skill at it. But is it possible to have skill at rock-paper-scissors?

In the real world, where a human plays against a human, “cheating” is relatively easy. A player can gain an advantage by subtly delaying their release just long enough to read their opponent’s body language and choose a winning gesture. For sake of argument, let’s ignore this and assume the players play fairly, where each is completely unaware of their opponent’s move.

In this case, optimal play between two players making truly random gestures ends in a tie (on average). The only way to repeatedly win is to somehow predict your opponent’s gesture, which is impossible if your opponent is truly random. Only if your opponent has a bias can statistics be used to potentially model and predict their gesture.

To demonstrate this, I implemented three simple prediction routines.

To play a round against each routine, just click a link representing the gesture you’d like to make. The routine will then calculate its own gesture (not looking at your current choice of course), and then display the result.

1. Conditional-0
All the opponent’s past gestures are counted, and these counts are used as weights when pseudo-randomly selecting a gesture. For example, if you’ve picked rock once, paper four times, and scissors five times, then there will be a 10% chance that the routine will choose paper, 40% for scissors, and 50% for rock.

2. Conditional-1
This method tracks the last two gestures made by the opponent, and counts the number of each transition. These counts are again used to weight the random selection of a gesture. This allows it to see simple sequential patterns where an opponent’s next gesture is dependent on their last gesture.

3. Conditional-2
Similar to the previous method, this one tracks a combination of the opponent’s last gesture, and its own next-to-last gesture. This allows it to see simple dependence between the opponent’s gesture and its own.

It’s a strange game in that the player with the most entropy wins in the long run. To see the code click here.

One Response to “Implementing a Simple Machine Learning Intelligence to Play Rock-Paper-Scissors in Javascript”

  1. That sounds like it would work against opponents unaware of the algorithm, but it takes more than statistics, which can’t tell the Thue-Morse sequence from alternation. Are you still interested in a good rps ai, bacause I’ve been thinking about this recently.

Leave a Reply