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.
puzzle-retro/pieces-background.ipynb

228 lines
6.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 27,
"id": "7b187c4c-1c53-494d-a30f-fa0431cd4665",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Done!\n",
"[['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']]\n"
]
}
],
"source": [
"# generate random ID for the pieces\n",
"from shortuuid import uuid\n",
"\n",
"# to draw the background\n",
"from wand.image import Image\n",
"from wand.drawing import Drawing\n",
"\n",
"# rows and columns for the puzzle\n",
"rows = 10\n",
"columns = 15\n",
"\n",
"# resolution for the print\n",
"dpi = 300\n",
"\n",
"# width and height in cm\n",
"image_width = 15\n",
"image_height = 10\n",
"\n",
"# name of the puzzle to be more legible?\n",
"name = 'katamari'\n",
"\n",
"\n",
"# width and height in pixel\n",
"width = int(dpi / 2.54 * image_width)\n",
"height = int(dpi / 2.54 * image_height)\n",
"\n",
"# piece sizes in pixel\n",
"piece_width = width / rows\n",
"piece_height = height / columns\n",
"\n",
"\n",
"pieces = []\n",
"adjacents = {}\n",
"\n",
"\n",
"with Image(width = width, height = height) as img:\n",
" draw = Drawing()\n",
" draw.font_size = 11\n",
" draw.text_alignment = 'center'\n",
" \n",
" for x in range(rows):\n",
" row = []\n",
" for y in range(columns):\n",
"\n",
" # generate a random ID for each piece \n",
" ID = uuid()[:4]\n",
" \n",
" # write the id in the center of each piece\n",
" draw.text(int(x * piece_width + piece_width/2), int(y*piece_height + piece_height/2), f'{name}\\n{ID}')\n",
" \n",
" # store the id in the pieces dictionary\n",
" row.append(ID)\n",
" \n",
" pieces.append(row)\n",
" \n",
" draw(img)\n",
" img.save(filename=f'{name}_retro.png')\n",
" print('Done!')"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "ea7900fd-a448-4101-87d9-214a60283579",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"TqTD\n"
]
}
],
"source": [
"print(pieces[0][1])"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "b77b827b-4bc1-4f20-b33e-7c8f6d0118c7",
"metadata": {},
"outputs": [],
"source": [
"for x in range(rows):\n",
" for y in range(columns):\n",
" \n",
" current = pieces[x][y]\n",
" \n",
" if y > 0:\n",
" n = pieces[x][y-1]\n",
" else:\n",
" n = None\n",
" \n",
" if y < columns - 1:\n",
" s = pieces[x][y+1]\n",
" else:\n",
" s = None\n",
" \n",
" if x > 0:\n",
" w = pieces[x-1][y]\n",
" else: \n",
" w = None\n",
" \n",
" if x < rows - 1:\n",
" e = pieces[x+1][y]\n",
" else:\n",
" e = None\n",
" \n",
" adjacents[current] = (n, e, s, w)\n"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "2a93542f-78fa-4ba4-b83b-8036165e463a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(None, 'kJ2u', 'TqTD', None)\n"
]
}
],
"source": [
"print(adjacents['LdPQ'])"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "aff104cd-e2ff-4471-968d-93b22f0481df",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The fragment is valid!\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fragment = ['f4fi', 'AFuQ', 'i3fZ', '2qUx', 'mvF7',]\n",
"\n",
"def is_valid_fragment(pieces):\n",
" valid = True\n",
" for p0 in pieces:\n",
" connection = []\n",
" for p1 in pieces:\n",
" if p0 == p1:\n",
" continue\n",
" connected = p0 in adjacents[p1]\n",
" connection.append(connected)\n",
" if not True in connection:\n",
" valid = False\n",
" print(f'Not valid fragment, piece {p0} is not connected')\n",
" break\n",
" print('The fragment is valid!')\n",
" return valid\n",
" \n",
" \n",
"is_valid_fragment(fragment)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e0cafe7-ce47-4a24-a41f-d7949f632225",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}