+ Create a fresh deck of Set cards.

+ Cards have 4 characteristics, with 3 varients of each characteristic.

+def deal_cards(deck, hand=12):

+ Pass in a list of cards (the deck), returns a deal of the defined hand size (default 12).

+ deal = [deck.pop(random.randint(0, len(deck)-1)) for i in range(0, hand)]

+def refresh_deal(deck, deal, hand=12):

+ Refresh the deal up to the required hand size.

+ You can pass in a fresh deck, or a used one.

+ while (len(deal) < hand):

+ draw = deck.pop(random.randint(0, len(deck)-1))

+def compare_cards(cards):

+ Pass in a tuple of two cards.

+ Compare two cards. If they only share one characteristic, return True and the

+ index of the matching characteristic as a tuple.

+ Otherwise return False.

+ # Generate a list of strings, containing each type of characteristic of the two cards.

+ # E.g. ['AA','AB','CB','BA']

+ # If the cards match, return False

+ if first_card == second_card: return (False, -1)

+ compare = [first_card[i] + second_card[i] for i in range(0,4)]

+ for index, value in enumerate(compare):

+ # If the first element matches the second element, increment a counter.

+ if elements_matched == 1:

+ # In ONLY one pair of characteristics matched, return True.

+ return (True, elements_index)

+ Find a set of three cards in a deal (a list of cards passed in).

+ # Generate a list of all possible card pairs from this deal, as tuples.

+ card_pairs.append(pair)

+ # Start iterating through this list to find a possible set pair

+ (first_match, index1) = compare_cards((first_card, second_card))

+ # When we find a set pair, take the first of the pair and start to

+ # compare it to the other cards in the deal.

+ for third_card in deal:

+ if third_card == first_card or third_card == second_card:

+ (second_match, index2) = compare_cards((first_card, third_card))

+ # If we found a possible set pair with the same matching characteristic,

+ # return the three cards.

+ if second_match and index1 == index2:

+ return (first_card, second_card, third_card, index1)