Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python function random cycling if statements

I'm making a rock paper scissor game and have run into a problem with the decisioncycle(). What I'm trying to do is ask the user to input a choice in usercycle(), have the computer generate a random choice in gamecycle(), then determine who won the round and keep track of each result with a win and loss count. It seems to be deciding when to work at random.

import random


class rpsgame:

    rps= ["rock", "paper","scissors"]


    wincount=0
    losecount=0
    def usercycle(self):
        userchoice = input("rock, paper, scissor.....")
        print("SHOOT")
        return userchoice

    def gamecycle(self):
        computerchoice = random.choice(rpsgame.rps)
        return computerchoice




    def decisioncycle(self):
            if rpsgame.usercycle(self) == rpsgame.rps[0] and rpsgame.gamecycle(self) == rpsgame.rps[1]:
                    print("paper beats rock, you lose!")
                    rpsgame.losecount +=1
            elif rpsgame.usercycle(self) == rpsgame.rps[1] and rpsgame.gamecycle(self) == rpsgame.rps[0]:
                    print("paper beats rock, you win!")
                    rpsgame.wincount+=1
            elif rpsgame.usercycle(self) == rpsgame.rps[0] and rpsgame.gamecycle(self) == rpsgame.rps[2]:
                    print("rock beats scissors, you win!")
                    rpsgame.wincount+=1
            elif rpsgame.usercycle(self) == rpsgame.rps[2] and rpsgame.gamecycle(self) == rpsgame.rps[0]:
                    print("rock beats scissors, you lose!")
                    rpsgame.losecount+=1
            elif rpsgame.usercycle(self) == rpsgame.rps[1] and rpsgame.gamecycle(self) == rpsgame.rps[2]:
                    print("scissors beats paper, you lose!")
                    rpsgame.losecount+=1
            elif rpsgame.usercycle(self) == rpsgame.rps[2] and rpsgame.gamecycle(self) == rpsgame.rps[1]:
                    print("scissors beats paper, you win!")
                    rpsgame.wincount+=1
            elif rpsgame.usercycle(self) == rpsgame.gamecycle(self):
                    print("it's a tie!!!")
            print("wins {}, losses {}".format(rpsgame.wincount, rpsgame.losecount))




while True:
    rg = rpsgame()
    rg.usercycle()
    rg.gamecycle()
    rg.decisioncycle()

I think my problem is in the decisioncycle(). this is my first shot at a class as the game was working with global variables, but I read here that that is a bad practice to carry out for the future.

like image 975
Rayfd3s Avatar asked Apr 19 '15 17:04

Rayfd3s


3 Answers

instead of evaluating each combination using numerous cycles you can use modular arithmetic.

lets say that you make mapping

"rock" => 0
"paper"=>1
"scissors" => 2

you can evaluate solution as

(A.number - B.number) % 3

if this result is 0, it is draw, if it is 1 that A won if 2 A is lost

like image 172
Luka Rahne Avatar answered Nov 14 '22 21:11

Luka Rahne


You are asking for a new user input in each condition. You probably want to read it just once and then compare it each time like

user_choice = self.usercicle()
game_choice = self.gamecycle()
if(user_choice == self.rps[0] and game_choice == self.rps[1]):
    print "Paper beats rock, you lose!"
    self.losecount += 1
elif( user_choice...

and so forth

like image 41
Eric Renouf Avatar answered Nov 14 '22 21:11

Eric Renouf


I think you should make a separate function for determining the winner, and use a dict, rather than a 7-way if statement:

def who_won(player, computer):
    rules = {"rock": "scissors", "paper": "rock", "scissors": "paper"}
    if player == computer:
        return None
    if rules[computer] == player:
        return "computer"
    return "player"

It's probably a good idea to check for invalid input, but that should be done in the input function, rather than this function.

like image 24
1.618 Avatar answered Nov 14 '22 21:11

1.618