迷宫生成算法JS实现

1.简单的递归分割

代码如下:

import Phaser from 'phaser';

export default class extends Phaser.State {
  init() { }
  preload() { }
  create() {
    let self = this;
    this.stage.backgroundColor = '#fff';
    let map = [];
    let size = 61;
    for (let i = 0; i < size; i++) { //初始化迷宫的墙
      map.push([]);
      for (let j = 0; j < size; j++) {
        if (i === 0 || i === size - 1 || j === 0 || j === size - 1) {
          map[i].push(1);
        } else {
          map[i].push(0);
        }
      }
    }
    map[0][1] = 0;
    map[size - 1][size - 2] = 0;

    function getEven(min, max) { // 随机获取区间中的偶数
      let result;
      while (true) {
        result = self.game.math.between(min, max) || 0;
        if (result % 2 === 0 && result !== min && result !== max) {
          break;
        }
      }
      return result;
    }
    function getOdd(min, max) { // 随机获取区间中的奇数
      let result;
      while (true) {
        result = self.game.math.between(min, max) || 0;
        if (result % 2 === 1 && result !== min && result !== max) {
          break;
        }
      }
      return result;
    }

    // 生成迷宫的函数
    function generate(map, x1, y1, x2, y2) {
      if (x2 - x1 <= 2 || y2 - y1 <= 2) { //如果没位置了,直接return
        return;
      }
      // 生成十字围墙
      let wallY = getEven(y1, y2); 
      let wallX = getEven(x1, x2);
      for (let i = x1; i < x2; i++) {
        map[wallY][i] = 1;
      }
      for (let i = y1; i < y2; i++) {
        map[i][wallX] = 1;
      }
      // 随机挖三个洞
      let direct = [1, 1, 1, 1];
      direct[Math.random() * 4 | 0] = 0;
      if (direct[0]) { // 上
        let roadY = getOdd(y1, wallY);
        map[roadY][wallX] = 0;
      }
      if (direct[1]) { // 下
        let roadY = getOdd(wallY, y2);
        map[roadY][wallX] = 0;
      }
      if (direct[2]) { // 左
        let roadX = getOdd(x1, wallX);
        map[wallY][roadX] = 0;
      }
      if (direct[3]) { // 右
        let roadX = getOdd(wallX, x2);
        map[wallY][roadX] = 0;
      }
      // 继续递归
      generate(map, x1, y1, wallX, wallY);
      generate(map, wallX, y1, x2, wallY);
      generate(map, x1, wallY, wallX, y2);
      generate(map, wallX, wallY, x2, y2);
    }
    generate(map, 0, 0, map[0].length - 1, map.length - 1);
    let bmd = this.add.bitmapData(5, 5);
    bmd.context.fillStyle = '#000';
    bmd.context.fillRect(0, 0, 5, 5);
    this.cache.addBitmapData('wall', bmd);
    for (let i = 0; i < map.length; i++) {
      for (let j = 0; j < map[0].length; j++) {
        if(map[i][j]){
          this.add.sprite(j * 5, i * 5, this.cache.getBitmapData('wall'));
        }
      }
    }
  }
}

迷宫生成算法JS实现_第1张图片
效果如图所示

你可能感兴趣的:(迷宫生成算法JS实现)