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.
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With