Http request to grab sudoku from websudoku and populate initial grid
This commit is contained in:
parent
db516cd52a
commit
ffc9b69906
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/__pycache__/
|
31
pydoku.py
31
pydoku.py
@ -2,6 +2,7 @@ import pygame as pg
|
|||||||
from pygame import Rect, Vector2 as v2
|
from pygame import Rect, Vector2 as v2
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import os
|
import os
|
||||||
|
from websudoku import get_sudoku_puzzle, Difficulty
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Cursor:
|
class Cursor:
|
||||||
@ -31,7 +32,15 @@ padding = 0
|
|||||||
|
|
||||||
cursor = Cursor()
|
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():
|
def draw_grid():
|
||||||
for row in range(9):
|
for row in range(9):
|
||||||
@ -43,7 +52,6 @@ def draw_grid():
|
|||||||
CELL_SIZE * row + padding * row + GRID_Y,
|
CELL_SIZE * row + padding * row + GRID_Y,
|
||||||
CELL_SIZE, CELL_SIZE)
|
CELL_SIZE, CELL_SIZE)
|
||||||
|
|
||||||
|
|
||||||
pg.draw.rect(screen, "white", rect)
|
pg.draw.rect(screen, "white", rect)
|
||||||
pg.draw.rect(screen, "gray", rect, width=1)
|
pg.draw.rect(screen, "gray", rect, width=1)
|
||||||
for l in range(4):
|
for l in range(4):
|
||||||
@ -64,14 +72,11 @@ def draw_cursor():
|
|||||||
CURSOR_SIZE, CURSOR_SIZE)
|
CURSOR_SIZE, CURSOR_SIZE)
|
||||||
screen.blit(cursorSurface, rect)
|
screen.blit(cursorSurface, rect)
|
||||||
|
|
||||||
def get_num(row, col):
|
|
||||||
return board[row, col]
|
|
||||||
|
|
||||||
filled_num_font = pg.font.Font(None, 60)
|
filled_num_font = pg.font.Font(None, 60)
|
||||||
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):
|
||||||
num = board[row * 9 + col]
|
num = board[row * 9 + col][0]
|
||||||
if num == 0:
|
if num == 0:
|
||||||
continue
|
continue
|
||||||
digit = filled_num_font.render(str(num), True, "black")
|
digit = filled_num_font.render(str(num), True, "black")
|
||||||
@ -100,8 +105,8 @@ while running:
|
|||||||
and mx <= grid_max_x
|
and mx <= grid_max_x
|
||||||
and my >= GRID_Y
|
and my >= GRID_Y
|
||||||
and my <= grid_max_y):
|
and my <= grid_max_y):
|
||||||
cursor.row = (my - GRID_Y) // CELL_SIZE + 1
|
cursor.row = int((my - GRID_Y) // CELL_SIZE + 1)
|
||||||
cursor.col = (mx - GRID_X) // CELL_SIZE + 1
|
cursor.col = int((mx - GRID_X) // CELL_SIZE + 1)
|
||||||
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 and cursor.col > 1:
|
if event.key == pg.K_h and cursor.col > 1:
|
||||||
@ -117,10 +122,16 @@ while running:
|
|||||||
if num.isdigit() and num != '0':
|
if num.isdigit() and num != '0':
|
||||||
num = int(num)
|
num = int(num)
|
||||||
idx = (cursor.row - 1) * 9 + cursor.col - 1
|
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:
|
if event.key == pg.K_F1:
|
||||||
print(board)
|
print(sudoku)
|
||||||
|
|
||||||
|
|
||||||
screen.fill('cornflower blue')
|
screen.fill('cornflower blue')
|
||||||
|
49
websudoku.py
Normal file
49
websudoku.py
Normal file
@ -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
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user