Rock Paper Scissors – get winner mathematically

Question:

I just finished my rock paper scissors game in python. Right now, the user has to chose between three buttons, and a string user1 is set to either “Rock”, “Paper”, or “Scissors”.
And when I evaluate the game, the code simply runs a few if statements and for example checks,

if computer == "Paper"

But if I want to expand the game a little bit with my own items, that would be a lot of “if/else”. Sow I thought I’d give each item a unique number, so that I just can use this number and don’t have to mess around with the strings.

My teacher gave me the hint to divide the two ints in order to get who wins.

But after I thought about this for a while, I didn’t found any solution on how I could do this. It’s just about the logic behind this, I don’t want any code.

How can I find the winner just with a little math, using the two ints as the items?
Thank you!

Asked By: dv02

||

Answers:

Let’s say we are implementing normal Rock Paper Scissors, as you say in your comment on your question. Let’s assign:

Rock:     0
Paper:    1
Scissors: 2

This answer assumes you know about the modulo operator (%), so if not, please look that up first to understand what it means. I use it in this answer so that when we add 1 to scissors (which is 2), you get 0 instead of 3, since rock is 0 and there are no items for number 3.

With this assignment of numbers to choices, we want a choice to win if it comes right after the other person’s choice, lose if it comes right before it, and tie if they’re equal. For example, 2 comes right after 1, so scissors beats paper. We will use the modulo operator to make sure that our numbers stay between 1 and 3 (including 1 and 3).

So if you want to determine if player 1 wins, you would check if their move is 1 bigger than player 2’s move. To tell if they tie, see if they have the same move. And if neither of those is true, then player 2 must have won. Here’s an example implementation with some tests:

>>> def winner(p1, p2):
...   if (p1+1) % 3 == p2:
...     return "Player 2 won because their move is one greater than player 1"
...   elif p1 == p2:
...     return "It's a draw because both players played the same move"
...   else:
...     return "Player 1 wins because we know that it's not a draw and that player 2 didn't win"
...
>>>
>>>
>>> rock = 0
>>> paper = 1
>>> scissors = 2
>>> winner(rock, paper)
'Player 2 won because their move is one greater than player 1'
>>> winner(paper, scissors)
'Player 2 won because their move is one greater than player 1'
>>> winner(scissors, rock)
'Player 2 won because their move is one greater than player 1'
>>> winner(rock, scissors)
"Player 1 wins because we know that it's not a draw and that player 2 didn't win"
>>> winner(paper, paper)
"It's a draw because both players played the same move"

Now in this game, the mathematical rule was that an item beats the item with the number 1 less than it (modulo 3). If you add more items, you will need to come up with a mathematical rule to govern how the game works. One example (that wouldn’t be very fun) would be to keep the rule that an item beats the item 1 less than it (and therefore loses to the item 1 more than it), and has a tie with any other item, though this would be a rather boring game.

Hope that answer helps!! Good luck!

Answered By: Christopher Shroba

An easier way to solve this is to make two dictionaries for win and lose conditions, then assign user input as the keys and computer input as the values in the dictionary.

rps_win={'rock':'scissor','paper':'rock','scissor':'paper'}
rps_lose={'rock':'paper','paper':'scissor','scissor':'rock'}

Then check if that user-computer pair is from the win or lose dictionary and accordingly add the scores.

if rps_win[b]==a:
    player=player+1 #incrementing player points
    print('win point!')
elif rps_lose[b]==a:
    comp=comp+1 #incrementing computer points
    print('lose point!')
else:
    print('tie!')
Answered By: Anujeet Swain
An easy way [for beginners] codehs rocks, paper and scissors 
Long because its for beginners if u are advanced use the above code..

""" 
Introduction - Imports, Instructions, Declaring Variables, Lists and Computer Choice 

"""

# Imports 
import time
time.sleep(2)
print "Hello !"
# Delays Response to 2 secs
time.sleep(2)
# Instructions
print "Are you ready to play Rock, Paper and Scissors with the Computer ? "
time.sleep(4)
# Asking if user is ready or not !! (Does Nothing whatever the User Answers)
yes_no = input("Are You Ready to play (Y/N) : ")
time.sleep(2)
print "Ready or Not, here we go !! "
time.sleep(2)
# Declaring Variables
win_user = 0
win_comp = 0
tie = 0
# Imports Random and Sys 
import random
import sys
# Choices in a list
choices = ["Rock", "Paper", "Scissors"]
# Computer Choice - between choices [list]
comp_choice = random.choice(choices)

"""
    User Input and Computer Input 

"""

# Asks User for Rocks, Papers or Scissors 
user_input = input("Rock, Paper or Scissors ? : ")

# User_input is now set to User_choice 
user_choice = user_input
time.sleep(2)
# Prints what user entered
print "User entered : " + user_choice
time.sleep(2)
# Prints what computer entered
print "Computer entered : " + comp_choice

# Basic Gameplay
if user_choice == "Rock" and comp_choice == "Paper":
    win_comp += 1
if user_choice == "Paper" and comp_choice == "Rock":
    win_user += 1
if user_choice == "Scissors" and comp_choice == "Paper":
    win_user += 1
if user_choice == "Paper" and comp_choice == "Scissors":
    win_comp +=1
if user_choice == "Rock" and comp_choice == "Scissors":
    win_user +=1
if user_choice == "Scissors" and comp_choice == "Rock":
    win_comp +=1
if user_choice == "Rock" and comp_choice == "Rock":
    tie +=1
if user_choice == "Paper" and comp_choice == "Paper":
    tie +=1
if user_choice == "Scissors" and comp_choice == "Scissors":
    tie +=1

""" 
    Ties, User Wins and Computer Wins and Conclusion 
""" 
time.sleep(2)    
# Checks the amounts of ties, user wins and computer wins !!!
print "User Wins : " + str(win_user) 
time.sleep(2)
print "Computer Wins : " + str(win_comp)
time.sleep(2)
print "Ties : " + str(tie)
time.sleep(2)
print "Thank You For Playing !!"

"""
Exit and Repeats 
"""

time.sleep(2)
# Asks if User wants to exit or Not !!
ask_user = input("Do you want to exit (Y/N) : ")
#  Exit is a variable which does nothing thus it exits the game
exit = 0
# If Y, then it will exit
if ask_user == "Y":
    # Does nothing, except adding a 1 to exit 
    exit += 1

# Elif ask_user == "N", repeats everything again using the WHile loop    
elif ask_user == "N": 
    # WHILE LOOP
    while ask_user == "N":
        win_user = 0
        win_comp = 0
        tie = 0
        import time
        import random
        import sys
        choices = ["Rock", "Paper", "Scissors"]
        comp_choice = random.choice(choices)


        time.sleep(2)
        user_input = input("Rock, Papers or Scissors ? : ")

        user_choice = user_input
        time.sleep(2)
        print "User entered : " + user_choice
        time.sleep(2)
        print "Computer entered : " + comp_choice


        if user_choice == "Rock" and comp_choice == "Paper":
            win_comp += 1
        if user_choice == "Paper" and comp_choice == "Rock":
            win_user += 1
        if user_choice == "Scissors" and comp_choice == "Paper":
            win_user += 1
        if user_choice == "Paper" and comp_choice == "Scissors":
            win_comp +=1
        if user_choice == "Rock" and comp_choice == "Scissors":
            win_user +=1
        if user_choice == "Scissors" and comp_choice == "Rock":
            win_comp +=1
        if user_choice == "Rock" and comp_choice == "Rock":
            tie +=1
        if user_choice == "Paper" and comp_choice == "Paper":
            tie +=1
        if user_choice == "Scissors" and comp_choice == "Scissors":
            tie +=1

        time.sleep(2)
        print "User Wins : " + str(win_user) 
        time.sleep(2)
        print "Computer Wins : " + str(win_comp)
        time.sleep(2)
        print "Ties : " + str(tie)
        time.sleep(2)
        print "Thank You For Playing !!"
        time.sleep(2)

        ask_user = input("Do you want to exit (Y/N) : ")

        if ask_user == "Y" or "Yes" or "y" or "yes":
            break
else:
    print "Wrong Input, Try again by restarting the program"
Answered By: A.S Gallery
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.