社内se × プログラマ × ビッグデータ

プログラミングなどITに興味があります。

Python CUIで迷路生成(2)

内壁を生成するところまで。
棒倒し法により生成するが、その手順は
1. 迷路全体を構成する2次元配列を、幅高さ5以上の奇数で生成する
2. 迷路の外周を壁とし、それ以外を通路とする
3. 外周の内側に基準となる壁(棒)を1セルおき(x, y ともに偶数の座標)に配置する
4. 内側の壁(棒)を走査し、ランダムな方向に倒して壁とする
※ただし以下に当てはまる方向以外に倒す

  • 1行目の内側の壁以外では上方向に倒してはいけない
  • すでに棒が倒され壁になっている場合、その方向には倒してはいけない
import sys
import random

class Maze():
    PATH = 0
    WALL = 1

    def __init__(self, height, width):
        self.maze = []
        self.width = width
        self.height = height
        if (self.width % 2) == 0:
            self.width += 1
        if (self.height % 2) == 0:
            self.height += 1
    
    def set_outer_wall(self):
        for y in range(0, self.height):
            row = []
            for x in range(0, self.width):
                if (x == 0 or y == 0 or x == self.width - 1 or y == self.height - 1):
                    cell = self.WALL
                else:
                    cell = self.PATH
                row.append(cell)
            self.maze.append(row)
        return self.maze

    def set_inner_wall(self):
        for x in range(2, self.width-1, 2):
            for y in range(2, self.height-1, 2):
                self.maze[y][x] = self.WALL
                while True:
                    wall_x = x
                    wall_y = y
                    if y == 2:
                        direction = random.randrange(0, 4)
                    else:
                        direction = random.randrange(0, 3)

                    if direction == 0:
                        wall_x += 1
                    elif direction == 1:
                        wall_y += 1
                    elif direction == 2:
                        wall_x -= 1
                    else:
                        wall_y -= 1
                    if self.maze[wall_y][wall_x] != self.WALL:
                        self.maze[wall_y][wall_x] = self.WALL
                        break
        return self.maze

    def print_maze(self):
        for row in self.maze:
            for cell in row:
                if cell == self.PATH:
                    print(' ', end='')
                elif cell == self.WALL:
                    print('#', end='')
            print()

args = sys.argv
maze = Maze(int(args[1]), int(args[2]))
maze.set_outer_wall()
maze.set_inner_wall()
maze.print_maze()

出力例

$ python create_maze.py 10 20
#####################
# #   #         # # #
# # # # # # ### # # #
#   #   # # #       #
# ##### ### # ##### #
#   #     # #     # #
# ##### ######### # #
#   #     #       # #
### # ### ##### ### #
#   #   #     #   # #
#####################