Skip to content

Instantly share code, notes, and snippets.

@nullableVoidPtr
Forked from stmkza/minesweeper.js
Last active September 22, 2019 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullableVoidPtr/3d6b00e32633c428e04be277df30d95e to your computer and use it in GitHub Desktop.
Save nullableVoidPtr/3d6b00e32633c428e04be277df30d95e to your computer and use it in GitHub Desktop.
const BOMB = -1;
const BLANK = 0;
function generateMap(width, height, bombCount)
{
const map = Array.from(new Array(height), () => Array.from(new Array(width), () => 0));
for(let i = 0; i < bombCount; ) {
const posX = Math.floor(Math.random() * width);
const posY = Math.floor(Math.random() * height);
if(map[posY][posX] !== BOMB) {
map[posY][posX] = BOMB;
i++;
}
}
for(let y = 0; y < height; y++) {
for(let x = 0; x < width; x++) {
if(map[y][x] === BOMB) {
continue;
}
for(let i = y - 1; i <= y + 1; i++) {
for(let j = x - 1; j <= x + 1; j++) {
if(i >= 0 && i < height && j >= 0 && j < width) {
if(map[i][j] === BOMB) {
map[y][x]++;
}
}
}
}
}
}
return map;
}
function mapToDiscordString(map)
{
return map.map(row => `||:${row.map(cell => (cell === BOMB) ? 'bomb' : ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'][cell]).join(':||||:')}:||`).join("\n");
}
const width = 9;
const height = 9;
const bombCount = 10;
if(width * height < bombCount) {
console.log('爆弾の数がマス目の数よりも多いです');
process.exit(1);
}
console.log(mapToDiscordString(generateMap(width, height, bombCount)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment