迷路の幅と高さをそれぞれ width, height として設定し、
その数値にしたがって壁のない迷路を生成する。
※迷路の幅と高さは5以上の奇数とする。
class Maze {
constructor(width, height) {
this.PATH = 0;
this.WALL = 1;
this.width = width;
this.height = height;
if (this.width < 5 || this.height < 5) {
return;
}
if (this.width%2 == 0) {
this.width++;
}
if (this.height%2 == 0) {
this.height++;
}
this.maze = Array.from(new Array(this.height), () => new Array(this.width).fill(this.PATH));
}
print_maze() {
for (let col of this.maze) {
let arr = '';
for (let cell of col) {
if (cell == this.WALL) {
arr += '#';
} else if (cell == this.PATH) {
arr += ' ';
}
}
console.log(arr);
}
}
}
let maze1 = new Maze(15, 15);
maze1.print_maze();
for (let col of maze1.maze) {
let arr = '';
for(let cell of col) {
arr += cell;
}
console.log(arr);
}
console.log(maze1.maze.length, maze1.maze[0].length);
今回は、以下のように出力される。
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
15 15

Online editor and compiler
Paiza.IO is online editor and compiler. Java, Ruby, Python, PHP, Perl, Swift, JavaScript... You can use for learning pro...