Skip to content

Instantly share code, notes, and snippets.

@da-steve101
Last active October 31, 2017 04:11
Show Gist options
  • Save da-steve101/73715fc707129798f9eb1013c93d5332 to your computer and use it in GitHub Desktop.
Save da-steve101/73715fc707129798f9eb1013c93d5332 to your computer and use it in GitHub Desktop.
A script to keep track of cards when playing pandemic season 2. It creates pseudo second deck to keep track of the information you should already know.
#! /usr/bin/python3
'''
deck = List[Set[Card]]
commands:
(d)iscard - discard the current infection rate of cards
(e)pidemic - input the epidemic card from the bottom
(r)emove - certain cards can remove cards from the discard deck. this command does this
(u)ndo - revert the last command if mistyped
(s)ave - save the current state of the deck to a file
(l)oad - restore the state of the deck from a file
script creates the deck from the dict city_names
edit the dict when new cities are added
it begins by asking for the 9 setup discards
then waits for input
'''
import readline
import copy
import pickle
city_names = {
"Istanbul" : 3,
"Cairo" : 3,
"Tripoli" : 3,
"London" : 3,
"New York" : 3,
"Washington" : 3,
"Jacksonville" : 3,
"Lagos" : 3,
"Sao Paulo" : 3
}
size_of_deck = -1
num_epidemics = 5
epidemic_distribution = []
class Card:
def __init__( self, name ):
self.name = name
def __str__( self ):
return self.name
def __print__( self ):
return self.__str__()
def create_deck( initial_cities ):
deck_set = set([])
for name in initial_cities:
for i in range( initial_cities[name] ):
deck_set.add(Card( name ))
return deck_set
def find_card( card_set ):
while True:
card_name = input( "Card name? " )
for card in card_set:
if card.name == card_name:
return card
print( "Card not found in set: " + card_name )
def discard_card( deck ):
card = find_card( deck[1] )
deck[1].remove( card )
deck[0].add( card )
# manage deck to remove empty sets
return [ s for s in deck if len(s) > 0 ]
def epidemic_card( deck ):
card = find_card( deck[-1] )
deck[-1].remove( card )
deck[0].add( card )
return [ set() ] + [ s for s in deck if len( s ) > 0 ]
def display_deck( deck, epidemics, card_cntr, infection_rate ):
# plus one as ends when trying to draw cards
turns = ( ( size_of_deck - card_cntr ) / 2 ) + 1
# current cards gone
curr_cards = 0
for i in range( epidemics ):
curr_cards += epidemic_distribution[i]
# remaining cards in this epidemics
rem_cards = 0
if epidemics < len(epidemic_distribution):
rem_cards = epidemic_distribution[ epidemics ]
# safe turns in this epidemic
safe_turns = 0
if curr_cards > card_cntr:
safe_turns = ( curr_cards - card_cntr ) / 2
# till gaurenteed epidemic
epi_turns = ( curr_cards + rem_cards - card_cntr ) / 2
print( "you have " + str(turns) + " turns left and had " + str(epidemics) + " epidemics" )
if epidemics < num_epidemics:
print( "safe for " + str( safe_turns ) + " turns from epidemic" )
print( "you have " + str( epi_turns ) + " turns until you must have an epidemic" )
for i, card_set in enumerate(deck):
card_counts = {}
for card in card_set:
if card.name in card_counts:
card_counts[ card.name ] += 1
else:
card_counts[ card.name ] = 1
if i == 0:
print( "Discards" )
else:
print( "Set " + str(i - 1) )
for name in card_counts:
# print probability too based on infection rate
print( " " + name + " = " + str(card_counts[name])
+ "/" + str(len(card_set)))
def remove_from_discard( deck ):
di = 0
if len(deck[0]) == 0:
# was epidemic so instead remove from set
di = 1
card = find_card( deck[di] )
deck[di].remove( card )
return [ s for s in deck if len( s ) > 0 ]
def getCompleter( city_names ):
def completer(text, state):
options = [ name for name in city_names.keys() if name.startswith(text)]
if state < len(options):
return options[state]
else:
return None
return completer
if __name__ == "__main__":
deck = [ set(), create_deck( city_names ) ]
size_of_deck = int( input( "How big is deck ( including epidemics )? " ) )
num_epidemics = int( input( "How many epidemics are in use? " ) )
deck_old = copy.deepcopy( deck )
infection_rate = 2
epidemics = 0
card_cntr = 0
epi_cards = int( size_of_deck / num_epidemics )
remaining_cards = size_of_deck - epi_cards
epidemic_distribution = [ epi_cards for i in range(num_epidemics) ]
for i in range( num_epidemics ):
if i < remaining_cards:
epidemic_distribution[i] += 1
readline.parse_and_bind("tab: complete")
readline.set_completer( getCompleter( city_names ) )
print( "What are the first 9 setup discards?" )
for i in range( 9 ):
deck = discard_card( deck )
# command loop
while True:
cmd = input("(d)iscard/(e)pidemic/(r)emove/(u)ndo/(s)ave/(l)oad? ")
if cmd in ["e", "d", "r"]:
deck_old = copy.deepcopy( deck )
if cmd == "e":
infection_rate = int(input( "infection rate? " ))
deck = epidemic_card( deck )
epidemics += 1
if cmd == "d":
for i in range(infection_rate):
deck = discard_card( deck )
card_cntr += 2
if cmd == "r":
deck = remove_from_discard( deck )
if cmd == "u":
deck = deck_old
if cmd == "s":
file_name = input( "filename? " )
f = open( file_name, "wb" )
pickle.dump( deck, f )
f.close()
if cmd == "l":
file_name = input( "filename? " )
f = open( file_name, "rb" )
deck = pickle.load( f )
f.close()
display_deck( deck, epidemics, card_cntr, infection_rate )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment