Python CUIで迷路生成(1)
外壁を生成するところまでのステップ。
__init__
1. Maze(迷路) クラスを生成し、maze, width, height の変数を定義する
※第一引数で縦のサイズ(height)、第二引数で横のサイズ(width)を与える
set_outer_wall
2. row という配列にそのセルが壁なのか、通路なのかを格納していく
3. maze に1行ずつ(row) を格納していく
print_maze
4. maze に格納されている row を一つずつ取り出し、さらにセルを一つずつ取り出し、画面出力(壁か通路)させる
import sys import random class Maze(): PATH = 0 WALL = 1 def __init__(self, height, width): self.maze = [] self.width = width self.height = height 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 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.print_maze()
出力例
python create_maze.py 10 20 #################### # # # # # # # # # # # # # # # # ####################