From ad3a365771dbcf8aa02e57d910e3284bc7d036fb Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Fri, 15 Jan 2021 19:36:38 -0500 Subject: [PATCH] Minesweeper Command --- README.md | 3 +- commands/games-sp/minesweeper.js | 110 +++++++++++++++++++++++++++++++ package.json | 3 +- 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 commands/games-sp/minesweeper.js diff --git a/README.md b/README.md index 1ef9ea23..e02af227 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,7 @@ in the appropriate channel's topic to use it. ## Commands -Total: 574 +Total: 575 ### Utility: @@ -580,6 +580,7 @@ Total: 574 * **mad-libs:** Choose words that fill in the blanks to create a crazy story! * **math-quiz:** See how fast you can answer a math problem in a given time limit. * **memory:** Test your memory. +* **minesweeper:** Play a game of Minesweeper. * **quiz:** Answer a quiz question. * **reaction-time:** Test your reaction time. * **rock-paper-scissors:** Play Rock-Paper-Scissors. diff --git a/commands/games-sp/minesweeper.js b/commands/games-sp/minesweeper.js new file mode 100644 index 00000000..7b42546c --- /dev/null +++ b/commands/games-sp/minesweeper.js @@ -0,0 +1,110 @@ +const Command = require('../../structures/Command'); +const BombSweeper = require('bombsweeper.js'); +const { stripIndents } = require('common-tags'); +const nums = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣']; + +module.exports = class MinesweeperCommand extends Command { + constructor(client) { + super(client, { + name: 'minesweeper', + aliases: ['bombsweeper', 'mines', 'bombs', 'msweeper', 'minesweep', 'msweep'], + group: 'games-mp', + memberName: 'minesweeper', + description: 'Play a game of Minesweeper.', + args: [ + { + key: 'size', + prompt: 'What size board do you want to use?', + type: 'integer', + default: 9, + max: 9, + min: 3 + } + ] + }); + } + + async run(msg, { size }) { + const current = this.client.games.get(msg.channel.id); + if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); + this.client.games.set(msg.channel.id, { name: this.name }); + try { + const game = new BombSweeper(size, size); + game.PlaceBombs(size + 1); + const taken = []; + let win = null; + game.onWin(() => { win = true; }); + game.onLose(() => { win = false; }); + while (!win) { + await msg.say(stripIndents` + ${user}, what coordinates do you pick (ex. 4,5)? Type \`end\` to forefeit. + + ${this.displayBoard(game.board, game.mask)} + `); + const filter = res => { + if (res.author.id !== user.id) return false; + const pick = res.content; + if (pick.toLowerCase() === 'end') return true; + const coordPicked = pick.match(/(\d), ?(\d)/i); + if (!coordPicked) return false; + const x = Number.parseInt(coordPicked[1], 10); + const y = Number.parseInt(coordPicked[2], 10); + if (taken.includes(`${x},${y}`)) return false; + return true; + }; + const turn = await msg.channel.awaitMessages(filter, { + max: 1, + time: 30000 + }); + if (!turn.size) { + await msg.say('Sorry, time is up!'); + break; + } + const choice = turn.first().content; + if (choice.toLowerCase() === 'end') { + winner = userTurn ? opponent : msg.author; + break; + } + const coordPicked = choice.match(/(\d), ?(\d)/i) + const x = Number.parseInt(coordPicked[1], 10); + const y = Number.parseInt(coordPicked[2], 10); + taken.push(`${x},${y}`); + game.CheckCell(x, y); + } + this.client.games.delete(msg.channel.id); + if (!win) return msg.say('Game ended due to inactivity.'); + return msg.say(stripIndents` + ${win ? 'Nice job! You win!' : 'Sorry... You lose.'} + + ${this.displayBoard(game.board)} + `); + } catch (err) { + this.client.games.delete(msg.channel.id); + throw err; + } + } + + displayBoard(board, mask) { + let str = ''; + str += '⬜⬜'; + str += nums.slice(0, board.length).join(''); + str += '\n⬜\n'; + for (let i = 0; i < board.length; i++) { + str += nums[i]; + str += '⬜'; + board[i].forEach((item, j) => { + if (mask[i][j] === 'F') { + str += '❓'; + } else if (item === '*') { + str += '💣'; + } else if (item === 0) { + str += '⬜'; + } else { + str += nums[item - 1]; + } + }); + str += '\n'; + } + return str; + } +}; diff --git a/package.json b/package.json index 32d7a041..9bc963cb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "126.1.0", + "version": "126.2.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { @@ -35,6 +35,7 @@ "@discordjs/opus": "^0.3.3", "@vitalets/google-translate-api": "^4.0.0", "aki-api": "^5.2.1", + "bombsweeper.js": "^1.0.1", "canvas": "^2.6.1", "cheerio": "^1.0.0-rc.5", "cloc": "^2.7.0",