mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-12 08:14:47 +02:00
Tic-Tac-Toe is ridiculous but it works
This commit is contained in:
@@ -44,8 +44,7 @@ module.exports = class BattleCommand extends Command {
|
||||
let userTurn = false;
|
||||
let guard = false;
|
||||
const reset = (changeGuard = true) => {
|
||||
if (userTurn) userTurn = false;
|
||||
else userTurn = true;
|
||||
userTurn = !userTurn;
|
||||
if (changeGuard && guard) guard = false;
|
||||
};
|
||||
const dealDamage = damage => {
|
||||
@@ -60,13 +59,12 @@ module.exports = class BattleCommand extends Command {
|
||||
const user = userTurn ? msg.author : opponent;
|
||||
let choice;
|
||||
if (!opponent.bot || (opponent.bot && userTurn)) {
|
||||
const id = userTurn ? msg.author.id : opponent.id;
|
||||
await msg.say(stripIndents`
|
||||
${user}, do you **fight**, **guard**, **special**, or **run**?
|
||||
**${msg.author.username}**: ${userHP}HP
|
||||
**${opponent.username}**: ${oppoHP}HP
|
||||
`);
|
||||
const turn = await msg.channel.awaitMessages(res => res.author.id === id, {
|
||||
const turn = await msg.channel.awaitMessages(res => res.author.id === user.id, {
|
||||
max: 1,
|
||||
time: 30000
|
||||
});
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
const Command = require('../../structures/Command');
|
||||
const { stripIndents } = require('common-tags');
|
||||
|
||||
module.exports = class TicTacToeCommand extends Command {
|
||||
constructor(client) {
|
||||
super(client, {
|
||||
name: 'tic-tac-toe',
|
||||
group: 'games',
|
||||
memberName: 'tic-tac-toe',
|
||||
description: 'Play a game of tic-tac-toe.',
|
||||
args: [
|
||||
{
|
||||
key: 'opponent',
|
||||
prompt: 'What user would you like to challenge?',
|
||||
type: 'user',
|
||||
default: ''
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
this.playing = new Set();
|
||||
}
|
||||
|
||||
async run(msg, args) { // eslint-disable-line complexity
|
||||
const opponent = args.opponent || this.client.user;
|
||||
if (this.playing.has(msg.channel.id)) return msg.say('Only one game may be occurring per channel.');
|
||||
this.playing.add(msg.channel.id);
|
||||
try {
|
||||
if (!opponent.bot) {
|
||||
await msg.say(`${opponent}, do you accept this challenge?`);
|
||||
const verify = await msg.channel.awaitMessages(res => res.author.id === opponent.id, {
|
||||
max: 1,
|
||||
time: 30000
|
||||
});
|
||||
if (!verify.size || !['yes', 'y'].includes(verify.first().content.toLowerCase())) {
|
||||
this.fighting.delete(msg.channel.id);
|
||||
return msg.say('Looks like they declined...');
|
||||
}
|
||||
}
|
||||
const sides = ['0', '1', '2', '3', '4', '5', '6', '7', '8'];
|
||||
const taken = [];
|
||||
let userTurn = true;
|
||||
let winner = null;
|
||||
while (!winner && taken.length < 9) {
|
||||
const user = userTurn ? msg.author : opponent;
|
||||
const sign = userTurn ? 'X' : 'O';
|
||||
let choice;
|
||||
if (!opponent.bot || (opponent.bot && userTurn)) {
|
||||
await msg.code(null, stripIndents`
|
||||
${sides[0]} | ${sides[1]} | ${sides[2]}
|
||||
—————————
|
||||
${sides[3]} | ${sides[4]} | ${sides[5]}
|
||||
—————————
|
||||
${sides[6]} | ${sides[7]} | ${sides[8]}
|
||||
`);
|
||||
await msg.say(`${user}, which side do you pick?`);
|
||||
const turn = await msg.channel.awaitMessages(res => res.author.id === user.id, {
|
||||
max: 1,
|
||||
time: 30000
|
||||
});
|
||||
if (!turn.size) {
|
||||
await msg.say('Time!');
|
||||
break;
|
||||
}
|
||||
choice = turn.first().content;
|
||||
} else {
|
||||
const filter = sides.filter(side => !['X', 'O'].includes(side));
|
||||
choice = filter[Math.floor(Math.random() * filter.length)];
|
||||
}
|
||||
if (taken.includes(choice)) {
|
||||
await msg.say('That spot is already taken!');
|
||||
} else if (!sides.includes(choice)) {
|
||||
await msg.say('I don\'t think that is a valid spot...');
|
||||
} else {
|
||||
sides[parseInt(choice, 10)] = sign;
|
||||
taken.push(choice);
|
||||
if ((sides[0] === sides[1] && sides[0] === sides[2])
|
||||
|| (sides[0] === sides[3] && sides[0] === sides[6])
|
||||
|| (sides[3] === sides[4] && sides[3] === sides[5])
|
||||
|| (sides[1] === sides[4] && sides[1] === sides[7])
|
||||
|| (sides[6] === sides[7] && sides[6] === sides[8])
|
||||
|| (sides[2] === sides[5] && sides[2] === sides[8])
|
||||
|| (sides[0] === sides[4] && sides[0] === sides[8])
|
||||
|| (sides[2] === sides[4] && sides[2] === sides[6])) {
|
||||
winner = userTurn ? msg.author : opponent;
|
||||
}
|
||||
userTurn = !userTurn;
|
||||
}
|
||||
}
|
||||
this.playing.delete(msg.channel.id);
|
||||
return msg.say(winner ? `Congrats, ${winner}!` : 'Oh... The cat won.');
|
||||
} catch (err) {
|
||||
this.playing.delete(msg.channel.id);
|
||||
return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
}
|
||||
};
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiaobot",
|
||||
"version": "37.1.0",
|
||||
"version": "37.2.0",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Shard.js",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user