From ffc9b69906e4749a00ef08920e952bc7512d4a4b Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 29 Oct 2023 13:11:37 +0700 Subject: [PATCH] Http request to grab sudoku from websudoku and populate initial grid --- .gitignore | 1 + pydoku.py | 31 +++++++++++++++++++++---------- websudoku.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 .gitignore create mode 100644 websudoku.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a348e50 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/__pycache__/ diff --git a/pydoku.py b/pydoku.py index d009500..565cfaa 100644 --- a/pydoku.py +++ b/pydoku.py @@ -2,6 +2,7 @@ import pygame as pg from pygame import Rect, Vector2 as v2 from dataclasses import dataclass import os +from websudoku import get_sudoku_puzzle, Difficulty @dataclass class Cursor: @@ -31,7 +32,15 @@ padding = 0 cursor = Cursor() -board = [0] * (81) +sudoku = get_sudoku_puzzle(Difficulty.EVIL) + +board = [] + +for i in range(81): + if sudoku.editmask[i] == 0: + board.append((sudoku.cheat[i], True)) + else: + board.append((0, False)) def draw_grid(): for row in range(9): @@ -43,7 +52,6 @@ def draw_grid(): CELL_SIZE * row + padding * row + GRID_Y, CELL_SIZE, CELL_SIZE) - pg.draw.rect(screen, "white", rect) pg.draw.rect(screen, "gray", rect, width=1) for l in range(4): @@ -64,14 +72,11 @@ def draw_cursor(): CURSOR_SIZE, CURSOR_SIZE) screen.blit(cursorSurface, rect) -def get_num(row, col): - return board[row, col] - filled_num_font = pg.font.Font(None, 60) def draw_numbers(): for row in range(9): for col in range(9): - num = board[row * 9 + col] + num = board[row * 9 + col][0] if num == 0: continue digit = filled_num_font.render(str(num), True, "black") @@ -100,8 +105,8 @@ while running: and mx <= grid_max_x and my >= GRID_Y and my <= grid_max_y): - cursor.row = (my - GRID_Y) // CELL_SIZE + 1 - cursor.col = (mx - GRID_X) // CELL_SIZE + 1 + cursor.row = int((my - GRID_Y) // CELL_SIZE + 1) + cursor.col = int((mx - GRID_X) // CELL_SIZE + 1) if event.type == pg.KEYDOWN: # keys = pg.key.get_pressed() if event.key == pg.K_h and cursor.col > 1: @@ -117,10 +122,16 @@ while running: if num.isdigit() and num != '0': num = int(num) idx = (cursor.row - 1) * 9 + cursor.col - 1 - board[idx] = num + if not board[idx][1]: + board[idx] = (num, False) + + if event.key == pg.K_BACKSPACE: + idx = (cursor.row - 1) * 9 + cursor.col - 1 + if not board[idx][1]: + board[idx] = (0, False) if event.key == pg.K_F1: - print(board) + print(sudoku) screen.fill('cornflower blue') diff --git a/websudoku.py b/websudoku.py new file mode 100644 index 0000000..008a170 --- /dev/null +++ b/websudoku.py @@ -0,0 +1,49 @@ +import http.client +import re +from enum import Enum +from dataclasses import dataclass +from typing import List + +@dataclass +class Sudoku: + cheat: List[int] + editmask: List[int] + +class Difficulty(Enum): + EASY = 1 + MEDIUM = 2 + HARD = 3 + EVIL = 4 + +host = "five.websudoku.com" + +def get_sudoku_puzzle(difficulty: Difficulty) -> Sudoku: + """Makes a web request to websudoku.com to grab a sudoku puzzle + + Args: + difficulty: Choose between easy, medium, hard, or evil + + Returns: + A Sudoku object which contains the cheat and the editmask + """ + + conn = http.client.HTTPSConnection(host) + + conn.request("GET", "/?level=4") + + response = conn.getresponse() + + if response.status == 200: + html_content = response.read().decode('utf-8') + # print(html_content) + for line in html_content.split('\n'): + if "editmask" in line: + editmask = [ int(n) for n in re.findall(r'\d', line) ] + if "var cheat" in line: + cheat = [ int(n) for n in re.findall(r'\d', line) ] + conn.close() + return Sudoku(cheat, editmask) + else: + print("Error getting sudoku puzzle:", response.status, response.reason) + return None +