Add a button to check the state of the board, F3 to solve

This commit is contained in:
Joseph Ferano 2023-10-29 16:46:56 +07:00
parent 6f089cf45c
commit 60deaed18a
3 changed files with 43 additions and 11 deletions

View File

@ -50,6 +50,7 @@ running = True
##################
title_font = pg.font.Font(None, 64)
filled_num_font = pg.font.Font(None, 60)
btn_font = pg.font.Font(None, 28)
pm_border_font = pg.font.Font(None, 18)
pm_center_font = pg.font.Font(None, 32)
title = title_font.render("Pydoku", True, "black")
@ -68,18 +69,17 @@ cursor = Cursor()
input_method = InputMethod.FILL
# TODO: We have to make this async
# sudoku = get_sudoku_puzzle(Difficulty.EVIL)
sudoku = get_sudoku_puzzle_test()
sudoku = get_sudoku_puzzle(Difficulty.EVIL)
# sudoku = get_sudoku_puzzle_test()
board = []
for i in range(81):
if sudoku.editmask[i] == 0:
board.append(Cell(sudoku.cheat[i], True))
board.append(Cell(sudoku.solution[i], True))
else:
board.append(Cell())
# pencil_marks = [PencilMark() for _ in range(81)]
pencil_marks = []
for i in range(81):
pm = PencilMark() if not board[i].given else None
@ -108,6 +108,7 @@ def draw_grid():
start_pos=(GRID_X + CELL_SIZE * 3 * l, GRID_Y),
end_pos =(GRID_X + CELL_SIZE * 3 * l, GRID_Y + CELL_SIZE * 9),
width=2)
def draw_cursor():
pad_diff = (CURSOR_SIZE - CELL_SIZE) // 2
pg.draw.rect(cursorSurface, (100, 0, 155, 100), [0, 0, CURSOR_SIZE, CURSOR_SIZE])
@ -128,11 +129,20 @@ def draw_pm_border(row, col, idx):
def draw_pm_center(row, col, idx):
third = CELL_SIZE // 3
nums = "".join(map(str, sorted(pencil_marks[idx].center)))
digits = pm_center_font.render(nums, True, "green")
digits = pm_center_font.render(nums, True, "dark green")
pos = (GRID_X + CELL_SIZE * col + CELL_SIZE // 2 - digits.get_width() // 2,
GRID_Y + CELL_SIZE * row + CELL_SIZE // 2 - digits.get_height() // 2.3)
screen.blit(digits, pos)
done_txt = btn_font.render("Check", True, "black")
done_rect = Rect(screen.get_width() - 10 - 100, 10, 100, 30)
def draw_buttons():
pg.draw.rect(screen, "gray", done_rect)
pg.draw.rect(screen, "black", done_rect, width=2)
screen.blit(done_txt,
(done_rect.x + done_rect.w // 2 - done_txt.get_width() // 2,
done_rect.y + done_rect.h // 2 - done_txt.get_height() // 2))
def draw_numbers():
for row in range(9):
for col in range(9):
@ -158,6 +168,16 @@ def draw_numbers():
def draw_hud():
screen.blit(title, (screen.get_width() // 2 - title.get_width() // 2, 70))
def check_board():
for i,cell in enumerate(board):
if cell.value == 0:
print("You still have to finish the puzzle!")
return
if cell.value != sudoku.solution[i]:
print("You made a mistake!")
return
print("You completed it!")
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
@ -175,6 +195,10 @@ while running:
cursor.row = int((my - GRID_Y) // CELL_SIZE + 1)
cursor.col = int((mx - GRID_X) // CELL_SIZE + 1)
if done_rect.collidepoint(event.pos):
check_board()
if event.type == pg.KEYDOWN:
# keys = pg.key.get_pressed()
if (event.key == pg.K_h or event.key == pg.K_a) and cursor.col > 1:
@ -219,21 +243,27 @@ while running:
case pg.K_b:
input_method = InputMethod.BORDER
##############
# Debug stuff
##############
if event.key == pg.K_F1:
print(sudoku)
if event.key == pg.K_F2:
idx = (cursor.row - 1) * 9 + cursor.col - 1
if not board[idx].given:
pencil_marks[idx].border = {i for i in range(1, 10)}
if event.key == pg.K_F3:
for i,cell in enumerate(board):
cell.value = sudoku.solution[i]
screen.fill('cornflower blue')
draw_grid()
draw_numbers()
draw_cursor()
draw_hud()
draw_buttons()
pg.display.flip()

View File

@ -10,10 +10,13 @@
* DONE Scrape websudoku.com
* DONE Populate given numbers
* DONE Pencil Marks
* TODO Add "Done" button
* TODO Check if puzzle is complete
* DONE Add "Done" button
* DONE Check if puzzle is complete
* DONE Improve pencil mark colors
* TODO Show errors
* TODO Pass difficulty through command line
* TODO Toast on valid solve
* TODO Show the time it took
* TODO Save current game
* TODO Create a pip file
* TODO Help labels

View File

@ -6,7 +6,7 @@ from typing import List
@dataclass
class Sudoku:
cheat: List[int]
solution: List[int]
editmask: List[int]
class Difficulty(Enum):
@ -52,7 +52,7 @@ def get_sudoku_puzzle(difficulty: Difficulty) -> Sudoku:
conn = http.client.HTTPSConnection(host)
conn.request("GET", "/?level=4")
conn.request("GET", "/?level=2")
response = conn.getresponse()
@ -69,4 +69,3 @@ def get_sudoku_puzzle(difficulty: Difficulty) -> Sudoku:
else:
print("Error getting sudoku puzzle:", response.status, response.reason)
return None