Add a button to check the state of the board, F3 to solve
This commit is contained in:
parent
6f089cf45c
commit
60deaed18a
42
pydoku.py
42
pydoku.py
@ -50,6 +50,7 @@ running = True
|
|||||||
##################
|
##################
|
||||||
title_font = pg.font.Font(None, 64)
|
title_font = pg.font.Font(None, 64)
|
||||||
filled_num_font = pg.font.Font(None, 60)
|
filled_num_font = pg.font.Font(None, 60)
|
||||||
|
btn_font = pg.font.Font(None, 28)
|
||||||
pm_border_font = pg.font.Font(None, 18)
|
pm_border_font = pg.font.Font(None, 18)
|
||||||
pm_center_font = pg.font.Font(None, 32)
|
pm_center_font = pg.font.Font(None, 32)
|
||||||
title = title_font.render("Pydoku", True, "black")
|
title = title_font.render("Pydoku", True, "black")
|
||||||
@ -68,18 +69,17 @@ cursor = Cursor()
|
|||||||
input_method = InputMethod.FILL
|
input_method = InputMethod.FILL
|
||||||
|
|
||||||
# TODO: We have to make this async
|
# TODO: We have to make this async
|
||||||
# sudoku = get_sudoku_puzzle(Difficulty.EVIL)
|
sudoku = get_sudoku_puzzle(Difficulty.EVIL)
|
||||||
sudoku = get_sudoku_puzzle_test()
|
# sudoku = get_sudoku_puzzle_test()
|
||||||
|
|
||||||
board = []
|
board = []
|
||||||
|
|
||||||
for i in range(81):
|
for i in range(81):
|
||||||
if sudoku.editmask[i] == 0:
|
if sudoku.editmask[i] == 0:
|
||||||
board.append(Cell(sudoku.cheat[i], True))
|
board.append(Cell(sudoku.solution[i], True))
|
||||||
else:
|
else:
|
||||||
board.append(Cell())
|
board.append(Cell())
|
||||||
|
|
||||||
# pencil_marks = [PencilMark() for _ in range(81)]
|
|
||||||
pencil_marks = []
|
pencil_marks = []
|
||||||
for i in range(81):
|
for i in range(81):
|
||||||
pm = PencilMark() if not board[i].given else None
|
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),
|
start_pos=(GRID_X + CELL_SIZE * 3 * l, GRID_Y),
|
||||||
end_pos =(GRID_X + CELL_SIZE * 3 * l, GRID_Y + CELL_SIZE * 9),
|
end_pos =(GRID_X + CELL_SIZE * 3 * l, GRID_Y + CELL_SIZE * 9),
|
||||||
width=2)
|
width=2)
|
||||||
|
|
||||||
def draw_cursor():
|
def draw_cursor():
|
||||||
pad_diff = (CURSOR_SIZE - CELL_SIZE) // 2
|
pad_diff = (CURSOR_SIZE - CELL_SIZE) // 2
|
||||||
pg.draw.rect(cursorSurface, (100, 0, 155, 100), [0, 0, CURSOR_SIZE, CURSOR_SIZE])
|
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):
|
def draw_pm_center(row, col, idx):
|
||||||
third = CELL_SIZE // 3
|
third = CELL_SIZE // 3
|
||||||
nums = "".join(map(str, sorted(pencil_marks[idx].center)))
|
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,
|
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)
|
GRID_Y + CELL_SIZE * row + CELL_SIZE // 2 - digits.get_height() // 2.3)
|
||||||
screen.blit(digits, pos)
|
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():
|
def draw_numbers():
|
||||||
for row in range(9):
|
for row in range(9):
|
||||||
for col in range(9):
|
for col in range(9):
|
||||||
@ -158,6 +168,16 @@ def draw_numbers():
|
|||||||
def draw_hud():
|
def draw_hud():
|
||||||
screen.blit(title, (screen.get_width() // 2 - title.get_width() // 2, 70))
|
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:
|
while running:
|
||||||
for event in pg.event.get():
|
for event in pg.event.get():
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
@ -175,6 +195,10 @@ while running:
|
|||||||
cursor.row = int((my - GRID_Y) // CELL_SIZE + 1)
|
cursor.row = int((my - GRID_Y) // CELL_SIZE + 1)
|
||||||
cursor.col = int((mx - GRID_X) // 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:
|
if event.type == pg.KEYDOWN:
|
||||||
# keys = pg.key.get_pressed()
|
# keys = pg.key.get_pressed()
|
||||||
if (event.key == pg.K_h or event.key == pg.K_a) and cursor.col > 1:
|
if (event.key == pg.K_h or event.key == pg.K_a) and cursor.col > 1:
|
||||||
@ -219,13 +243,18 @@ while running:
|
|||||||
case pg.K_b:
|
case pg.K_b:
|
||||||
input_method = InputMethod.BORDER
|
input_method = InputMethod.BORDER
|
||||||
|
|
||||||
|
##############
|
||||||
|
# Debug stuff
|
||||||
|
##############
|
||||||
if event.key == pg.K_F1:
|
if event.key == pg.K_F1:
|
||||||
print(sudoku)
|
print(sudoku)
|
||||||
if event.key == pg.K_F2:
|
if event.key == pg.K_F2:
|
||||||
idx = (cursor.row - 1) * 9 + cursor.col - 1
|
idx = (cursor.row - 1) * 9 + cursor.col - 1
|
||||||
if not board[idx].given:
|
if not board[idx].given:
|
||||||
pencil_marks[idx].border = {i for i in range(1, 10)}
|
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')
|
screen.fill('cornflower blue')
|
||||||
@ -234,6 +263,7 @@ while running:
|
|||||||
draw_numbers()
|
draw_numbers()
|
||||||
draw_cursor()
|
draw_cursor()
|
||||||
draw_hud()
|
draw_hud()
|
||||||
|
draw_buttons()
|
||||||
|
|
||||||
|
|
||||||
pg.display.flip()
|
pg.display.flip()
|
||||||
|
@ -10,10 +10,13 @@
|
|||||||
* DONE Scrape websudoku.com
|
* DONE Scrape websudoku.com
|
||||||
* DONE Populate given numbers
|
* DONE Populate given numbers
|
||||||
* DONE Pencil Marks
|
* DONE Pencil Marks
|
||||||
* TODO Add "Done" button
|
* DONE Add "Done" button
|
||||||
* TODO Check if puzzle is complete
|
* DONE Check if puzzle is complete
|
||||||
|
* DONE Improve pencil mark colors
|
||||||
* TODO Show errors
|
* TODO Show errors
|
||||||
|
* TODO Pass difficulty through command line
|
||||||
* TODO Toast on valid solve
|
* TODO Toast on valid solve
|
||||||
|
* TODO Show the time it took
|
||||||
* TODO Save current game
|
* TODO Save current game
|
||||||
* TODO Create a pip file
|
* TODO Create a pip file
|
||||||
* TODO Help labels
|
* TODO Help labels
|
||||||
|
@ -6,7 +6,7 @@ from typing import List
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Sudoku:
|
class Sudoku:
|
||||||
cheat: List[int]
|
solution: List[int]
|
||||||
editmask: List[int]
|
editmask: List[int]
|
||||||
|
|
||||||
class Difficulty(Enum):
|
class Difficulty(Enum):
|
||||||
@ -52,7 +52,7 @@ def get_sudoku_puzzle(difficulty: Difficulty) -> Sudoku:
|
|||||||
|
|
||||||
conn = http.client.HTTPSConnection(host)
|
conn = http.client.HTTPSConnection(host)
|
||||||
|
|
||||||
conn.request("GET", "/?level=4")
|
conn.request("GET", "/?level=2")
|
||||||
|
|
||||||
response = conn.getresponse()
|
response = conn.getresponse()
|
||||||
|
|
||||||
@ -69,4 +69,3 @@ def get_sudoku_puzzle(difficulty: Difficulty) -> Sudoku:
|
|||||||
else:
|
else:
|
||||||
print("Error getting sudoku puzzle:", response.status, response.reason)
|
print("Error getting sudoku puzzle:", response.status, response.reason)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user