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
+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 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

115,875 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator