Skip to content

Instantly share code, notes, and snippets.

@justanr
Created August 13, 2014 11:00
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 justanr/7e1cd1c5f79af2f4fe55 to your computer and use it in GitHub Desktop.
Save justanr/7e1cd1c5f79af2f4fe55 to your computer and use it in GitHub Desktop.
Cards Against Python
from collections import namedtuple, deque
from random import shuffle
from functools import partial
from itertools import chain
WhiteCard = namedtuple('WhiteCard', 'text', verbose=False)
BlackCard = namedtuple('BlackCard', ['text', 'pick', 'draw'], verbose=False)
white_text = ['itertools', 'functools', 'fibonacci sequence', 'WSGI',
'a list comphrension', 'Javascript', 'RESTful', 'C++',
'messy inheritance', 'an unclear variable name',
"a class that's actually a closure", 'overriding a built-in',
'Java style getters and setters', 'Ruby', 'a blank except line',
'a lambda', '__future__', 'braces', 'beautiful and idiomatic',
'Pythonic', 'spam', 'eggs', 'wheels', 'packaging', '*'
]
black_text = ['{} nested inside {}', 'from {} import {}',
'Using Python with {}', "Implementing {} only to find it's in {}",
'Treating Python like {}', 'Python: The Old {}',
'The next feature Guido should implement is {}',
'{} in Python is like {}'
]
white_cards = [WhiteCard(text) for text in white_text]
black_cards = [BlackCard(*fields) for fields in \
[(text, text.count('{}'), text.count('{}')) for text in \
black_text]
]
white_discard = []
black_discard = []
shuffle(white_cards)
shuffle(black_cards)
max_cards = 5
players = 3
Hand = partial(deque, maxlen=max_cards)
def main():
global white_cards, black_cards, hands, white_discard, black_discard
while True:
print("dealing hands")
hands = [Hand(white_cards.pop() for _ in range(max_cards)) for _ in range(players)]
while black_cards:
card = black_cards.pop()
picked = [[hand.pop() for _ in range(card.pick)] for hand in hands]
if not len(white_cards) > len(hands) * card.draw:
print("Out of white cards, restocking and shuffling.")
white_cards.extend(white_discard)
shuffle(white_cards)
white_discard = []
for hand in hands:
hand.extend(white_cards.pop() for _ in range(card.draw))
for pick in picked:
print(card.text.format(*[c.text for c in pick]))
input("")
white_discard.extend(chain.from_iterable(picked))
black_discard.append(card)
print("Out of black cards, restocking and shuffling.")
black_cards.extend(black_discard)
white_cards.extend(white_discard)
white_cards.extend(chain.from_iterable(hands))
shuffle(black_cards)
shuffle(white_cards)
black_discard = []
white_discard = []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment