Skip to content

Instantly share code, notes, and snippets.

@TheUnlocked
Created September 13, 2019 03:33
Show Gist options
  • Save TheUnlocked/3fcca4cdc371f61fe58ec53a0fe2af21 to your computer and use it in GitHub Desktop.
Save TheUnlocked/3fcca4cdc371f61fe58ec53a0fe2af21 to your computer and use it in GitHub Desktop.
Spoiler-based Mine Sweeper in Discord
const numberIcons = [
":zero:",
":one:",
":two:",
":three:",
":four:",
":five:",
":six:",
":seven:",
":eight:",
":nine:",
];
const bomb = ":anger:"
const bombChance = 0.06;
const minBombs = 6;
const boardSize = {x: 7, y: 7};
const generateBombBoard = () => {
const board = [];
for (let x = 0; x < boardSize.x; x++){
board.push([]);
for (let y = 0; y < boardSize.y; y++){
board[x].push(Math.random() < bombChance ? 1 : 0);
}
}
if (board.reduce((result, current) => result + current.reduce((result, current) => result + current, 0), 0) < minBombs){
return generateBombBoard();
}
return board;
}
const generateNeighborsBoard = (bombBoard) => {
const neighborBoard = new Array(boardSize.x).fill(0).map(x => new Array(boardSize.y).fill(0));
for (let x = 0; x < boardSize.x; x++){
for (let y = 0; y < boardSize.y; y++){
if (bombBoard[x][y] === 1){
for (r = -1; r < 2; r++)
for (c = -1; c < 2; c++)
if (x+r >= 0 && x+r < boardSize.x && y+c >= 0 && y+c < boardSize.y)
neighborBoard[x+r][y+c]++;
}
}
}
return neighborBoard;
}
const generateFullBoard = () => {
const bombBoard = generateBombBoard();
const neighborBoard = generateNeighborsBoard(bombBoard);
let result = "";
for (let x = 0; x < boardSize.x; x++){
for (let y = 0; y < boardSize.y; y++){
if (bombBoard[x][y] === 1){
result += `||${bomb}||`;
}
else{
result += `||${numberIcons[neighborBoard[x][y]]}||`;
}
}
result += "\n"
}
return result;
}
console.log(generateFullBoard());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment