Eller v0.1
by Sam Fereday
JavaScript
// All highly unoptimized currently (will work on that later)
// http://weblog.jamisbuck.org/2010/12/29/maze-generation-eller-s-algorithm
//
const cells = [];
//
class Cell {
constructor(v = -1) {
this.x = -1;
this.v = v;
}
}
// Consider a row
for(let r = 0; r < 5; r++) {
const cell = new Cell(r);
cells.push(cell);
}
// Randomly join
let currentSet;
let nextSet;
let lastRandom = 0;
let setIdx = 0;
const moddedCells = [];
for(let i = 0; i < cells.length; i++) {
// For each cell, randomly join it to the one before (or dont)
const changeSet = Math.round(Math.random());
const cell = cells[i];
if(lastRandom !== changeSet && i > 0) {
const prevCell = cells[i - 1];
nextSet = prevCell.set;
cell.v = prevCell.v;
lastRandom = changeSet;
setIdx += 1;
}
moddedCells.push(cell);
}
// Combine sets
const grouped = moddedCells.reduce((rv, x) => {
console.log(rv);
// No idea what the hell's going on here. Returns a function?
(rv[x['v']] = rv[x['v']] || []).push(x);
return rv;
}, {});
// Randomly select a vertical point, or a point to go vertical from (pick between 0 and n of the set/array).
// After which you can generate another cell within that array and smack it on to the screen.
// Any spaces left over can be filled with a new cell.
console.log(grouped);
// Any orphans are then randomly connected with pre existing sets
// Final row expects no verticals, but will need to be mapped to the bit before that.