Set Solver
A solver for the card game Set
Several years ago, my family and I were very into a board game called Set, in which you find three cards that match in a certain way in a large group of cards. You can read more about the rules of it here. I created a Python script in 2020 that would take as input all the cards and find matches automatically.
Here is the code (Note: This was written in Python 2.7, hence why I use raw_input instead of input):
from itertools import combinations
def findSets(cards):
sets = []
c = list(combinations(cards, 3))
for i in c:
if checkSet(i):
sets.append(i)
return sets
def checkSet(cards):
if cards[0]["color"] == cards[1]["color"] and cards[0]["color"] == cards[2]["color"] and cards[1]["color"] == cards[2]["color"]:
color = True
elif cards[0]["color"] != cards[1]["color"] and cards[0]["color"] != cards[2]["color"] and cards[1]["color"] != cards[2]["color"]:
color = True
else:
color = False
if cards[0]["shape"] == cards[1]["shape"] and cards[0]["shape"] == cards[2]["shape"] and cards[1]["shape"] == cards[2]["shape"]:
shape = True
elif cards[0]["shape"] != cards[1]["shape"] and cards[0]["shape"] != cards[2]["shape"] and cards[1]["shape"] != cards[2]["shape"]:
shape = True
else:
shape = False
if cards[0]["fill"] == cards[1]["fill"] and cards[0]["fill"] == cards[2]["fill"] and cards[1]["fill"] == cards[2]["fill"]:
fill = True
elif cards[0]["fill"] != cards[1]["fill"] and cards[0]["fill"] != cards[2]["fill"] and cards[1]["fill"] != cards[2]["fill"]:
fill = True
else:
fill = False
if cards[0]["number"] == cards[1]["number"] and cards[0]["number"] == cards[2]["number"] and cards[1]["number"] == cards[2]["number"]:
number = True
elif cards[0]["number"] != cards[1]["number"] and cards[0]["number"] != cards[2]["number"] and cards[1]["number"] != cards[2]["number"]:
number = True
else:
number = False
return color and shape and fill and number
def createCard(c_num):
carddict = {}
prompt = str(c_num) + " (c, s, f, n): "
ans = raw_input(prompt)
ans_split = ans.split(",")
carddict["color"] = ans_split[0]
carddict["shape"] = ans_split[1]
carddict["fill"] = ans_split[2]
carddict["number"] = ans_split[3]
return carddict
def p_print(p_val, c_list):
for i in p_val:
print("\n\n")
for j in i:
print("Card: " + str(c_list.index(j) + 1))
card_list = []
num_of_cards = int(raw_input("How many cards? "))
for i in range(num_of_cards):
temp_card = createCard(i+1)
card_list.append(temp_card)
c_sets = findSets(card_list)
p_print(c_sets, card_list)