Zpět na projektyBack to projects

Python Pygame Sudoku

Python Pygame JSON ctypes

Objektově orientovaná hra Sudoku vytvořená v Pythonu a knihovně Pygame. Projekt disponuje integrovaným algoritmickým řešitelem (backtracking), moderním uživatelským rozhraním, serializací stavu hry a na událostech založeným přehrávačem hudby na pozadí. Object-oriented Sudoku game built with Python and Pygame. This project features a built-in algorithmic solver (backtracking), a modern UI, game state serialization, and an event-driven background music player.

Sudoku Game Demo

Ukázka herní plochy a uživatelského rozhraní Game board and user interface showcase

Klíčové funkceKey Features

OvládáníControls

Struktura projektuProject Structure

Directory Tree
sudoku/
├── assets/                 # Icons and music files
│   ├── icon.png            # Application window/taskbar icon
│   └── music/              # Directory for background music (.mp3)
├── src/
│   ├── __init__.py
│   ├── config.py           # Constants, colors, and dimensions
│   ├── cube.py             # Single grid cell logic and rendering
│   ├── grid.py             # Main board logic and state management
│   ├── game.py             # Game loop, UI, and state machine
│   ├── solver.py           # Backtracking algorithm and validation
│   └── ui.py               # Interactive button components
├── main.py                 # Application entry point
├── save.json               # Auto-generated save state
└── README.md

Rychlý startQuick Start

Doporučuje se použít virtuální prostředí (venv/conda). It is highly recommended to use a virtual environment (venv/conda).

1. Klonování repozitáře1. Clone the repository

Terminal
git clone https://github.com/PinkiMan/Sudoku.git
cd Sudoku

2. Instalace závislostí2. Install Dependencies

Terminal
pip install pygame>=2.5.0

3. Spuštění hry3. Run the game

Terminal
python main.py

Detail algoritmického řešitele (Backtracking)Algorithmic Solver Detail (Backtracking)

Jádrem auto-solveru je algoritmus s návratem (backtracking), který rekurzivně zkouší možná čísla a vrací se zpět, jakmile narazí na slepou uličku. Tento proces je vizualizován přímo v Pygame okně. The core of the auto-solver is a backtracking algorithm that recursively attempts possible valid numbers, reverting changes when it hits a dead end. This process is visualized directly within the Pygame window.

solver.py
def solve(grid):
    """
    Backtracking algorithm to solve the Sudoku board.
    Returns True if solved, False if unsolvable.
    """
    empty_cell = find_empty(grid)
    if not empty_cell:
        return True  # Puzzle completely solved
        
    row, col = empty_cell

    for num in range(1, 10):
        # Check if the number is valid in the current row, column and 3x3 box
        if is_valid(grid, num, (row, col)):
            grid[row][col] = num

            # Recursively try to solve the rest of the board
            if solve(grid):
                return True
            
            # Dead end reached, backtrack (reset the cell and try next number)
            grid[row][col] = 0

    return False
Zobrazit kód na GitHubu View Code on GitHub