Created
February 2, 2019 04:26
-
-
Save metanomial/f0e998c21420ce0f61d11816a40aecad to your computer and use it in GitHub Desktop.
Generates minesweeper for Discord
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
// Use CLI flag --size <number> (or -s <number>) to set board dimensions | |
// Use CLI flag --count <number> (or -c <number>) to set bomb count | |
const argv = require('minimist')(process.argv.slice(2)) | |
const minesweeper = require('minesweeper') | |
const size = Math.max(Math.floor(Number(argv.size || argv.s)) || 8, 4) | |
const count = Math.min(Math.max(Math.floor(Number(argv.count || argv.c)) || Math.floor(size**2 / 6), 4), size**2) | |
const mineArray = minesweeper.generateMineArray({ | |
rows: size, | |
cols: size, | |
mines: count | |
}) | |
const board = new minesweeper.Board(mineArray).grid() | |
const icons = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'] | |
for(const row of board) { | |
for(const cell of row) { | |
if(cell.isMine) { | |
process.stdout.write(`||:bomb:||`) | |
} | |
else { | |
process.stdout.write(`||:${icons[cell.numAdjacentMines]}:||`) | |
} | |
} | |
process.stdout.write('\n') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment