You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6.5 KiB

In [27]:
# generate random ID for the pieces
from shortuuid import uuid

# to draw the background
from wand.image import Image
from wand.drawing import Drawing

# rows and columns for the puzzle
rows = 10
columns = 15

# resolution for the print
dpi = 300

# width and height in cm
image_width = 15
image_height = 10

# name of the puzzle to be more legible?
name = 'katamari'


# width and height in pixel
width = int(dpi / 2.54 * image_width)
height = int(dpi / 2.54 * image_height)

# piece sizes in pixel
piece_width = width / rows
piece_height = height / columns


pieces = []
adjacents = {}


with Image(width = width, height = height) as img:
    draw = Drawing()
    draw.font_size = 11
    draw.text_alignment = 'center'
    
    for x in range(rows):
        row = []
        for y in range(columns):

            # generate a random ID for each piece    
            ID = uuid()[:4]
            
            # write the id in the center of each piece
            draw.text(int(x * piece_width + piece_width/2), int(y*piece_height + piece_height/2), f'{name}\n{ID}')
            
            # store the id in the pieces dictionary
            row.append(ID)
            
        pieces.append(row)
            
    draw(img)
    img.save(filename=f'{name}_retro.png')
    print('Done!')
Done!
[['LdPQ', 'TqTD', 'oXxV', 'SDED', 'f4fi', 'AFuQ', '6aQ6', 'MRkN', '5Yih', 'neh7', '4iFu', 'hcuu', 'MKvC', 'FQah', 'XBV3'], ['kJ2u', 'LhB2', '663j', 'Nge2', 'gJAQ', 'i3fZ', 'DxYS', 'Yu4x', 'SEQx', '26qm', 'EBEq', 'iNFA', 'Usde', '3vWt', 'G9E2'], ['WQ2k', 'R3mh', 'C9x4', 'Nnrz', 'mvF7', '2qUx', 'YVnK', '2SKA', 'mEeF', 'FSKH', 'UNsr', 'FC6x', 'kDbf', 'dGe9', 'kZWC'], ['55Sn', 'gMaU', 'NUXW', 'JkmV', '9zgi', 'UZoN', 'bsvK', 'U67P', '2RLV', 'ktxF', 'Eh3T', 'jExX', 'ZKW9', 'NRAT', '6J8b'], ['ct9T', 'KNwy', 'EvPM', 'is6X', 'Bw4u', 'j7Mc', '9wEb', 'Gbog', 'Rq4G', 'Li4Z', 'e5VP', 'YByC', '5us6', '7HqJ', 'hgzi'], ['F6Jp', 'Sb66', 'F2KR', 'KT4E', 'SsDP', 'jRvw', 'WZGh', 'XmH3', 'YLts', 'XPW5', 'WwQr', '9qHo', 'gYWg', 'XZwu', 'YQNb'], ['GCLu', 'd7q6', 'NgZc', 'jahF', '7826', 'nijr', '5eZR', '4ajp', 'NVT7', 'LU3x', 'fRTu', 'HEbf', 'Ykek', 'm9oE', 'KB8h'], ['Z9a6', 'QEBi', '7Bo6', 'bmBt', 'LcaY', 'RmVm', 'FrPH', 'NYnu', 'kv9R', 'A4CU', 'K6CG', 'NNF8', 'BfFx', 'ktn4', 'RBKH'], ['Zq9j', 'aBXk', 'VJh7', 'iqby', 'Qasx', 'Lttd', 'RVkq', 'kEqx', '6k5s', 'nqEX', '6gXY', 'Fvg6', 'H2FQ', '2TAE', 'CCGn'], ['cUGD', 'GqAj', 'P79K', 'MCZm', 'bddT', '6ZZD', 'XeBV', 'jdZG', 'g8Bp', 'f2yf', '5eHV', 'XZCU', 'UDPC', 'RWXt', 'FAFT']]
In [33]:
print(pieces[0][1])
TqTD
In [32]:
for x in range(rows):
    for y in range(columns):
        
        current = pieces[x][y]
        
        if y > 0:
            n = pieces[x][y-1]
        else:
            n = None
        
        if y < columns - 1:
            s = pieces[x][y+1]
        else:
            s = None
        
        if x > 0:
            w = pieces[x-1][y]
        else: 
            w = None
        
        if x < rows - 1:
            e = pieces[x+1][y]
        else:
            e = None
        
        adjacents[current] = (n, e, s, w)
In [31]:
print(adjacents['LdPQ'])
(None, 'kJ2u', 'TqTD', None)
In [48]:
fragment = ['f4fi', 'AFuQ', 'i3fZ', '2qUx', 'mvF7',]

def is_valid_fragment(pieces):
    valid = True
    for p0 in pieces:
        connection = []
        for p1 in pieces:
            if p0 == p1:
                continue
            connected = p0 in adjacents[p1]
            connection.append(connected)
        if not True in connection:
            valid = False
            print(f'Not valid fragment, piece {p0} is not connected')
            break
    print('The fragment is valid!')
    return valid
            
        
is_valid_fragment(fragment)
The fragment is valid!
Out[48]:
True
In [ ]: