add sudoku solution

This commit is contained in:
samerbam 2023-08-23 20:02:31 -04:00
parent 3f89e6fc85
commit e226a751fa
6 changed files with 53 additions and 12 deletions

View File

@ -37,6 +37,17 @@ class TodoDate(BaseModel):
# date: str = datetime.today().strftime('%Y-%m-%d') # date: str = datetime.today().strftime('%Y-%m-%d')
class PrintAction(BaseModel):
date: str = datetime.today().strftime('%Y-%m-%d')
action: str #Options:
# default (pulls current todos from date, needs date)
# sudoku (prints a random sudoku)
# todos (prints only dates todos)
# wordsearch (prints a random wordsearch)
# quote (prints a random quote)
# greeting (prints a greeting)
#
# @app.get("/api/public") # @app.get("/api/public")
# def public(): # def public():
# """No access token required to access this route""" # """No access token required to access this route"""
@ -107,7 +118,7 @@ def write_todos(todos: TodoList, auth_result: str = Security(auth.verify)):
@app.get("/api/todos/print") @app.get("/api/todos/print")
def print_todos(date: str = datetime.today().strftime('%Y-%m-%d'), auth_result: str = Security(auth.verify)): def print_todos(date: PrintAction, auth_result: str = Security(auth.verify)):
"""A valid access token is required to access this route""" """A valid access token is required to access this route"""
# result = { # result = {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.9 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

BIN
application/sudoku_16.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
application/sudoku_4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -1,6 +1,7 @@
from PIL import Image from PIL import Image
from PIL import ImageDraw from PIL import ImageDraw
from PIL import ImageFont from PIL import ImageFont
import copy
def generate_sudoku(): def generate_sudoku():
base = 3 base = 3
@ -21,7 +22,8 @@ def generate_sudoku():
board = [ [nums[pattern(r,c)] for c in cols] for r in rows ] board = [ [nums[pattern(r,c)] for c in cols] for r in rows ]
# for line in board: print(line) # for line in board: print(line)
# solution = board # solution = board[:]
solution = copy.deepcopy(board)
# print(solution) # print(solution)
@ -36,6 +38,7 @@ def generate_sudoku():
for p in sample(range(squares),empties): for p in sample(range(squares),empties):
board[p//side][p%side] = 0 board[p//side][p%side] = 0
# numSize = len(str(side)) # numSize = len(str(side))
# for line in board: # for line in board:
# print(*(f"{n or '.':{numSize}} " for n in line)) # print(*(f"{n or '.':{numSize}} " for n in line))
@ -69,35 +72,48 @@ def generate_sudoku():
symbol = " 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ" symbol = " 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
nums = [ [""]+[symbol[n] for n in row] for row in board ] nums = [ [""]+[symbol[n] for n in row] for row in board ]
nums_sol = [ [""]+[symbol[n] for n in row] for row in solution ]
output = [] output = []
output_sol = []
# print(line0) # print(line0)
output.append(line0) output.append(line0)
output_sol.append(line0)
for r in range(1,side+1): for r in range(1,side+1):
output.append("".join(n+s for n,s in zip(nums[r-1],line1.split(".")))) output.append("".join(n+s for n,s in zip(nums[r-1],line1.split("."))))
output.append([line2,line3,line4][(r%side==0)+(r%base==0)]) output.append([line2,line3,line4][(r%side==0)+(r%base==0)])
# print( "".join(n+s for n,s in zip(nums[r-1],line1.split("."))) ) # print( "".join(n+s for n,s in zip(nums[r-1],line1.split("."))) )
# print([line2,line3,line4][(r%side==0)+(r%base==0)]) # print([line2,line3,line4][(r%side==0)+(r%base==0)])
for r in range(1,side+1):
output_sol.append("".join(n+s for n,s in zip(nums_sol[r-1],line1.split("."))))
output_sol.append([line2,line3,line4][(r%side==0)+(r%base==0)])
# print('=') # print('=')
# print(output) # print(output)
# for x in output: # for x in output:
# print(x) # print(x)
# print('-') # print('-')
return output return [output, output_sol]
def convert_to_image(sudoku): def convert_to_image(sudoku, size=16, flipped=False):
# sample text and font # sample text and font
# unicode_text = u"Unicode Characters: \u00C6 \u00E6 \u00B2 \u00C4 \u00D1 \u220F" # unicode_text = u"Unicode Characters: \u00C6 \u00E6 \u00B2 \u00C4 \u00D1 \u220F"
unicode_text = sudoku[0] unicode_text = sudoku[0]
verdana_font = ImageFont.truetype("application/IBMPlexMono-Medium.ttf", 16, encoding="unic") # print(sudoku)
# print(len(sudoku))
verdana_font = ImageFont.truetype("IBMPlexMono-Medium.ttf", size, encoding="unic")
# get the line size # get the line size
text_width, text_height = verdana_font.getsize(unicode_text) bx, by, text_width, text_height = verdana_font.getbbox(unicode_text)
# print(verdana_font.getsize(unicode_text))
# print(verdana_font.getbbox(unicode_text))
# print(verdana_font.getlength(unicode_text))
# print(text_height)
# create a blank canvas with extra space between lines # create a blank canvas with extra space between lines
canvas = Image.new('RGB', (text_width + 10, (text_height*(len(sudoku)-1))), (255, 255, 255)) canvas = Image.new('RGB', (text_width + 10, ((text_height-2)*(len(sudoku)))), (255, 255, 255))
# canvas.convert('L') # canvas.convert('L')
@ -114,7 +130,9 @@ def convert_to_image(sudoku):
# save the blank canvas to a file # save the blank canvas to a file
# fn = lambda x : 255 if x > 200 else 0 # fn = lambda x : 255 if x > 200 else 0
# canvas.convert('L').point(fn, mode='1') # canvas.convert('L').point(fn, mode='1')
canvas.save("sudoku.png", "PNG") if flipped:
canvas = canvas.transpose(Image.FLIP_TOP_BOTTOM)
canvas.save("sudoku_" + str(size) +".png", "PNG")
# img = Image.new('RGB', (200, 100)) # img = Image.new('RGB', (200, 100))
# d = ImageDraw.Draw(img) # d = ImageDraw.Draw(img)
# d.text((30, 20), sudoku[0], fill=(255, 0, 0)) # d.text((30, 20), sudoku[0], fill=(255, 0, 0))
@ -122,11 +140,17 @@ def convert_to_image(sudoku):
# print(text_width, text_height) # print(text_width, text_height)
if __name__ == '__main__': if __name__ == '__main__':
su = generate_sudoku() out = generate_sudoku()
su = out[0]
su_sol = out[1]
# print(out)
for x in su: for x in su:
print(x) print(x)
for y in su_sol:
print(y)
convert_to_image(su) convert_to_image(su)
print(su) convert_to_image(su_sol, size=4, flipped=True)
# print(su)
# for x in generate_sudoku(): # for x in generate_sudoku():
# print(x) # print(x)

View File

@ -129,9 +129,15 @@ class ThermalPrinter():
# self.p.close() # self.p.close()
# self.p = Usb(0x0416, 0x5011, None, 4, 0x81, 0x02) # self.p = Usb(0x0416, 0x5011, None, 4, 0x81, 0x02)
# self.p = Usb(idVendor=0x0416, idProduct=0x5011, usb_args=None, timeout=4000 , in_ep=0x81, out_ep=0x02, profile="POS-5890") # self.p = Usb(idVendor=0x0416, idProduct=0x5011, usb_args=None, timeout=4000 , in_ep=0x81, out_ep=0x02, profile="POS-5890")
convert_to_image(generate_sudoku()) # convert_to_image(generate_sudoku())
sudokus = generate_sudoku()
convert_to_image(sudokus[0], size=16, flipped=False)
convert_to_image(sudokus[1], size=4, flipped=True)
self.p.set(align="right")
self.p.image("sudoku_4.png")
self.p.text("\n")
self.p.set(align="center") self.p.set(align="center")
self.p.image("sudoku.png") self.p.image("sudoku_16.png")
# self.p.close() # self.p.close()
# self.p = Usb(idVendor=0x0416, idProduct=0x5011, usb_args=None, timeout=4 , in_ep=0x81, out_ep=0x02, profile="POS-5890") # self.p = Usb(idVendor=0x0416, idProduct=0x5011, usb_args=None, timeout=4 , in_ep=0x81, out_ep=0x02, profile="POS-5890")