stuck with a rock paper scissors game - checking for a winner

+1 Daniel Bogorad · November 1, 2014
Hy everyone ))
I've been learning python for a couple of days now , and i am making a small rock paper scissors game to practice stuff ...
I've emphasized the use of functions even when it was possible to do without them (because i really wanted to get them down well - they are confusing me :P )..
I think i mostly managed fine except the part when the program checks who won ..
every time there was a different result - sometimes correct , sometimes not . i cant find my mistake ...
if someone could notice it - please let me now where I'm wrong ...
your comments are appreciated :)

import random


def player_move(p_choise):                 # main function to decide the player's move and convert to int
    if p_choise == 'r':                   # based on what the player has chosen .
        int_player_move = 1
    if p_choise == 'p':
        int_player_move = 2
    if p_choise == 's':
        int_player_move = 3
    return int_player_move


def comp_move():                            # main function to look for - decides the computer's move .
    int_comp_move = random.randrange(1, 4)    # refer to this when checking winner .
    return int_comp_move


def comp_move_to_str():     # converts the integer computer choise to a Word  
    if comp_move() == 1:              # - Rock paper or Scissors and returns the                   
        str_comp_move = "Rock"                                                              
    if comp_move() == 2:
        str_comp_move = "Paper"
    if comp_move() == 3:
        str_comp_move = "Scissors"
    return str_comp_move

def announce_comp():
    print("Computer Won")

def announce_player():
    print("Congratz ! You Won !")

p_choise = input("Choose rock , paper, or scissors ? (r/p/s) : ")  # p_choise stores the players choise
print("Computer Rolled : ", comp_move_to_str())

if int(player_move(p_choise)) == comp_move():
    print("Computer has chosen the same move . Roll again")

if int(player_move(p_choise)) == 1 and comp_move() == 2:
    announce_comp()
elif int(player_move(p_choise)) == 1 and comp_move() == 3:
    announce_player()

if int(player_move(p_choise)) == 2 and comp_move() == 1:
    announce_player()
elif int(player_move(p_choise)) == 2 and comp_move() == 3:
    announce_comp()

if int(player_move(p_choise)) == 3 and comp_move() == 1:
    announce_comp()
elif int(player_move(p_choise)) == 3 and comp_move() == 2:
    announce_player()


Post a Reply

Replies

Oldest  Newest  Rating
+2 Jay Tauron · December 31, 2014
if i have the statement
if x in (1,5)

it will evaluate True only if x is 1 or 5, it's just a shorter way of writing
if x == 1 or x == 5

this is useful if you're testing x for many different possibilities.

Why this works is because i started with Daniel's code:
def who_won():
        if int(user_to_int()) == compMove:
            declare_winner = "Tie game"
        else:
            if user_to_int() == 1:
                if compMove == 2:
                    declare_winner = "Computer Won ."
                elif compMove == 3:
                    declare_winner = "You Won !!!"
            elif user_to_int() == 2:
                if compMove == 1:
                    declare_winner = "You Won !!!"
                elif compMove == 3:
                    declare_winner = "Computer Won ."
            elif user_to_int() == 3:
                if compMove == 1:
                    declare_winner = "Computer Won ."
                elif compMove == 2:
                    declare_winner = "You Won !!!"
        return declare_winner


then i just removed the variable and just return the values instead, removing the need for the else or elif statements, where elif could just be if and also I removed the int() function since it's already an integer
def who_won():
        if user_to_int() == compMove:
            return "Tie game"
            
        if user_to_int() == 1:
            if compMove == 2:
                return "Computer Won ."
            if compMove == 3:
                return "You Won !!!"
        if user_to_int() == 2:
            if compMove == 1:
                return "You Won !!!"
            if compMove == 3:
                return "Computer Won ."
        if user_to_int() == 3:
            if compMove == 1:
                return "Computer Won ."
            if compMove == 2:
                return "You Won !!!"


Next I decided to concentrate on the conditions when the computer won, although i could've done the same with the user winning, and so removed the cases for the user winning and added it to the end since if the computer didn't win then the user must have
def who_won():
        if user_to_int() == compMove:
            return "Tie game"

        if user_to_int() == 1:
            if compMove == 2:
                return "Computer Won ."
        if user_to_int() == 2:
            if compMove == 3:
                return "Computer Won ."
        if user_to_int() == 3:
            if compMove == 1:
                return "Computer Won ."

        return "You Won !!!"


then i thought how i would relate the numbers mathematically instead of having multiple if statements. You can see that if you do compMove - user_to_int() then you'll either get 1 or -2, and so replaced the conditions with this test

def who_won():
        if user_to_int() == compMove:
            return "Tie game"

        if compMove - user_to_int() in (1, -2):
            return "Computer Won ."

        return "You Won !!!"


I hope you see how this works :)
using this method, this function could have been written 3 other different ways by changing the equation and changing which winner it tests for
+1 K D · November 2, 2014
Hello

You are constantly getting a new result for the computer.  Every time you call comp_move() it generates a new random number so computers choice is constantly changing.  Put the result into a variable and test the variable instead of the function comp_move().

Have comp_move_to_str() take a parameter.  Then pass the variable you made of the return value of comp_move()  to  comp_move_to_str() and test the parameter instead of the function comp_move().



I really hope this helps...
+1 Daniel Bogorad · November 2, 2014
Yeah man  thanks a lot :)
and actually i have figured things out on my own finally and even added a few neat touches :)
check it out )) 
import random
import time

keepGame = input("Would you like to play Rock Paper Scissors ? (y/n)")
while keepGame == 'y':
    def user_to_int():
        if userMove == 'r':
            int_user = 1
        elif userMove == 'p':
            int_user = 2
        else:
            int_user = 3
        return int_user

    def user_to_word():
        if userMove == 'r':
            user_word = "Rock"
        elif userMove == 'p':
            user_word = "Paper"
        else:
            user_word = "Scissors"
        return user_word

    def comp_to_word():
        if compMove == 1:
            comp_word = "Rock"
        elif compMove == 2:
            comp_word = "Paper"
        else:
            comp_word = "Scissors"
        return comp_word

    def who_won():
        if int(user_to_int()) == compMove:
            declare_winner = "Tie game"
        else:
            if user_to_int() == 1:
                if compMove == 2:
                    declare_winner = "Computer Won ."
                elif compMove == 3:
                    declare_winner = "You Won !!!"
            elif user_to_int() == 2:
                if compMove == 1:
                    declare_winner = "You Won !!!"
                elif compMove == 3:
                    declare_winner = "Computer Won ."
            elif user_to_int() == 3:
                if compMove == 1:
                    declare_winner = "Computer Won ."
                elif compMove == 2:
                    declare_winner = "You Won !!!"
        return declare_winner

    userMove = input("Rock , Paper or Scissors ? (r/p/s) : ")
    compMove = random.randrange(1, 4)

    print("You Rolled :", user_to_word())
    print("Computer Rolled :", comp_to_word())
    print(who_won())

    keepGame = input("Play Again ? (y/n) : \n")

else:
    if keepGame == 'n':
        print("Ok , maybe next time :) \nClosing Game . . . ")
        time.sleep(1.5)
        exit()
    else:
        print("Invalid Character. \nClosing game . . . ")
        time.sleep(1.5)
        exit()
+1 Jay Tauron · December 30, 2014
got bored and just shortened your code for you

import random
import time


def play():
    keepGame = input("Would you like to play Rock Paper Scissors? (y/n): ")
    if keepGame == 'y':
        return True
    if keepGame == 'n':
        print("Ok, maybe next time :)\nClosing game . . .")
    else:
        print("Invalid Character.\nClosing game . . . ")
    time.sleep(1.5)
    return False

while play():
    def user_to_int():
        if userMove == 'r':
            return 1
        if userMove == 'p':
            return 2
        return 3

    def user_to_word():
        if userMove == 'r':
            return "Rock"
        if userMove == 'p':
            return "Paper"
        return "Scissors"

    def comp_to_word():
        if compMove == 1:
            return "Rock"
        if compMove == 2:
            return "Paper"
        return "Scissors"

    def who_won():
        if user_to_int() == compMove:
            return "Tie game"
        if compMove - user_to_int() in (1, -2):
            return "Computer Won."
        return "You Won!!!"

    userMove = input("Rock, Paper or Scissors? (r/p/s): ")
    compMove = random.randrange(1, 4)

    print("You Rolled :", user_to_word())
    print("Computer Rolled :", comp_to_word())
    print(who_won())
exit()
+1 Jay Tauron · December 31, 2014
Well with the randrange / randint, I just left whatever Daniel had but I would probably use randint in this case since it will have to be integers.

With assigning an integer, the compMove variable is already an integer so need not be changed. With the userMove variable, it's a string and so does need to be converted to an integer in order to do the comparison.

Would you like me to expand further on how the who_won() function works?
+1 Chris Nelson · December 31, 2014
Hey Jay,
thanks for the clarification. Yes I realized after looking over the code again, that comp_move() is already integer and comp_to_word() is just displaying a word for the user to see what integer the computer picked.

I am still a bit puzzled on the who_won() function.. I understand the first if statement about the game being a time, but the 'if in' statement I don't seem to get probably because I have never seen one.

I guess it is basically saying if comp_move (3) minus user_to_int (2) if that equals either 1, 0, -1, or -2 then the computer wins? Else, user wins? I'm just confused because if the computer gets 3, which is scissors, and the user chooses 2, rock. Shouldn't the user win?

I'm still learning, so I could be interpreting the 'if in' statement incorrectly.

Thanks, Chris.
+1 Chris Nelson · December 31, 2014
Thanks for the quick reply! Yeah that does save a lot of code. Really like the idea of just evaluating the equation part of how to decide who_won. 

Cleared it up nicely. Thanks again!

Have a nice New Year!
0 Chris Nelson · December 31, 2014
Jay,

Would you mind explaining how your code checks if the user or computer won? I'm a bit confused how you could figure it without assigning an integer to 'Scissors' on the computers end, but you do on the users? Also, is there any advantage to using randrange over say.. randint(1, 3)? 

Thanks for any clarification! 
Chris.
  • 1

Python

106,994 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator