Callysto.ca Banner

number_of_rounds = 50
player1 = 'Steven'
player2 = 'Edward'
choices = {
    'Steven':['Rock','Paper','Scissors'],
    'Bart':['Rock'],
    'Edward':['Scissors','Scissors','Paper'],
    'Freddie':['Paper','Paper','Rock','Paper','Paper','Rock']}


# create a dataframe of rock beats scissors etc.
import pandas as pd
winlose = pd.DataFrame(columns=['Rock','Paper','Scissors'])
winlose['Rock'] = ['Tie','Win','Loss']
winlose['Paper'] = ['Loss','Tie','Win']
winlose['Scissors'] = ['Win','Loss','Tie']
winlose.index = ['Rock','Paper','Scissors']

from random import choices as c
p1wins = 0
p2wins = 0
ties = 0
p1c = ''
p2c = ''
results = pd.DataFrame(columns=['Tie']) # so that 'Tie' is listed below the others in the chart
for x in range(number_of_rounds+1):
    results = results.append({player1+' Choice':p1c, player2+' Choice':p2c, 'Tie':ties, player2:p2wins, player1:p1wins}, ignore_index=True)
    p1c = c(choices[player1])[0]
    p2c = c(choices[player2])[0]
    result = winlose.at[p1c, p2c]
    if result == 'Win':
        p1wins += 1
    if result == 'Loss':
        p2wins +=1
    if result == 'Tie':
        ties += 1

# convert wide dataframe to tidy
results['Round'] = results.index
tidy_results = results.drop(columns=[player1+' Choice', player2+' Choice']).melt('Round', var_name='Player', value_name='Wins')        

import plotly.express as px
fig = px.bar(tidy_results, y='Player', x='Wins', orientation='h', animation_frame='Round', color='Player')
fig.update_layout(xaxis_range=[0,tidy_results['Wins'].max()+1], showlegend=False)
fig.update_layout(title='Rock Paper Scissors Tournament to '+str(number_of_rounds))
#fig.write_html('rps.html')
fig.show()

Results Data

Let's have a look at the table of results from that tournament by running the following code cell.

results

We can also count how many times a player chose each of the options:

results['Edward Choice'].value_counts()

Or display the round choices for all of the players:

for column in results.columns:
    if 'Choice' in column:
        print(results[column].value_counts())
        print('---')

Determining the Winner of a Round

We can see the winlose table by running the following cell. Player 1's choice is the index column and Player 2's choice is the colunn headings.

winlose

To check if Player 1 wins by choosing Rock when Player 2 chooses Paper, we can use the following code.

winlose.at['Rock','Paper']

Challenges

Try editing the first code cell to have different matches, for example Bart versus Freddie.

If you are feeling even more adventurous, try adjusting the code to have them play Rock Paper Scissors Lizard Spock.

Callysto.ca License