commit e134c10a2fa7f1c01c6a23819df7d690cc425b84 Author: Anton Dzyk Date: Sun Dec 21 20:41:57 2025 +0300 task1 - игра в пятнашки diff --git a/app.py b/app.py new file mode 100644 index 0000000..9bf7746 --- /dev/null +++ b/app.py @@ -0,0 +1,55 @@ +# app.py +from flask import Flask, render_template, request, jsonify +import random + +app = Flask(__name__) + +def create_puzzle(): + """Создаёт случайную, но решаемую расстановку плиток.""" + puzzle = list(range(1, 16)) + [0] # 0 — пустая ячейка + while True: + random.shuffle(puzzle) + if is_solvable(puzzle): + break + return puzzle + +def is_solvable(puzzle): + """Проверяет, можно ли решить пазл (на основе инверсий).""" + inversions = 0 + flat = [n for n in puzzle if n != 0] + for i in range(len(flat)): + for j in range(i + 1, len(flat)): + if flat[i] > flat[j]: + inversions += 1 + return inversions % 2 == 0 + +def find_empty(puzzle): + """Находит индекс пустой ячейки (0).""" + return puzzle.index(0) + +@app.route('/') +def index(): + puzzle = create_puzzle() + return render_template('index.html', puzzle=puzzle) + +@app.route('/move', methods=['POST']) +def move(): + puzzle = request.json['puzzle'] + empty_idx = find_empty(puzzle) + move_idx = request.json['index'] + + # Проверяем, можно ли переместить (соседняя ячейка по вертикали или горизонтали) + row, col = move_idx // 4, move_idx % 4 + empty_row, empty_col = empty_idx // 4, empty_idx % 4 + + if (abs(row - empty_row) + abs(col - empty_col) == 1): # Соседняя + puzzle[empty_idx], puzzle[move_idx] = puzzle[move_idx], puzzle[empty_idx] + + # Проверка на победу + if puzzle[:-1] == list(range(1, 16)) and puzzle[-1] == 0: + return jsonify({'puzzle': puzzle, 'winner': True}) + + return jsonify({'puzzle': puzzle, 'winner': False}) + +if __name__ == '__main__': + app.run(debug=True) diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..797db89 --- /dev/null +++ b/static/style.css @@ -0,0 +1,70 @@ +/* static/style.css */ +body { + font-family: 'Segoe UI', sans-serif; + background: #f0f2f5; + text-align: center; + margin: 0; + padding: 20px; +} + +.container { + max-width: 500px; + margin: 0 auto; + padding: 20px; + background: white; + border-radius: 12px; + box-shadow: 0 4px 12px rgba(0,0,0,0.1); +} + +h1 { + color: #333; +} + +#puzzle { + display: grid; + grid-template-columns: repeat(4, 1fr); + grid-gap: 8px; + margin: 20px auto; + width: 320px; + height: 320px; +} + +.tile { + display: flex; + align-items: center; + justify-content: center; + background: #3498db; + color: white; + font-size: 24px; + font-weight: bold; + border-radius: 8px; + cursor: pointer; + user-select: none; + transition: 0.2s; +} + +.tile:hover { + background: #2980b9; +} + +.empty { + background: #f0f2f5; + border: 2px dashed #ccc; + cursor: default; +} + +button { + padding: 10px 20px; + font-size: 16px; + background: #2ecc71; + color: white; + border: none; + border-radius: 6px; + cursor: pointer; + margin-top: 10px; +} + +button:hover { + background: #27ae60; +} + diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..f4ba286 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,69 @@ + + + + + + Игра в Пятнашки + + + +
+

🎮 Игра в Пятнашки

+

Нажимайте на плитки рядом с пустым местом, чтобы переместить их.

+ +
+ + +

+
+ + + + +