Skip to content

Instantly share code, notes, and snippets.

@oindrillac
Created February 9, 2018 03:53
Show Gist options
  • Save oindrillac/4d5204c933b4fbb4168d43ccbc416911 to your computer and use it in GitHub Desktop.
Save oindrillac/4d5204c933b4fbb4168d43ccbc416911 to your computer and use it in GitHub Desktop.
import random
#CS542Problemset1
Rprob = 0.0
Sprob = 0.0
Pprob = 0.0
rock = 0.0
scissor = 0.0
paper = 0.0
total = 0
userscore = 0
computerscore = 0
def play(Userplay, Compplay):
global userscore
global computerscore
if Userplay == Compplay:
userscore = userscore + 1
computerscore = computerscore + 1
print("Computer : " + str(Compplay) + "\n Tie! \n" "Score ( You|Computer ) :" + str(userscore) + "|" + str(computerscore))
elif Userplay == "R" and Compplay == "P":
computerscore = computerscore + 1
print("Computer : " + str(Compplay) + "\n You Lose! \n" "Score ( You|Computer ) :" + str(userscore) + "|" + str(computerscore))
elif Userplay == "P" and Compplay == "R":
userscore = userscore + 1
print("Computer : " + str(Compplay) + "\n You Win! \n" "Score ( You|Computer ) :" + str(userscore) + "|" + str(computerscore))
elif Userplay == "S" and Compplay == "R":
computerscore = computerscore + 1
print("Computer : " + str(Compplay) + "\n You Lose! \n" "Score ( You|Computer ) :" + str(userscore) + "|" + str(computerscore))
elif Userplay == "S" and Compplay == "P":
computerscore = computerscore + 1
print("Computer : " + str(Compplay) + "\n You Lose! \n" "Score ( You|Computer ) :" + str(userscore) + "|" + str(computerscore))
elif Userplay == "R" and Compplay == "S":
userscore = userscore + 1
print("Computer : " + str(Compplay) + "\n You Win! \n" "Score ( You|Computer ) :" + str(userscore) + "|" + str(computerscore))
elif Userplay == "P" and Compplay == "S":
computerscore = computerscore + 1
print("Computer : " + str(Compplay) + "\n You Lose! \n" "Score ( You|Computer ) :" + str(userscore) + "|" + str(computerscore))
def probability(count,total):
if total == 0:
total = 1
return float(count/total)
def computernext(input):
global Rprob
global Sprob
global Pprob
global rock
global scissor
global paper
global total
if input == 'R':
rock += 1
total += 1
elif input == 'P':
paper += 1
total += 1
elif input == 'S':
scissor += 1
total += 1
Rprob = probability(rock, total)
Sprob = probability(scissor, total)
Pprob = probability(paper, total)
if max(Rprob, Sprob, Pprob) == Rprob:
return 'P'
elif max(Rprob, Sprob, Pprob) == Sprob:
return 'R'
elif max(Rprob, Sprob, Pprob) == Pprob:
return 'S'
if __name__ == "__main__":
game_set = ['R', 'P', 'S']
firsttime = True
num = 0
nextmove = random.choice(game_set)
n = int(input("How many times would you want to play : "))
while(num <= n):
usermove = input("\n Please choose from [R,P,S] : ")
if(firsttime == True):
firsttime = False
computermove = random.choice(game_set)
play(usermove,computermove)
else:
play(usermove,nextmove)
nextmove = computernext(usermove)
num = num + 1
print("\n************************************\n Your score is : "+str(userscore)+ "\n Computer score is : "+str(computerscore))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment