Commits

Ashley Felton  committed ffcd04f

Initial commit.

  • Participants

Comments (0)

Files changed (2)

+#regex syntax
+\.pyc$
+import random
+
+def create_deck():
+    '''
+    Create a fresh deck of Set cards.
+    Cards have 4 characteristics, with 3 varients of each characteristic.
+    '''
+    chars = ('A','B','C')
+    deck = []
+    for a in chars:
+        for b in chars:
+            for c in chars:
+                for d in chars:
+                    card = a + b + c + d
+                    deck.append(card)
+    return deck
+
+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)]
+    return deal
+
+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))
+        if draw in deal:
+            deck.append(draw)
+        else:
+            deal.append(draw)
+    return deal
+
+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']
+    first_card = cards[0]
+    second_card = cards[1]
+    # 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)]
+    elements_matched = 0
+    elements_index = 0
+    for index, value in enumerate(compare):
+        # If the first element matches the second element, increment a counter.
+        if value[0]==value[1]:
+            elements_matched += 1
+            elements_index = index 
+    if elements_matched == 1:
+        # In ONLY one pair of characteristics matched, return True.
+        return (True, elements_index) 
+    else:
+        return (False, -1)
+
+def find_set(deal):
+    '''
+    Find a set of three cards in a deal (a list of cards passed in).
+    '''
+    cards_set = []
+    first_match = False
+    second_match = False
+    index1 = -1
+    index2 = -1
+    # Generate a list of all possible card pairs from this deal, as tuples.
+    card_pairs = []
+    for i in deal:
+        for j in deal:
+            pair = (i, j)
+            if not i == j:
+                card_pairs.append(pair)
+    # Start iterating through this list to find a possible set pair
+    for i in card_pairs:
+        first_card = i[0]
+        second_card = i[1]
+        (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.
+        if first_match:
+            for third_card in deal:
+                if third_card == first_card or third_card == second_card:
+                    pass
+                (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)