patternpythonMinor
RockPaperScissor in Python
Viewed 0 times
pythonrockpaperscissorstackoverflow
Problem
This is a simple Rock - Paper - Scissor game. I'm trying to find a better way to write the algorithm and keeping the scoreboard.
```
#!/usr/bin/python
#
import random
import os
import json
import pprint
#-------------------------------------------------#
def main():
user, sys = 0, 0
name = os.getlogin()
while True:
dictionary = {1:'Rock', 2:'Paper', 3:'Scissors'}
sysChoice = random.choice(dictionary.keys())
try:
userChoice = int(raw_input("Select One [1:Rock, 2:Paper, 3:Scissors, 0:Exit] : "))
if (userChoice in (1,2,3)):
print "Computer Choice is : ", sysChoice, dictionary[sysChoice]
print "Your Choice is : ", userChoice, dictionary[userChoice]
result = rockPaperScissors(sysChoice, userChoice)
print "Result : ", result
if (result == 'Win'):
user = user + 1
sys = 0
scoreBoard(user, sys, name)
elif (result == 'Lose'):
user = 0
sys = sys + 1
scoreBoard(user, sys, name)
else:
sys, user = 0, 0
scoreBoard(user, sys, name)
elif(userChoice == 0):
topScore()
break
else:
continue
except ValueError:
continue
#-------------------------------------------------#
def rockPaperScissors (a, b):
if ((a) % 3 + 1 == b):
return "Win";
elif ((b) % 3 + 1 == a):
return "Lose"
else:
return "Draw"
#-------------------------------------------------#
def scoreBoard(u, s, name):
if (os.path.isfile("score.json")):
jsonFile = open("score.json", "r")
data = json.load(jsonFile)
jsonFile.close()
if name in data:
print "Current Score : ", u, s
```
#!/usr/bin/python
#
import random
import os
import json
import pprint
#-------------------------------------------------#
def main():
user, sys = 0, 0
name = os.getlogin()
while True:
dictionary = {1:'Rock', 2:'Paper', 3:'Scissors'}
sysChoice = random.choice(dictionary.keys())
try:
userChoice = int(raw_input("Select One [1:Rock, 2:Paper, 3:Scissors, 0:Exit] : "))
if (userChoice in (1,2,3)):
print "Computer Choice is : ", sysChoice, dictionary[sysChoice]
print "Your Choice is : ", userChoice, dictionary[userChoice]
result = rockPaperScissors(sysChoice, userChoice)
print "Result : ", result
if (result == 'Win'):
user = user + 1
sys = 0
scoreBoard(user, sys, name)
elif (result == 'Lose'):
user = 0
sys = sys + 1
scoreBoard(user, sys, name)
else:
sys, user = 0, 0
scoreBoard(user, sys, name)
elif(userChoice == 0):
topScore()
break
else:
continue
except ValueError:
continue
#-------------------------------------------------#
def rockPaperScissors (a, b):
if ((a) % 3 + 1 == b):
return "Win";
elif ((b) % 3 + 1 == a):
return "Lose"
else:
return "Draw"
#-------------------------------------------------#
def scoreBoard(u, s, name):
if (os.path.isfile("score.json")):
jsonFile = open("score.json", "r")
data = json.load(jsonFile)
jsonFile.close()
if name in data:
print "Current Score : ", u, s
Solution
You use a variable named
There are really three things being associated: the move names, the user input, and whatever internal representation you use. You currently have the user input being equivalent to the internal representation; that's okay. Initially I was against the idea of using a dictionary, but doing so allows you to use any user input.
Whatever variable name and type you use, the data structure does not need to be reinitialized in each loop. Set it once and leave it (though this may not affect your speed)
This is duplicated code. Factor it out into a new function.
Why do you have
dictionary. This is not helpful, as it's just the variable's type. Use meaningful variable names, how about handsigns or shapes or commands.There are really three things being associated: the move names, the user input, and whatever internal representation you use. You currently have the user input being equivalent to the internal representation; that's okay. Initially I was against the idea of using a dictionary, but doing so allows you to use any user input.
Whatever variable name and type you use, the data structure does not need to be reinitialized in each loop. Set it once and leave it (though this may not affect your speed)
userChoice = int(raw_input("Select One [1:Rock, 2:Paper, 3:Scissors, 0:Exit] : ")) I don't like this, because it duplicates the contents of your dictionary structure. You should just have one copy of the mapping, and refer to it here. if (userChoice in (1,2,3)): same thing with this. You should just check if it's one of the keys.print "Computer Choice is : ", sysChoice, dictionary[sysChoice]
print "Your Choice is : ", userChoice, dictionary[userChoice]This is duplicated code. Factor it out into a new function.
user and sys are poor variable names. It took a while before I realized that they meant the length of the user's and computer's current winning streaks. The variable name should reflect its purpose.Why do you have
else and except blocks that do literally nothing?Code Snippets
print "Computer Choice is : ", sysChoice, dictionary[sysChoice]
print "Your Choice is : ", userChoice, dictionary[userChoice]Context
StackExchange Code Review Q#86675, answer score: 2
Revisions (0)
No revisions yet.