Created
August 3, 2021 03:50
-
-
Save creationix/3bc6ea5dff0895bb34f9b580d15a0ee1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var WIDTH = 10 | |
var HEIGHT = 10 | |
// 0 is null, pointers start at 1 | |
const SIZE = WIDTH * HEIGHT | |
var set = new Uint16Array(SIZE) | |
// return end of chain | |
function walk(a) { | |
while (set[a - 1]) { | |
a = set[a - 1] | |
} | |
return a | |
} | |
// return true if they were different and we merged them | |
function merge(a, b) { | |
a = walk(a) | |
b = walk(b) | |
if (a == b) return 1; | |
set[a - 1] = b | |
return 0 | |
} | |
var wall_order = new Uint16Array(SIZE * 2) | |
var walls = new Uint16Array(SIZE * 2) | |
for (var i = 0; i < SIZE * 2; i++) { | |
wall_order[i] = i | |
} | |
// Shuffle Shuffle | |
for (var i = 0; i < SIZE * 2; i++) { | |
var b = Math.floor(Math.random() * SIZE * 2) | |
var temp = wall_order[i] | |
wall_order[i] = wall_order[b] | |
wall_order[b] = temp; | |
} | |
for (var i = 0; i < SIZE * 2; i++) { | |
var index = wall_order[i] | |
var a, b | |
if (index < SIZE) { // first set are right walls | |
a = index + 1 | |
// skip right walls on right side | |
if ((a % WIDTH) == 0) { | |
b = a | |
} else { | |
b = a + 1 | |
} | |
} else { // next set are bottom walls | |
a = index - SIZE + 1 | |
// skip bottom walls on bottom side | |
if (a + WIDTH > SIZE) { | |
b = a | |
} else { | |
b = a + WIDTH | |
} | |
} | |
walls[index] = merge(a, b) | |
} | |
var icons = [" ▄", " █", "▄▄", "▄█"] | |
var lines = [] | |
for (var y = -1; y < HEIGHT; y++) { | |
var line = "" | |
for (var x = -1; x < WIDTH; x++) { | |
var index | |
if (y == -1 && x == -1) { | |
index = 0 | |
} else if (y == -1) { | |
index = 2 | |
} else if (x == -1) { | |
index = 1 | |
} else { | |
var i = x + y * WIDTH | |
index = walls[i] + walls[i + SIZE] * 2 | |
} | |
line += icons[index] | |
} | |
lines.push(line) | |
} | |
console.log(lines.join("\n")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment