jozsefsallai / discord.js-minesweeper

Generate Minesweeper mine fields using Discord's spoiler tags.
MIT License
16 stars 4 forks source link

Discord.js Minesweeper

A Minesweeper generator library for Discord. See it in action here!

Build Status Coverage Status Package Size npm version

Table of Contents

Advantages

Usage

First, you need to add the library to your project (duh!):

npm install discord.js-minesweeper
# or
yarn add discord.js-minesweeper

Import it and use it as you wish!

const Minesweeper = require('discord.js-minesweeper');

// ...

const minesweeper = new Minesweeper();
minesweeper.start();
// Returns a Discord-interpretable string with a 9x9 matrix of spoilers and emojis, 10 of which are mines.

const minesweeper = new Minesweeper({
  rows: 12,
  columns: 16,
  mines: 20,
  emote: 'tada',
  returnType: 'code',
});
minesweeper.start();
// Returns a Discord-interpretable code block with a 12x16 matrix of spoilers and emojis, 20 of which are mines that will appear as the :tada: emote.

Options

Returned Data

Based on the specified returnType, the returned data can vary.

If a mine field is not possible with the provided data (i.e. too many mines), it will return null.

Methods

start()

Generates a minesweeper mine field and returns it.

Returns

spoilerize(str)

Turns a text into a Discord spoiler.

Params

Returns

getNumberOfMines(x, y)

Gets the number of mines in a particular (x, y) coordinate of the matrix.

Params

Returns

getTextRepresentation()

Returns the Discord message equivalent of the mine field.

Returns

populate()

Populates the matrix with mine counts.

revealFirst()

Reveal a random cell.

Returns


Note: the methods generateEmptyMatrix() and plantMines() are for internal use and should not be used outside the module. Using any of these could alter the generated matrix in unexpected ways. Don't use them in your app unless you know what you're doing.

Examples

Discord.js

const Discord = require('discord.js');
const Minesweeper = require('discord.js-minesweeper');

const bot = new Discord.Client();

bot.on('message', function (message) {
  const content = message.content.split(' ');
  const args = content.slice(1);

  if (content[0] === '.minesweeper') {
    const rows = parseInt(args[0]);
    const columns = parseInt(args[1]);
    const mines = parseInt(args[2]);

    if (!rows) {
      return message.channel.send(':warning: Please provide the number of rows.');
    }

    if (!columns) {
      return message.channel.send(':warning: Please provide the number of columns.');
    }

    if (!mines) {
      return message.channel.send(':warning: Please provide the number of mines.');
    }

    const minesweeper = new Minesweeper({ rows, columns, mines });
    const matrix = minesweeper.start();

    return matrix
      ? message.channel.send(matrix)
      : message.channel.send(':warning: You have provided invalid data.');
  }
});

bot.login(TOKEN);

Commando

const { Command } = require('discord.js-commando');
const Minesweeper = require('discord.js-minesweeper');

class MinesweeperCommand extends Command {
  constructor(client) {
    super(client, {
      // ...
      args: [
        {
          key: 'rows',
          prompt: 'How many rows?',
          type: 'integer',
          min: 4,
          max: 20
        },
        {
          key: 'columns',
          prompt: 'How many columns?',
          type: 'integer',
          min: 4,
          max: 20
        },
        {
          key: 'mines',
          prompt: 'How many mines?',
          type: 'integer',
          min: 1
        }
      ]
    });
  }

  async run(message, { rows, columns, mines }) {
    const minesweeper = new Minesweeper({ rows, columns, mines });
    const matrix = minesweeper.start();

    return matrix
      ? message.say(matrix)
      : message.say(':warning: The provided data is invalid.');
  }
};

module.exports = MinesweeperCommand;

Custom RNG (seeded Minesweeper)

You can now pass in any function that returns a number between 0 and 1 as the random number generator function. With the following basic example, you can create a seeded Minesweeper:

const seedrandom = require('seedrandom');

const myrng = seedrandom('myseed');

const minesweeper = new Minesweeper({
  rng: myrng
});

This will always generate the same Minesweeper game for the seed myseed.

License

Licensed under MIT. Check LICENSE for more information.