Skip to content

Instantly share code, notes, and snippets.

@cchudant
Created May 8, 2020 23:16
Show Gist options
  • Save cchudant/04d7d5bdf6bfa1c007cb7d6124b10e18 to your computer and use it in GitHub Desktop.
Save cchudant/04d7d5bdf6bfa1c007cb7d6124b10e18 to your computer and use it in GitHub Desktop.
const ROWS = 10
const COLS = 10
const N = 20
const TABLE = [
':zero:', ':one:', ':two:', ':three:', ':four:',
':five:', ':six:', ':seven:', ':eight:',
]
const BOMB = ':bomb:'
const board = Array(ROWS * COLS).fill(0)
function incBoard(x, y) {
if (x < 0 || x >= ROWS || y < 0 || y >= COLS || board[y * ROWS + x] == -1)
return
board[y * ROWS + x]++
}
for (let i = 0; i < N;) {
const x = Math.floor(Math.random() * ROWS)
const y = Math.floor(Math.random() * COLS)
const index = y * ROWS + x
if (board[index] == -1)
continue;
incBoard(x - 1, y - 1)
incBoard(x , y - 1)
incBoard(x + 1, y - 1)
incBoard(x - 1, y )
board[index] = -1
incBoard(x + 1, y )
incBoard(x - 1, y + 1)
incBoard(x , y + 1)
incBoard(x + 1, y + 1)
i++;
}
let display = ''
for (let y = 0; y < COLS; y++) {
for (let x = 0; x < ROWS; x++) {
let val = board[y * ROWS + x]
let n = val != -1 ? TABLE[val] : BOMB
display += `||${n}||`
}
display += '\n'
}
console.log(display)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment