npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

discord.js-minesweeper

v1.1.0

Published

Generate Minesweeper mine fields using Discord's spoiler tags.

Downloads

443

Readme

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

  • Runs pretty smooth.
  • It's very easy to use.
  • Supports multiple return formats.
  • Highly customizable.
  • No dependencies.
  • Written in TypeScript.

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

  • rows (number) - The number of rows in the mine field. Defaults to 9.
  • columns (number) - The number of columns in the mine field. Defaults to 9.
  • mines (number) - The number of mines in the mine field. Defaults to 10.
  • emote (string) - The emote used as a mine (without colons). Defaults to "boom".
  • spaces (boolean) - Specifies whether or not the emojis should be surrounded by spaces. Defaults to true.
  • zeroFirstCell (boolean) - Whether or not the first cell revealed should always be a zero (and automatically reveal any surrounding safe cells). Does nothing if revealFirstCell is false. Defaults to true.
  • revealFirstCell (boolean) - Whether or not the first cell should be revealed (like in regular Minesweeper). Defaults to FALSE.
  • returnType (string) - The type of the returned data.
  • rng (() => number) - The random number generator function used for placing mines. Must generate a number between 0 and 1. Defaults to Math.random.

Returned Data

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

  • 'emoji' - default value, returns a Discord message format with spoilers and emojis
  • 'code' - same as 'emoji', but in a code block (i.e. for copying)
  • 'matrix' - the matrix array itself.

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

  • string - A Discord-interpretable message.
  • string[][] - A matrix of the generated Minesweeper field.
  • null - If it's not possible to generate a Minesweeper field using the provided data.

spoilerize(str)

Turns a text into a Discord spoiler.

Params

  • string str - The string to spoilerize.

Returns

  • string - A Discord-interpretable spoiler containing the provided text.

getNumberOfMines(x, y)

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

Params

  • number x - The x coordinate (row).
  • number y - The y coordinate (column).

Returns

  • string - The number of mines surrounding the provided cell in the format of a spoilerized emoji.

getTextRepresentation()

Returns the Discord message equivalent of the mine field.

Returns

  • string - A Discord-interpretable message.

populate()

Populates the matrix with mine counts.

revealFirst()

Reveal a random cell.

Returns

  • SafeCell - the x and y coordinates of the revealed cell.

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.