import random
from athenaCL.libATH import dialog
def fill(spaces, max_value):
# sources size = number of voices moving from
# destination size = number of voices moving to
# spaces is the number of spaces to fill
# max_value is the highest int to be represented in the list, starting from 0
value_l = range(0,(max_value))
test_map = []
for i in range(0, spaces):
test_map.append(0) # fill test with 0s
strEntries = [] # stores output for printing
found_maps = []
counter = 0
temp_value_l = []
# fill with all possible values
for entry in value_l: # duplicate list
temp_value_l.append(entry)
while 1:
counter = counter + 1
i = 0 # counter for location in map
for space in test_map:
# refull the temp value list of only one value left
if len(temp_value_l) <= 1: # used to be 0
# reset value list of emptied
for entry in value_l:
temp_value_l.append(entry)
# choose a random location in temp
value_loc = random.randint(0, (len(temp_value_l)-1))
# asign to map from all possible values
test_map[i] = temp_value_l[value_loc]
# remove from list of values
temp_value_l.remove(test_map[i])
i = i + 1
for val in value_l:
if test_map.count(val) == 0:
continue
score_l = []
for val in test_map:
if val not in score_l:
score_l.append(val)
score_l.sort()
if score_l != value_l:
continue
# add all rotations to found maps
for i in range(0,len(test_map)):
rot_map = test_map[i:] + test_map[:i]
if rot_map in found_maps:
continue
else:
found_maps.append(rot_map)
# maximum number of attempts
if counter >= 10000:
break
found_maps.sort()
# convert all maps to tuples
for i in range(0, len(found_maps)):
found_maps[i] = tuple(found_maps[i])
key = 0
for entry in found_maps:
key = key + 1
str = "%2i : %r" % (key, entry)
strEntries.append(str)
return strEntries
#-----------------------------------------------------------------||||||||||||--
#-----------------------------------------------------------------||||||||||||--
class genMC:
def __init__(self, min_card=1, max_card=3):
f = dialog.Print_to_File()
card_1 = [(1,1),] # returns an error
card_2 = [(2,2), (2,1)]
card_3 = [(3,3), (3,2), (3,1)]
card_4 = [(4,4), (4,3), (4,2), (4,1)]
card_5 = [(5,5), (5,4), (5,3), (5,2), (5,1)]
card_6 = [(6,6), (6,5), (6,4), (6,3), (6,2), (6,1)]
allGroups = [card_1, card_2, card_3, card_4, card_5, card_6]
#print allGroups
for group in allGroups:
print '#----------------------------'
for pair in group:
sourceCard, destCard = pair
spaces = sourceCard
max_value = destCard # 0 counts, so this value is less then card num
strEntries = fill(spaces, max_value)
for i in range(0,len(strEntries)):
if i ==0 and i == (len(strEntries)-1):
print 'map_%i_%i ={%s}' % (sourceCard, destCard, strEntries[i])
elif i ==0:
print 'map_%i_%i ={%s,' % (sourceCard, destCard, strEntries[i])
elif i ==len(strEntries)-1:
print ' %s}\n' % (strEntries[i])
else:
print ' %s,' % strEntries[i]
name, pathname = f.close_file() #/*returns filename*/
|