0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

アプリ開発100本ノック Day 3: 図形迷路ジェネレーター

Posted at

目的: 選んだ図形の形で迷路を自動生成するアプリを作る!

開発ストーリー:

今日は、ちょっと変わった迷路アプリに挑戦!💪 四角い迷路はよく見るけど、円形とか星型とかの迷路ってあんまり見ないよね?🤔 ということで、図形の形に合わせて迷路を生成するアプリを作ってみることに!✨ 迷路のアルゴリズムには、王道の「穴掘り法」を採用!⛏️ 図形の形に沿って迷路をトリミングするっていう手法で実装してみました!

コードはこちら! (Python):

from flask import Flask, render_template, request, jsonify
import random
import math

app = Flask(__name__, template_folder='rabi', static_folder='static')

@app.route("/", methods=["GET"])
def index():
    return render_template("index.html")

@app.route("/generate_maze", methods=["POST"])
def generate_maze():
    shape = request.form.get('shape')
    width = 21  # 迷路の幅 (奇数)
    height = 21  # 迷路の高さ (奇数)

    maze = [['#'] * width for _ in range(height)]

    def is_valid(x, y):
        return 0 <= x < width and 0 <= y < height

    def dig(x, y):
        maze[y][x] = ' '
        directions = [(0, 2), (2, 0), (0, -2), (-2, 0)]
        random.shuffle(directions)
        for dx, dy in directions:
            nx, ny = x + dx, y + dy
            if is_valid(nx, ny) and maze[ny][nx] == '#':
                maze[ny][x + dx // 2] = ' '
                dig(nx, ny)

    start_x, start_y = 1, 1
    dig(start_x, start_y)

    # ランダムに出口を作成 (円形にトリミング後、有効な出口に変更が必要な場合があります)
    exit_side = random.choice([0, 1, 2, 3])
    # ... (出口生成ロジックは同じ)


    # 円形にトリミング
    center_x = width // 2
    center_y = height // 2
    radius = width // 2

    trimmed_maze = []
    for y in range(height):
        row = []
        for x in range(width):
            distance = math.sqrt((x - center_x)**2 + (y - center_y)**2)
            if distance <= radius:
                row.append(maze[y][x])
            else:
                 row.append('') # 円の外側は空白にする
        trimmed_maze.append(row)


    return jsonify(maze=trimmed_maze, shape=shape) # trimmed_maze を返す

if __name__ == "__main__":
    app.run(debug=True, port=5003)

工夫ポイント:
穴掘り法で迷路生成!
図形の形に沿ってトリミング!

今日の壁…:
迷路は生成できるんだけど、図形の形にトリミングするのがうまくいかなくて…😭 円形の迷路を作りたかったのに、なんか変な形になっちゃった… 一旦断念…

未来への野望!:
円形以外の図形にも対応したい! (星型とかハート型とか!💖)
迷路の難易度を調整できるようにしたい! (簡単モード、鬼畜モード😈)
3D迷路とか作ってみたい!🤩

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?