/mines.html Secret
Last active
February 15, 2019 15:01
Star
You must be signed in to star a gist
Discord Minesweeper Message Generator
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
<html> | |
<head> | |
<script> | |
var mineOutput; | |
function minesweeper (X, Y, mines, char, formatted) { | |
let grid = []; | |
for (let y = 0; y < Y; ++y) { | |
grid[y] = new Array(X); | |
for (let x = 0; x < X; ++x) { | |
grid[y][x] = 0; | |
} | |
} | |
for (let i = 0; i < mines; ++i) { | |
let x,y; | |
while (grid[y = Math.floor(Math.random()*Y)][x = Math.floor(Math.random()*X)] == char) {} | |
grid[y][x] = char; | |
for (let yr = y-1; yr <= y + 1; ++yr) { | |
for (let xr = x-1; xr <= x + 1; ++xr) { | |
if (!(xr >= 0 || xr < X || yr >= 0 || yr < Y || grid[yr][xr] >= 0)) grid[yr][xr]++; | |
} | |
} | |
} | |
return formatted ? minesweeper_format(grid, X, Y, char) : grid; | |
}; | |
function minesweeper_format (grid, X, Y, char) { | |
let strArray = []; | |
for (let y = 0; y < Y; ++y) { | |
strArray[y] = `||${grid[y].join("||||")}||`; | |
} | |
const bombex = new RegExp(char, "gi"); | |
return strArray.join('\r\n') | |
.replace(bombex, ":bomb:") | |
.replace(/0/gi, ":zero:") | |
.replace(/1/gi, ":one:") | |
.replace(/2/gi, ":two:") | |
.replace(/3/gi, ":three:") | |
.replace(/4/gi, ":four:") | |
.replace(/5/gi, ":five:") | |
.replace(/6/gi, ":six:") | |
.replace(/7/gi, ":seven:") | |
.replace(/8/gi, ":eight:"); | |
}; | |
function create () { | |
let x = parseInt(document.getElementById("x")); | |
let y = parseInt(document.getElementById("y")); | |
let m = parseInt(document.getElementById("m")); | |
mineOutput = minesweeper(14,14,20,'X',true); | |
if (!window.isSecureContext) { | |
console.log("Window is not secured outputting to the global 'mineOutput' variable"); | |
return; | |
} | |
copyToClipboard(mineOutput); | |
} | |
const copyToClipboard = str => { | |
navigator.clipboard.writeText(str).then(function() { | |
console.log("Copied to clipboard successfully!"); | |
}, function() { | |
console.error("Unable to write to clipboard. :-("); | |
}); | |
}; | |
</script> | |
</head> | |
<body> | |
Width:<br> | |
<input id="x" type="text"><br><br> | |
Height:<br> | |
<input id="y" type="text"><br><br> | |
Mines:<br> | |
<input id="m" type="text"><br><br> | |
<button onclick="create();">Copy To Clipboard</button> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment