Rock-paper-scissors: Difference between revisions

Line 552:
=={{header|Python}}==
<lang python>#!/usr/bin/python
from random import randintchoice, randomrandrange
from operatorbisect import addbisect
from functoolscollections import reducedefaultdict
from bisect import bisect_left
 
WHATBEATS = { 'paper' : 'scissors',
Line 563 ⟶ 562:
ORDER = ('rock', 'paper', 'scissors')
 
CHOICEFREQUENCY = {}defaultdict(int)
 
def probChoice(choices, probabilities):
scalefactortotal = reducesum(add, probabilities)
prob_accumulator = 0
accumulator = []
for p in probabilities:
prob_accumulator += float(p) / scalefactor
accumulator.append(prob_accumulator)
r = randomrandrange(total)
bsct = bisect_leftbisect(accumulator, r)
chc = choices[bsct]
return chc
 
def checkWinner(a, b):
globalif b == WHATBEATS[a]:
 
abeater = WHATBEATS[a]
bbeater = WHATBEATS[b]
 
if b == abeater:
return b
elif a == bbeaterWHATBEATS[b]:
return a
 
Line 595 ⟶ 589:
 
def registerPlayerChoice(choice):
global CHOICEFREQUENCY[choice] += 1
if choice in CHOICEFREQUENCY:
CHOICEFREQUENCY[choice] += 1
else:
CHOICEFREQUENCY[choice] = 1
 
def getRandomChoice():
if len(CHOICEFREQUENCY[choice]) +== 10:
global WHATBEATS
global return choice(ORDER)
global CHOICEFREQUENCY
if len(CHOICEFREQUENCY.keys()) == 0:
return ORDER[randint(0,len(ORDER)-1)]
choices = CHOICEFREQUENCY.keys()
probabilities = CHOICEFREQUENCY.values()
Line 612 ⟶ 599:
 
while True:
choicehumanChoice = raw_input()
choicehumanChoice = sanitizeChoice(choicehumanChoice)
if nothumanChoice choicenot in ORDER:
continue
 
Line 622 ⟶ 609:
# Don't register the player choice until after the computer has made
# its choice.
registerPlayerChoice(choicehumanChoice)
 
winner = checkWinner(choicehumanChoice, compChoice)
 
if winner == None:
Anonymous user