Last active
December 19, 2015 15:49
-
-
Save swirepe/5979263 to your computer and use it in GitHub Desktop.
LCR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# let's play LCR! | |
# http://en.wikipedia.org/wiki/LCR_(dice_game) | |
from random import choice, shuffle, random | |
def makeDie(L=1,C=1,R=1,D=3): | |
print "Making a die with " + str(L + C + R + D) + " sides." | |
die = ([-1] * L) + (["pot"] * C) + ([1] * R) + ([0] * D) | |
return die | |
def makeNDice(n=3): | |
return [makeDie() for _ in xrange(n)] | |
def makeRandomDie(sides=6, weights=[1.0/6.0, 1.0/6.0, 1.0/6.0, 3.0/6.0]): | |
"""Weights: L C R D""" | |
# make it the right length and normalize | |
weights = weights[0:4] + ([1.0] * (4 - len(weights))) | |
totalWeight = sum(weights) | |
weights = [w / totalWeight for w in weights] | |
die = [] | |
sideVals = (-1, "pot", 1, 0) | |
# since we add the weights together as we go along, the last index | |
# will have weight of 1, meaning we are guaranteed to pick a side | |
for _ in range(sides): | |
currWeight = 0.0 | |
for side, weight in zip(sideVals, weights): | |
currWeight += weight | |
if random() < currWeight: | |
die.append(side) | |
break | |
return die | |
def makeNRandomDice(n=3): | |
return [makeRandomDie() for _ in range(n)] | |
def makePlayers(numPlayers=4): | |
return [3] * numPlayers | |
def keepGoing(players): | |
"""Stop if only one player has tokens, otherwise keep going""" | |
return sum([score > 0 for score in players]) >= 2 | |
def play(players, dice): | |
pot = 0 | |
rounds = 0 | |
while keepGoing(players): | |
rounds += 1 | |
for playerIndex, score in enumerate(players): | |
if not keepGoing(players): | |
break | |
shuffle(dice) # assume the dice aren't all the same; mix it up. | |
for die in dice[0:min(len(dice),score)]: | |
roll = choice(die) | |
players[playerIndex] -= 1 | |
if roll is "pot": | |
pot += 1 | |
else: | |
players[(playerIndex + roll) % len(players)] += 1 | |
if players[playerIndex] == 0: | |
break | |
print "Rounds taken: ", rounds | |
winner = (player for player, score in enumerate(players) if score > 0).next() | |
print "Winner: Player", winner | |
print "Pot worth ", pot | |
return (rounds, winner, pot) | |
def playWithNames(names, dice): | |
shuffle(names) | |
print "Playing with", names | |
rounds, winner, dice = play(makePlayers(len(names)), dice) | |
print names[winner], "is the best!" | |
play(makePlayers(100), makeNDice(10)) | |
playWithNames(["adams", "madison", "van buren", "tyler", "polk"], makeNDice(3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment