notebookvb
This commit is contained in:
@@ -24,7 +24,7 @@ _fonts_dir = os.path.join(os.environ.get("WINDIR", r"C:\Windows"), "Fonts")
|
||||
pdfmetrics.registerFont(TTFont("Arial", os.path.join(_fonts_dir, "arial.ttf")))
|
||||
pdfmetrics.registerFont(TTFont("ArialBold", os.path.join(_fonts_dir, "arialbd.ttf")))
|
||||
|
||||
OUTPUT = Path(__file__).parent / "test_killer_sudoku_v6.pdf"
|
||||
OUTPUT = Path(__file__).parent / "killer_sudoku_31414.pdf"
|
||||
|
||||
|
||||
def parse_cages(puzzle_str: str) -> list[dict]:
|
||||
@@ -79,78 +79,101 @@ def draw_killer_sudoku(c: Canvas, x0: float, y0: float, cell: float,
|
||||
cy = y0 - (r + 1) * cell + cell * 0.28
|
||||
c.drawCentredString(cx, cy, str(solution[r][co]))
|
||||
|
||||
# --- Vrstva 1: kompletní sudoku mřížka (tenké plné čáry) ---
|
||||
# --- Vrstva 1: tečkované ohraničení klecí (KRESLÍ SE JAKO PRVNÍ) ---
|
||||
# Tečkované jdou ZESPODU, mřížka přes ně. Tím se zachová čistý vzhled —
|
||||
# tenká mřížka překryje místa, kde tečkovaná protíná čáru mřížky.
|
||||
inset = cell * 0.10
|
||||
c.setStrokeColor(colors.Color(0.2, 0.2, 0.2))
|
||||
c.setLineWidth(cage_line * 0.5)
|
||||
c.setDash(3, 2)
|
||||
|
||||
has_top = [[r == 0 or cage_map[r - 1][co] != cage_map[r][co]
|
||||
for co in range(9)] for r in range(9)]
|
||||
has_bot = [[r == 8 or cage_map[r + 1][co] != cage_map[r][co]
|
||||
for co in range(9)] for r in range(9)]
|
||||
has_lft = [[co == 0 or cage_map[r][co - 1] != cage_map[r][co]
|
||||
for co in range(9)] for r in range(9)]
|
||||
has_rgt = [[co == 8 or cage_map[r][co + 1] != cage_map[r][co]
|
||||
for co in range(9)] for r in range(9)]
|
||||
|
||||
def in_cg(rr, cc, cid):
|
||||
return 0 <= rr <= 8 and 0 <= cc <= 8 and cage_map[rr][cc] == cid
|
||||
|
||||
# Top borders — slévání v řádku
|
||||
# Vnější roh: zkrácení o inset. Vnitřní roh: prodloužení o inset.
|
||||
for r in range(9):
|
||||
co = 0
|
||||
while co < 9:
|
||||
if not has_top[r][co]:
|
||||
co += 1
|
||||
continue
|
||||
cid = cage_map[r][co]
|
||||
s = co
|
||||
while co < 9 and cage_map[r][co] == cid and has_top[r][co]:
|
||||
co += 1
|
||||
# Levý konec: vnitřní roh když (r, s-1) je v kleci (cage tam pokračuje směrem nahoru)
|
||||
x_s = x0 + s * cell + (-inset if in_cg(r, s - 1, cid) else inset)
|
||||
# Pravý konec: vnitřní roh když (r, co) je v kleci
|
||||
x_e = x0 + co * cell + (inset if in_cg(r, co, cid) else -inset)
|
||||
c.line(x_s, y0 - r * cell - inset, x_e, y0 - r * cell - inset)
|
||||
|
||||
# Bottom borders
|
||||
for r in range(9):
|
||||
co = 0
|
||||
while co < 9:
|
||||
if not has_bot[r][co]:
|
||||
co += 1
|
||||
continue
|
||||
cid = cage_map[r][co]
|
||||
s = co
|
||||
while co < 9 and cage_map[r][co] == cid and has_bot[r][co]:
|
||||
co += 1
|
||||
x_s = x0 + s * cell + (-inset if in_cg(r, s - 1, cid) else inset)
|
||||
x_e = x0 + co * cell + (inset if in_cg(r, co, cid) else -inset)
|
||||
c.line(x_s, y0 - (r + 1) * cell + inset, x_e, y0 - (r + 1) * cell + inset)
|
||||
|
||||
# Left borders — slévání ve sloupci
|
||||
for co in range(9):
|
||||
r = 0
|
||||
while r < 9:
|
||||
if not has_lft[r][co]:
|
||||
r += 1
|
||||
continue
|
||||
cid = cage_map[r][co]
|
||||
s = r
|
||||
while r < 9 and cage_map[r][co] == cid and has_lft[r][co]:
|
||||
r += 1
|
||||
# Horní konec: vnitřní roh když (s-1, co) je v kleci
|
||||
y_s = y0 - s * cell + (inset if in_cg(s - 1, co, cid) else -inset)
|
||||
# Dolní konec: vnitřní roh když (r, co) je v kleci
|
||||
y_e = y0 - r * cell + (-inset if in_cg(r, co, cid) else inset)
|
||||
c.line(x0 + co * cell + inset, y_s, x0 + co * cell + inset, y_e)
|
||||
|
||||
# Right borders
|
||||
for co in range(9):
|
||||
r = 0
|
||||
while r < 9:
|
||||
if not has_rgt[r][co]:
|
||||
r += 1
|
||||
continue
|
||||
cid = cage_map[r][co]
|
||||
s = r
|
||||
while r < 9 and cage_map[r][co] == cid and has_rgt[r][co]:
|
||||
r += 1
|
||||
y_s = y0 - s * cell + (inset if in_cg(s - 1, co, cid) else -inset)
|
||||
y_e = y0 - r * cell + (-inset if in_cg(r, co, cid) else inset)
|
||||
c.line(x0 + (co + 1) * cell - inset, y_s, x0 + (co + 1) * cell - inset, y_e)
|
||||
|
||||
c.setDash()
|
||||
|
||||
# --- Vrstva 2: kompletní sudoku mřížka (tenké plné čáry přes tečkované) ---
|
||||
c.setStrokeColor(colors.Color(0.55, 0.55, 0.55))
|
||||
c.setLineWidth(thin)
|
||||
for i in range(1, 9):
|
||||
c.line(x0, y0 - i * cell, x0 + 9 * cell, y0 - i * cell)
|
||||
c.line(x0 + i * cell, y0, x0 + i * cell, y0 - 9 * cell)
|
||||
|
||||
# --- Vrstva 2: tečkované ohraničení klecí (odsazené dovnitř buněk) ---
|
||||
inset = cell * 0.10
|
||||
c.setStrokeColor(colors.Color(0.2, 0.2, 0.2))
|
||||
c.setLineWidth(cage_line * 0.5)
|
||||
c.setDash(3, 2)
|
||||
|
||||
# Horizontální hrany klecí — top borders
|
||||
for r in range(9):
|
||||
co = 0
|
||||
while co < 9:
|
||||
cid = cage_map[r][co]
|
||||
if not (r == 0 or cage_map[r - 1][co] != cid):
|
||||
co += 1
|
||||
continue
|
||||
seg_start = co
|
||||
while co < 9 and cage_map[r][co] == cid and (r == 0 or cage_map[r - 1][co] != cid):
|
||||
co += 1
|
||||
c.line(x0 + seg_start * cell + inset, y0 - r * cell - inset,
|
||||
x0 + co * cell - inset, y0 - r * cell - inset)
|
||||
|
||||
# Horizontální hrany klecí — bottom borders
|
||||
for r in range(9):
|
||||
co = 0
|
||||
while co < 9:
|
||||
cid = cage_map[r][co]
|
||||
if not (r == 8 or cage_map[r + 1][co] != cid):
|
||||
co += 1
|
||||
continue
|
||||
seg_start = co
|
||||
while co < 9 and cage_map[r][co] == cid and (r == 8 or cage_map[r + 1][co] != cid):
|
||||
co += 1
|
||||
c.line(x0 + seg_start * cell + inset, y0 - (r + 1) * cell + inset,
|
||||
x0 + co * cell - inset, y0 - (r + 1) * cell + inset)
|
||||
|
||||
# Vertikální hrany klecí — left borders
|
||||
for co in range(9):
|
||||
r = 0
|
||||
while r < 9:
|
||||
cid = cage_map[r][co]
|
||||
if not (co == 0 or cage_map[r][co - 1] != cid):
|
||||
r += 1
|
||||
continue
|
||||
seg_start = r
|
||||
while r < 9 and cage_map[r][co] == cid and (co == 0 or cage_map[r][co - 1] != cid):
|
||||
r += 1
|
||||
c.line(x0 + co * cell + inset, y0 - seg_start * cell - inset,
|
||||
x0 + co * cell + inset, y0 - r * cell + inset)
|
||||
|
||||
# Vertikální hrany klecí — right borders
|
||||
for co in range(9):
|
||||
r = 0
|
||||
while r < 9:
|
||||
cid = cage_map[r][co]
|
||||
if not (co == 8 or cage_map[r][co + 1] != cid):
|
||||
r += 1
|
||||
continue
|
||||
seg_start = r
|
||||
while r < 9 and cage_map[r][co] == cid and (co == 8 or cage_map[r][co + 1] != cid):
|
||||
r += 1
|
||||
c.line(x0 + (co + 1) * cell - inset, y0 - seg_start * cell - inset,
|
||||
x0 + (co + 1) * cell - inset, y0 - r * cell + inset)
|
||||
|
||||
c.setDash()
|
||||
|
||||
# Tlusté 3×3 čáry + vnější okraj
|
||||
# --- Vrstva 3: tlusté 3×3 čáry + vnější okraj ---
|
||||
c.setStrokeColor(colors.black)
|
||||
c.setLineWidth(thick)
|
||||
for i in range(0, 10, 3):
|
||||
|
||||
Reference in New Issue
Block a user