From 0f8d485df37f8850167ae9151440429432035a94 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Wed, 6 Sep 2017 21:44:51 +0000 Subject: [PATCH] Tic-Tac-Toe is ridiculous but it works --- commands/games/battle.js | 6 +-- commands/games/tic-tac-toe.js | 97 +++++++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 commands/games/tic-tac-toe.js diff --git a/commands/games/battle.js b/commands/games/battle.js index 1d5bf663..5d04dd9b 100644 --- a/commands/games/battle.js +++ b/commands/games/battle.js @@ -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 }); diff --git a/commands/games/tic-tac-toe.js b/commands/games/tic-tac-toe.js new file mode 100644 index 00000000..27d48db5 --- /dev/null +++ b/commands/games/tic-tac-toe.js @@ -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!`); + } + } +}; diff --git a/package.json b/package.json index 6b78a974..cd5edcac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "37.1.0", + "version": "37.2.0", "description": "Your personal server companion.", "main": "Shard.js", "scripts": {