Create Multiplayer Rock Paper Scissors in python — Part 1

Mehrab Hojjati Pour
5 min readAug 20, 2020

Hello everyone!

I’m here with a brand new tutorial of how to create multiplayer rock paper scissors in python!

To make the tutorial understandable, I would create this tutorial in 3 different parts and you are here with the first part!

What we actually do in this tutorial?

  1. At first, we create the offline mode of our game in the terminal! note we don’t want to rush!!
  2. Then we go for part 2, Creating the GUI :)
  3. And finally, in part 3, We are going to create our game multiplayer! this is the part that makes you read this post ;)

So why are we wasting our time?! Let’s start the tutorial!!

Ok, so rock paper scissors is a basic game that stands on 3 choices! Surely you know how to play this game with your friend but when we want to play with the computer, we should create a choice generator!

We can simply do it by Random library. So let’s import it:

import random

So after We import random library, its time to define our variables:

# Create choices dictionary for defining our choices names and etc...
choices = {
'0':{
'name': 'Rock',
'emoji': '👊',
'action': 'Smashes'
},
'1':{
'name': 'Paper',
'emoji': '🖐',
'action': 'covers'
},
'2':{
'name': 'Scissors',
'emoji': '✌️',
'action': 'cut'
}
}
score_board = {
'wins': 0,
'draws': 0,
'loses': 0
}

Before we go to the next part let me explain more about these variables!

So the first variable (Choices) is a dictionary of all possible choices name, emoji, and a string of action that you will understand it later. its nothing big or else, it's just a way I keep all of my game choices.

and I think the second variable is so obvious! it’s the game scoreboard and we keep our score over there.

So we have defined our variables, maybe its good time to print a welcome message too:

print('Welcome to Multiplayer Rock Paper Scissors game!')
print('This game created by www.mehrab.xyz for teaching how to create multiplayer games with socket.io')

the only step between us and our final app is defining some functions to decrease our codes and make it more readable.

def find_choice_advantage(choice,choices = choices):
# Check if our choise - 1 exist in our choices dictionary, it means current choice has advantage on the previews choice
if str(choice - 1) in choices: return str(int(choice) - 1)
else: return (len(choices) - 1)
# when there is no choice above are selected choice, it means we should take the last choice
def print_score_board(score_board = score_board):
for item in score_board:
print(item,':',score_board[item])
def print_choices():
print('your choices in terminal mode is:')
for choice_index in choices:
print(choice_index,'-',choices[choice_index]['name'])
print_choices()

The second and third function is so clear! we wrote these functions to print our dictionaries in a way we like it every time or every place we want.

But the first function is so important to understand! So you know that in this game there is 3 choice and each choice has an advantage upon another one! there is no choice that has an advantage upon other choices.

So with this function, we find what choice has an advantage upon another with the help of index numbers in the choices dictionary. ( this is so important function and I suggest really try to understand the depth of it and if you don't, comment below and I try to answer that in a different way )

ok now everything is ready and we go to create our game handler.

Because we want our game can be played until the player decides to exit it, we are going to use a while loop with a value of true. and inside that while loop, we should place an input() to get a choice from the player via terminal.

while True:
user_choice = input('Rock Paper Scissors? ')

so now we have user_choice but how should we know that the user tries to quit our game? by creating and if condition this problem will be solved too!

if user_choice == 'q': break
# We dont write else because if when the input is q, the app break and writing else here, will only add one extra and useless line here

I think you already test your game by now! when you run this game, it will print your welcome message at first and then came into this loop and ask your choice for the game, it will ask your choices until you finally enter q as input and the game will be closed.

ok, now we should check the entered choice is exists in our choices dictionary or not!

if user_choice not in choices:
print_choices()
continue

by the above code, we check if the entered user_choice has not existed in our choices dictionary, then print the choices to help the player, then skip and run the while loop again.

now its time to use the random library that we import earlier to generate a choice for computer

computer_choice = str(random.randint(0,2))

So now we have the user and computer choice, it’s time to get the advantage of our user choice and check it with the computer.

# Get user choice advantage
user_choice_advantage = find_choice_advantage(int(user_choice))
if user_choice == computer_choice: # Check if the user and computer choiced the same option
score_board['draws'] += 1
print('[DRWA] Wow you and computer both choiced',choices[user_choice]['name'])
elif str(user_choice_advantage) == computer_choice: # When user_choice_advantage is equal to computer choice, it means user is winner!
score_board['wins'] += 1
print('[WIN] Your',choices[user_choice]['name'],choices[user_choice]['action'],'computer',choices[computer_choice]['name'])
else:
score_board['loses'] += 1
print('[LOSE] Computer',choices[computer_choice]['name'],choices[computer_choice]['action'],'your',choices[user_choice]['name'])

With the above code, we get the user choice advantage and check it with computer choice. we also make changes to our scoreboard and print the result of the game.

the only thing left is the printing scoreboard when players close the game and make our players happy with a good message.

print_score_board()
print('Have a nice day ;)')

congratulation! now you just finished your Rob Paper scissors game in terminal mode. it’s a great move for the future!

because python codes in Reddit won’t get a perfect style, I’ll upload my codes in GitHub and I really suggest to review my codes again over there.

after each part of this tutorial, this GitHub repository will be updated too.

Writing this post took me about 3 hours, so with your upvotes and awards you can make me happy ❤️

also note that I’m, not a native English speaker, so if I have any grammatical problems, please forgive me, and by commenting your ideas you can help me to create better content.

--

--

Mehrab Hojjati Pour

a full-stack developer with more focused on Back-end with more than 7 years of experience. love to create new things and grow startups