Created
July 23, 2024 15:06
-
-
Save 0sn/abc01a1fb49ad6e2ae2b4d29e97a9afc to your computer and use it in GitHub Desktop.
testing shuffles and guessing of different decks
This file contains hidden or 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
from random import shuffle, randrange | |
from statistics import mean | |
import multiprocessing as mp | |
cards = {} | |
# Uno All Wild deck | |
# cards = { | |
# 'wild': 54, | |
# 'reverse': 14, | |
# 'skip': 14, | |
# 'skiptwo': 6, | |
# 'drawtwo': 10, | |
# 'drawfour': 6, | |
# 'targetdrawtwo': 4, | |
# 'fullswap': 4 | |
# } | |
# Uno Deck (without blanks) | |
# colours = ['red','blue','green','yellow'] | |
# for i in range(10): | |
# for colour in colours: | |
# cards[colour+str(i)] = 2 | |
# special = ['skip', 'reverse', 'draw'] | |
# for spec in special: | |
# for colour in colours: | |
# cards[spec+colour] = 2 | |
# cards['wild'] = 4 | |
# cards['wildfour'] = 4 | |
# regular deck of cards | |
suits = ['club', 'spade', 'heart', 'diamond'] | |
fancy = ['ace', 'jack', 'queen', 'king'] | |
for suit in suits: | |
for i in range(1,11): | |
cards[suit+str(i)] = 1 | |
for fan in fancy: | |
cards[suit+fan] = 1 | |
def build_deck(cards): | |
deck = [] | |
for card_type in cards: | |
deck += [card_type] * cards[card_type] | |
return deck | |
def riffle(left, right): | |
if len(left) and len(right): | |
choice = randrange(2) | |
if choice: | |
card = right.pop(0) | |
else: | |
card = left.pop(0) | |
return [card] + riffle(left, right) | |
elif len(left): | |
return left | |
else: | |
return right | |
def repeat_riffle(deck, times): | |
for i in range(times): | |
half = int(len(deck)/2) | |
left = deck[:half] | |
right = deck[half:] | |
deck = riffle(left, right) | |
return deck | |
def guess(deck): | |
wins = 0 | |
guesses = {} | |
for card_type in cards: | |
guesses[card_type] = cards[card_type] | |
wins = 0 | |
for card in deck: | |
total_cards = sum(guesses.values()) | |
odds = {card_type: guesses[card_type]/total_cards for card_type in guesses} | |
max_value = max(odds, key=odds.get) | |
guesses[max_value] -= 1 | |
if max_value == card: | |
wins += 1 | |
return wins | |
def unshuffled(count): | |
deck = build_deck(cards) | |
return guess(deck) | |
def randomized(count): | |
deck = build_deck(cards) | |
shuffle(deck) | |
return guess(deck) | |
def shuffled_one(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 1) | |
return guess(shuffled) | |
def shuffled_two(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 2) | |
return guess(shuffled) | |
def shuffled_three(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 3) | |
return guess(shuffled) | |
def shuffled_four(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 4) | |
return guess(shuffled) | |
def shuffled_five(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 5) | |
return guess(shuffled) | |
def shuffled_six(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 6) | |
return guess(shuffled) | |
def shuffled_seven(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 7) | |
return guess(shuffled) | |
def shuffled_eight(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 8) | |
return guess(shuffled) | |
def shuffled_nine(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 9) | |
return guess(shuffled) | |
def shuffled_ten(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 10) | |
return guess(shuffled) | |
def shuffled_eleven(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 11) | |
return guess(shuffled) | |
def shuffled_twelve(count): | |
deck = build_deck(cards) | |
shuffled = repeat_riffle(build_deck(cards), 12) | |
return guess(shuffled) | |
if __name__ == '__main__': | |
testcount = 10000 | |
pool = mp.Pool() | |
print('Unshuffled\t', mean(pool.map(unshuffled, range(testcount)))) | |
print('Shuffles',1,'\t', mean(pool.map(shuffled_one, range(testcount)))) | |
print('Shuffles',2,'\t', mean(pool.map(shuffled_two, range(testcount)))) | |
print('Shuffles',3,'\t', mean(pool.map(shuffled_three, range(testcount)))) | |
print('Shuffles',4,'\t', mean(pool.map(shuffled_four, range(testcount)))) | |
print('Shuffles',5,'\t', mean(pool.map(shuffled_five, range(testcount)))) | |
print('Shuffles',6,'\t', mean(pool.map(shuffled_six, range(testcount)))) | |
print('Shuffles',7,'\t', mean(pool.map(shuffled_seven, range(testcount)))) | |
print('Shuffles',8,'\t', mean(pool.map(shuffled_eight, range(testcount)))) | |
print('Randomized\t', mean(pool.map(randomized, range(testcount)))) | |
pool.close() | |
# testcount = 10 | |
# wincount = [] | |
# for i in range(testcount): | |
# deck = build_deck(cards) | |
# wincount.append(guess(deck)) | |
# print('Unshuffled\t',mean(wincount)) | |
# wincount = [] | |
# for i in range(testcount): | |
# deck = build_deck(cards) | |
# shuffle(deck) | |
# wincount.append(guess(deck)) | |
# print('Randomized\t',mean(wincount)) | |
# for total_shuffles in range(1,12): | |
# wincount = [] | |
# for i in range(total_shuffles): | |
# shuffled = repeat_riffle(build_deck(cards), total_shuffles) | |
# wincount.append(guess(shuffled)) | |
# print('Shuffle', total_shuffles,'\t',mean(wincount)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment