top of page

Take on the Challenge: Can you Beat AI in Rock-Paper-Scissors?

  • Writer: Daniel McMahon
    Daniel McMahon
  • May 13, 2024
  • 3 min read

Updated: Jun 4, 2024



ree

By Joshua Wang ('25)


For my Extended Essay, I explored the use of mathematics to analyze the game of rock paper scissors. Through the exploration, I created a program using a mathematical concept, Markov chain, that is capable of predicting an opponent's move and countering it. 


After testing my program with my friends, it turned out to be quite fun and I decided to share it here! Take on the challenge and see if you can beat my rock-paper-scissors AI by clicking here.

 

After you have given it a shot and seen how effective the AI can be (or maybe not because you were able to beat my AI :<), are you also interested in using the AI to beat your friends in rock-paper-scissors? If so, click here.


So, how does the program work? 

First of all, the Markov chain is a stochastic model that shows the sequence of events along with its probabilities of transition between “states” which depends only on the state at the current “time step”. In other words, the Markov chain is a model that tells you what happens next in a series of events. It tells you chances of going from one part of the event to the next based on what is happening at the current moment.  


Therefore, the Markov chain can be applied to games of rock-paper-scissors by treating each of the three hands (rock, paper, and scissors) as individual “states” and each round of the game as a “time step”. This allows us to model transition probabilities from what a player plays in the current round to the next round. In this way, the Markov chain model can be used to guess which of the three hands the player will play in the next round based on what the player plays in the current round. Below is an example of a Markov chain model illustrated using a matrix called transition matrix. 

Current / Next

R

P

S

R

10%

70%

20%

P

30%

40%

30%

S

20%

30%

50%

In this example, the column represents the current hand and row represents the next hand where R, P, and S represent rock, paper, and scissors respectively. What the AI does is to take in the hand played by a player in the current round, compares the transition probabilities in the same row, and counteracts the hand with highest transition probability. For example, if a player plays rock in the current round, the program compares all the values in the first row. As paper (first row, second column) has the highest transition probability of 70%, the program will then play scissors. This is because the player is most likely to play paper after playing rock. 


To improve the program, this AI also takes the results of the round (win, lose, or tie) into account. Therefore, there will be a total of 27 states instead of 9 states from the previous model above (as each of 9 states can have 3 different results: 3x9=27). If you are curious to see your 27 states of transition probabilities in rock-paper-scissors, simply enter ‘yes’ after you have ended the program.


Now, you might be wondering if there are applications for the Markov chain other than analyzing games of rock-paper-scissors, and the answer is yes! It can be used for predicting the weather and even predicting stock prices or generating sequence of words for chatbots. As long as you can define the “state” and “time step” for a sequence of events, the Markov chain is ready to reveal the pattern! 


Through the journey of writing an Extended Essay, I was inspired by the ability of mathematics to take something as simple as a game, like rock-paper-scissors, and reveal hidden depths in it. I hope my little AI has sparked some excitement and encouraged you to explore your own interests through Extended Essays or any other projects you have in mind :) 

Comments


bottom of page