mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-24 14:19:56 +02:00
Add flagging support to minesweeper
This commit is contained in:
@@ -69,7 +69,7 @@ module.exports = class PokerCommand extends Command {
|
|||||||
while (!winner) {
|
while (!winner) {
|
||||||
for (const player of players.values()) {
|
for (const player of players.values()) {
|
||||||
if (players.has(player.id)) continue;
|
if (players.has(player.id)) continue;
|
||||||
rotation = removeFromArray(player.id);
|
rotation = removeFromArray(rotation, player.id);
|
||||||
}
|
}
|
||||||
const bigBlind = players.get(rotation[1]);
|
const bigBlind = players.get(rotation[1]);
|
||||||
bigBlind.money -= bigBlindAmount;
|
bigBlind.money -= bigBlindAmount;
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
const Command = require('../../structures/Command');
|
const Command = require('../../structures/Command');
|
||||||
const BombSweeper = require('bombsweeper.js');
|
const BombSweeper = require('bombsweeper.js');
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
const nums = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣'];
|
const { removeFromArray, verify } = require('../../util/Util');
|
||||||
|
const nums = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟'];
|
||||||
|
const turnRegex = /(flag )?(\d+), ?(\d+)/i;
|
||||||
|
|
||||||
module.exports = class MinesweeperCommand extends Command {
|
module.exports = class MinesweeperCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
@@ -17,7 +19,7 @@ module.exports = class MinesweeperCommand extends Command {
|
|||||||
prompt: 'What size board do you want to use?',
|
prompt: 'What size board do you want to use?',
|
||||||
type: 'integer',
|
type: 'integer',
|
||||||
default: 9,
|
default: 9,
|
||||||
max: 9,
|
max: 10,
|
||||||
min: 3
|
min: 3
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -34,20 +36,22 @@ module.exports = class MinesweeperCommand extends Command {
|
|||||||
let win = null;
|
let win = null;
|
||||||
game.onWin = () => { win = true; };
|
game.onWin = () => { win = true; };
|
||||||
game.onLoss = () => { win = false; };
|
game.onLoss = () => { win = false; };
|
||||||
|
let flagged = [];
|
||||||
while (win === null) {
|
while (win === null) {
|
||||||
await msg.say(stripIndents`
|
await msg.say(stripIndents`
|
||||||
${msg.author}, what coordinates do you pick (ex. 4,5)? Type \`end\` to forefeit.
|
${msg.author}, what coordinates do you pick (ex. 4,5)? Type \`end\` to forefeit.
|
||||||
|
Type \`flag <coordinates>\` to flag a spot as a bomb. To remove a flag, run it again.
|
||||||
|
|
||||||
${this.displayBoard(game.board, game.mask)}
|
${this.displayBoard(game.board, game.mask, flagged)}
|
||||||
`);
|
`);
|
||||||
const filter = res => {
|
const filter = res => {
|
||||||
if (res.author.id !== msg.author.id) return false;
|
if (res.author.id !== msg.author.id) return false;
|
||||||
const pick = res.content;
|
const pick = res.content;
|
||||||
if (pick.toLowerCase() === 'end') return true;
|
if (pick.toLowerCase() === 'end') return true;
|
||||||
const coordPicked = pick.match(/(\d), ?(\d)/i);
|
const coordPicked = pick.match(turnRegex);
|
||||||
if (!coordPicked) return false;
|
if (!coordPicked) return false;
|
||||||
const x = Number.parseInt(coordPicked[1], 10);
|
const x = Number.parseInt(coordPicked[2], 10);
|
||||||
const y = Number.parseInt(coordPicked[2], 10);
|
const y = Number.parseInt(coordPicked[3], 10);
|
||||||
if (x > size || y > size || x < 1 || y < 1) return false;
|
if (x > size || y > size || x < 1 || y < 1) return false;
|
||||||
if (game.mask[y - 1][x - 1]) return false;
|
if (game.mask[y - 1][x - 1]) return false;
|
||||||
return true;
|
return true;
|
||||||
@@ -65,11 +69,28 @@ module.exports = class MinesweeperCommand extends Command {
|
|||||||
win = false;
|
win = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const coordPicked = choice.match(/(\d), ?(\d)/i);
|
const coordPicked = choice.match(turnRegex);
|
||||||
const x = Number.parseInt(coordPicked[1], 10);
|
const x = Number.parseInt(coordPicked[2], 10);
|
||||||
const y = Number.parseInt(coordPicked[2], 10);
|
const y = Number.parseInt(coordPicked[3], 10);
|
||||||
game.CheckCell(x - 1, y - 1); // eslint-disable-line new-cap
|
const flag = Boolean(coordPicked[1]);
|
||||||
if (win === true || win === false) break;
|
if (flag) {
|
||||||
|
if (flagged.includes(`${x - 1},${y - 1}`)) {
|
||||||
|
flagged = removeFromArray(flagged, `${x - 1},${y - 1}`);
|
||||||
|
} else {
|
||||||
|
flagged.push(`${x - 1},${y - 1}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (flagged.includes(`${x - 1},${y - 1}`)) {
|
||||||
|
await msg.say('Are you sure you want to check this spot? You have it flagged.');
|
||||||
|
const verification = await verify(msg.channel, msg.author);
|
||||||
|
if (!verification) {
|
||||||
|
await msg.say('Okay, the spot will remain unchecked.');
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
game.CheckCell(x - 1, y - 1); // eslint-disable-line new-cap
|
||||||
|
if (win === true || win === false) break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.client.games.delete(msg.channel.id);
|
this.client.games.delete(msg.channel.id);
|
||||||
if (win === null) return msg.say('Game ended due to inactivity.');
|
if (win === null) return msg.say('Game ended due to inactivity.');
|
||||||
@@ -84,7 +105,7 @@ module.exports = class MinesweeperCommand extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
displayBoard(board, mask) {
|
displayBoard(board, mask, flagged) {
|
||||||
let str = '';
|
let str = '';
|
||||||
str += '⬛';
|
str += '⬛';
|
||||||
str += nums.slice(0, board.length).join('');
|
str += nums.slice(0, board.length).join('');
|
||||||
@@ -100,6 +121,8 @@ module.exports = class MinesweeperCommand extends Command {
|
|||||||
} else {
|
} else {
|
||||||
str += nums[item - 1];
|
str += nums[item - 1];
|
||||||
}
|
}
|
||||||
|
} else if (flagged.includes(`${i},${j}`)) {
|
||||||
|
str += '🚩';
|
||||||
} else {
|
} else {
|
||||||
str += '❓';
|
str += '❓';
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiao",
|
"name": "xiao",
|
||||||
"version": "126.3.1",
|
"version": "126.3.2",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "Xiao.js",
|
"main": "Xiao.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user