Skip to content

Instantly share code, notes, and snippets.

@lewdev
Last active December 28, 2024 22:55
Show Gist options
  • Select an option

  • Save lewdev/6f2480e5083f56f608a2dd51540473db to your computer and use it in GitHub Desktop.

Select an option

Save lewdev/6f2480e5083f56f608a2dd51540473db to your computer and use it in GitHub Desktop.
πŸ―πŸ’³ Dungeon Generator on a business card #bookmarklet

πŸ―πŸ’³ Dungeon Generator on a business card #bookmarklet

I saw this here: https://gist.github.com/munificent/b1bcd969063da3e6c298be070a22b604

But it was in C so I converted it into JavaScript. First I used this tool: https://www.codeconvert.ai/c-to-javascript-converter

And then I had to make a few tweaks to get it to display a to the screen.

Run it in your Address bar (802b bookmarklet)

data:text/html,<title>Dungeon Gen</title><script>r='';R=n=>~~(Math.random()*n),H=40,W=80,m=Array.from({length:40},()=>Array(80).fill(' ')),V=r=>{let o=R(10)+5,e=R(6)+3,f=R(80-o-2)+1,n=R(40-e-2)+1;for(B=n-1;B<n+e+2;B++)for(a=f-1;a<f+o+2;a++)if('.'===m[B][a])return;let t,l,d=0;if(!r){for(i=n-1;i<n+e+2;i++)for(c=f-1;c<f+o+2;c++)(c<f||c>f+o)^(i<n||i>n+e)&&'%23'===m[i][c]&&0===R(++d)&&(t=c,l=i);if(0===d)return}for(h=n-1;h<n+e+2;h++)for(u=f-1;u<f+o+2;u++){let r=u<f||u>f+o,t=h<n||h>n+e;m[h][u]=r&&t?'!':r^t?'%23':'.'}for(d>0&&(m[l][t]=R(2)?`'`:'+'),g=0;g<(r?1:R(6)+1);g++)m[R(e)+n][R(o)+f]=r?'@':0===R(4)?'%24':String.fromCharCode(65+R(62))},onload=f=>{for(r='',e=0;e<1e3;e++)V(0===e);for(n=0;n<40;n++)for(t=0;t<80;t++){let o=m[n][t];r+='!'===o?'%23':o,79===t&&(r+='\n')}z.innerHTML=r}</script><pre id=z>

Here's an example of what I generated:

                                        ############                            
                                        #.....\....#                            
                                        #..........#                            
  #################                     #....N.....#         ##########         
  #...$...........#                     #..........#         #........##########
  #...............#                     #...$......########  #........#x....u..#
  #...............#   ################  #..........#..$...#  #....i.u.#[.......#
  #...............#   #j....y........#  #..........+..|...#  #..$.....#....$...#
  #...............#   #..............#  ############......#  #........#........#
  #.......R.......#   #..............#             #......#  #######'###+#######
  #...............#   #..............#        #######+#####       #.$....#      
  #...............#   #..............#        #....$....#         #......#      
  ###############+######'######################.........#         #......#      
               #......g..#          #p..J.....+N........#         #..L...#      
               #.....c...#          #.........#...._....###########.$....#      
               #.....k...#          #.........#.........##....m...#......#      
    ############.........#          #.........############........'......#      
    #.$...T...##.........#          #.........#          #..$...s.####+#########
    #y....{...##.........#          #.........############^.......##..\........#
    #....U....##....$..$.########## #.........#.......N..+........##...........#
    #.........##.........'......W.# #.........+......K...###########$..j.......#
    #########'##'#########........######+######..........#         #...........#
           #.......`...# #........#......#    #....a.....#         #############
           #...........# #........#..@...#    #..........#          #...n......#
           #.$.......A.# #........#......#    #..........#          #..x.......#
           #...........# #a.......#......#    #######+############# #..........#
           #...........# #........+......#         #...]...y......# #..........#
           #################+######......#         #..............# #...Q......#
                   #.............##......#         #..............# #..........#
                   #.............###'##########    #.........F....##########'###
                   #.............#..r.........#    #..............#..........#  
                   #.............#............#    #..............'..........#  
   #################....]........#............#    ###########'####..........#  
   #...............#.............#.$.....y....#    #.............##[.....b...#  
   #..G.....Z...|..'.........L...#............#    #.............##...$......#  
   #........c......#.............#............#    #.............##..y.......#  
   #...............###############............#    #_.$..........##..........#  
   #...............#             ##############    #.............##..........#  
   #################                               ###########################  
<pre id=o></pre><script>let s="";R=r=>~~(Math.random()*r),H=40,W=80;m=Array.from({length:40},()=>Array(80).fill(" "));cave=r=>{let e=R(10)+5,n=R(6)+3,t=R(80-e-2)+1,l=R(40-n-2)+1;for($=l-1;$<l+n+2;$++)for(a=t-1;a<t+e+2;a++)if("."===m[$][a])return;let f=0,d,_;if(!r){for(i=l-1;i<l+n+2;i++)for(c=t-1;c<t+e+2;c++)(c<t||c>t+e)^(i<l||i>l+n)&&"#"===m[i][c]&&0===R(++f)&&(d=c,_=i);if(0===f)return}for(h=l-1;h<l+n+2;h++)for(u=t-1;u<t+e+2;u++){let v=u<t||u>t+e,C=h<l||h>l+n;m[h][u]=v&&C?"!":v^C?"#":"."}f>0&&(m[_][d]=R(2)?"'":"+");for(g=0;g<(r?1:R(6)+1);g++)m[R(n)+l][R(e)+t]=r?"@":0===R(4)?"$":String.fromCharCode(65+R(62))};onload=r=>{s="";for(e=0;e<1e3;e++)cave(0===e);for(n=0;n<40;n++)for(t=0;t<80;t++){let l=m[n][t];s+="!"===l?"#":l,79===t&&(s+="\n")}o.innerHTML=s};</script>
<pre id=o></pre>
<script>
let s = "";
const rand = max => ~~(Math.random() * max);
const H = 40;
const W = 80;
let m = Array.from({ length: H }, () => new Array(W).fill(' '));
const cave = s => {
let w = rand(10) + 5;
let h = rand(6) + 3;
let t = rand(W - w - 2) + 1;
let u = rand(H - h - 2) + 1;
for (let y = u - 1; y < u + h + 2; y++)
for (let x = t - 1; x < t + w + 2; x++) if (m[y][x] === '.') return;
let d = 0;
let e, f;
if (!s) {
for (let y = u - 1; y < u + h + 2; y++) {
for (let x = t - 1; x < t + w + 2; x++) {
let s = x < t || x > t + w;
let t2 = y < u || y > u + h;
if ((s ^ t2) && m[y][x] === '#') {
d++;
if (rand(d) === 0) e = x, f = y;
}
}
}
if (d === 0) return;
}
for (let y = u - 1; y < u + h + 2; y++) {
for (let x = t - 1; x < t + w + 2; x++) {
let s = x < t || x > t + w;
let t2 = y < u || y > u + h;
m[y][x] = s && t2 ? '!' : s ^ t2 ? '#' : '.';
}
}
if (d > 0) m[f][e] = rand(2) ? '\'' : '+';
for (let j = 0; j < (s ? 1 : rand(6) + 1); j++) {
m[rand(h) + u][rand(w) + t] = s ? '@' : rand(4) === 0 ? '$' : String.fromCharCode(65 + rand(62));
}
};
onload = _ => {
s = "";
for (let j = 0; j < 1e3; j++) cave(j === 0);
for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
let c = m[y][x];
s += (c === '!' ? '#' : c);
if (x === W - 1) s += "\n";
}
o.innerHTML = s;
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment