Skip to content

Instantly share code, notes, and snippets.

@pberkes
Created January 28, 2019 20:01
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 pberkes/b5df12c4100f8b37060798b32ae69866 to your computer and use it in GitHub Desktop.
Save pberkes/b5df12c4100f8b37060798b32ae69866 to your computer and use it in GitHub Desktop.
def random_deck():
# initialize cards deck
cards = numpy.array([card for card in itertools.product(range(nfeatures),
repeat=ndims)]).T
n = cards.shape[1]
# shuffle
return cards[:, numpy.random.permutation(n)]
ncards = 12
def onegame():
nsolutions = []
deck = random_deck()
pos = ncards
cards = deck[:, :pos]
while True:
# find all sets
sets = find_sets2(cards)
nsets = len(sets)
if nsets > 0:
# choose a random set
chosen = sets[numpy.random.randint(len(sets))]
# remove cards from chosen set
idx = [i for i in range(cards.shape[1]) if i not in chosen]
cards = cards[:, idx]
# add new cards
if cards.shape[1] < 12:
nadd = 12 - cards.shape[1]
cards = numpy.concatenate((cards, deck[:, pos:pos + nadd]), axis=1)
pos += nadd
else:
if pos >= deck.shape[1]:
break # game is over
# add additional cards
cards = numpy.concatenate((cards, deck[:, pos:pos + 3]), axis=1)
pos += 3
nsolutions.append(nsets)
return nsolutions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment