2010-08-09

I love Python

It's so easy to quickly code something and calculate something. The following snippet actually computes the number of possible cards in the game Tsuro. The game ships with 64 cards, but my colleague and I suspected that those were not all possible combinations (eliminating all rotational symmetries). So we did some quick calculations, and came up with 105 possible cards. So I wrote this short Python code to enumerate all possible cards then:
Update: My first algorithm was wrong. The actual number of cards is 35. It is only 105, when the numbering of the conncetion points is of interest. So here is the augmented algorithm, which outputs the 35 unique cards. The game comes with two of every card, except for 6 highly symmetric cards, which are unique in the game.

def makeCards(cards, card, connectionSet):
if connectionSet == set([]):
for i in range(1,4):
c = shiftCard(card, i)
try:
cards.index(c)
return cards
except ValueError:
pass
return cards + [card]
start = connectionSet.pop()
for i in connectionSet:
newConnection = sorted([start, i])
cards = makeCards(cards, sorted(card + [newConnection]), connectionSet - set([i]))
return cards

def shiftCard(card, i):
newCard = []
for c in card:
conn = sorted([(c[0] + 2 * i) % 8, (c[1] + 2 * i) % 8])
newCard = sorted(newCard + [conn])
return newCard

mycards = makeCards([], [], set([0,1,2,3,4,5,6,7]))
print mycards
print len(mycards)

No comments:

Post a Comment