Skip to content

Instantly share code, notes, and snippets.

@fweez
Created August 20, 2011 17:18
Show Gist options
  • Save fweez/1159364 to your computer and use it in GitHub Desktop.
Save fweez/1159364 to your computer and use it in GitHub Desktop.
Roschambo multiperson game simulator
#!/usr/bin/env python
import random
PLAYER_COUNT = 5
players = [ None ] * PLAYER_COUNT
ROCK = 1
PAPER = 2
SCISSORS = 3
player_names = {"Seamus":0, "Ryan":0, "Laurel":0, "Jenn":0, "Ariel":0}
pkeys = sorted(player_names.keys())
one_throw = 0
two_throw = 0
all_different = 0
total = 0
THROW_COUNT = 100000
def find_winner(a, b):
r = a - b
if r == 0:
return None
if r == 1: # paper v rock, scissors v paper
return a
if r == -2: # rock v scissors
return a
return b
while total < THROW_COUNT:
total += 1
for i in range(len(players)):
players[i] = random.randint(1,3)
uniques = set(players)
if len(uniques) == 1:
one_throw += 1
if len(uniques) == 2:
(a, b) = uniques
winner = find_winner(a,b)
for i,k in enumerate(pkeys):
if players[i] == winner:
player_names[k] += 1
two_throw += 1
else:
all_different += 1
print "One: %0.4f percent" % (100 * (float(one_throw) / total))
print "Two: %0.4f percent" % (100 * (float(two_throw) / total))
print "All Different: %0.4f percent" % (100 * (float(all_different) / total))
print "Winners:"
for k,v in player_names.iteritems():
print " ", k, "won", v, "times"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment