Skip to content

Instantly share code, notes, and snippets.

@nofishleft
Last active February 15, 2019 15:01
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nofishleft/ccaa378b20dfdacab1b5fe47abdf2220 to your computer and use it in GitHub Desktop.
Discord Minesweeper Message Generator
<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