From 8c7f4f4cb6256247faa3c39f81373b8b094115f8 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Tue, 23 Dec 2025 00:20:31 +0100 Subject: [PATCH] New job: maze --- jobs/maze.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ print_server.py | 4 +++- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 jobs/maze.py diff --git a/jobs/maze.py b/jobs/maze.py new file mode 100644 index 0000000..9aabb33 --- /dev/null +++ b/jobs/maze.py @@ -0,0 +1,61 @@ +import random +from .base import Job + +class MazeJob(Job): + def get_name(self): + return "BLUDISTE" + + def print_body(self, p): + # Width and Height in cells. + # Total width in chars = 2 * w + 1. + # w=15 -> 31 chars (Fits comfortably on 80mm printers, tight on 58mm) + w = 15 + h = 15 + + maze = self.generate_maze(w, h) + + p.text("Najdi cestu z S do E:\n\n") + + # Center the maze + p.set(align='center') + for row in maze: + p.text("".join(row) + "\n") + p.set(align='left') + p.text("\n") + + def generate_maze(self, width, height): + rows = 2 * height + 1 + cols = 2 * width + 1 + # Initialize grid with walls + maze = [['#' for _ in range(cols)] for _ in range(rows)] + + # Starting cell (1, 1) + stack = [(1, 1)] + maze[1][1] = ' ' + + while stack: + x, y = stack[-1] + # Directions: Up, Down, Left, Right (step 2 to jump over walls) + directions = [(0, -2), (0, 2), (-2, 0), (2, 0)] + random.shuffle(directions) + + moved = False + for dx, dy in directions: + nx, ny = x + dx, y + dy + # Check bounds (ensure we stay within the outer border) + if 1 <= nx < rows - 1 and 1 <= ny < cols - 1: + if maze[nx][ny] == '#': # If unvisited + maze[nx][ny] = ' ' # Mark cell as path + maze[x + dx // 2][y + dy // 2] = ' ' # Knock down wall + stack.append((nx, ny)) + moved = True + break + + if not moved: + stack.pop() + + # Create Entrance (S) and Exit (E) + maze[1][0] = 'S' + maze[rows - 2][cols - 1] = 'E' + + return maze \ No newline at end of file diff --git a/print_server.py b/print_server.py index 95c3815..b9f48f0 100644 --- a/print_server.py +++ b/print_server.py @@ -3,6 +3,7 @@ from escpos.exceptions import USBNotFoundError from jobs.math_homework import MathHomeworkJob from jobs.unit_conversion import UnitConversionJob from jobs.chess_puzzle import ChessPuzzleJob +from jobs.maze import MazeJob # ========================================== # CONFIGURATION @@ -40,7 +41,8 @@ def get_printer(): JOBS = [ MathHomeworkJob(), UnitConversionJob(), - ChessPuzzleJob() + ChessPuzzleJob(), + MazeJob() ] def run_tui():