Skip to content

Instantly share code, notes, and snippets.

@jsomers
Created October 11, 2013 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsomers/6929017 to your computer and use it in GitHub Desktop.
Save jsomers/6929017 to your computer and use it in GitHub Desktop.
A better method of generating Yahtzee-ish dice possibilities in Python
from itertools import product
def iterate_dice_simple(diceNums, diceToHold):
roles = list(product(range(1, 7), repeat=len(diceNums)))
return [r for r in roles if has_held_dice(r, diceNums, diceToHold)]
def has_held_dice(r, diceNums, diceToHold):
for i in range(len(diceToHold)):
if diceToHold[i] == 0: continue
if r[i] != diceNums[i]: return False
return True
print iterate_dice_simple([3, 5, 4], [0, 1, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment