From 5dfce030d1062173b12842d07122a5e566fd5a79 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 9 Apr 2020 18:22:29 -0400 Subject: [PATCH] Add winner calculation logic --- commands/games-mp/dots-and-boxes.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/commands/games-mp/dots-and-boxes.js b/commands/games-mp/dots-and-boxes.js index a5303f79..86424cd2 100644 --- a/commands/games-mp/dots-and-boxes.js +++ b/commands/games-mp/dots-and-boxes.js @@ -37,7 +37,8 @@ module.exports = class DotsAndBoxesCommand extends Command { } const board = this.generateBoard(); const taken = []; - const owned = {}; + const userOwned = []; + const oppoOwned = []; let userTurn = true; let winner = null; while (taken.length < 40) { @@ -48,7 +49,7 @@ module.exports = class DotsAndBoxesCommand extends Command { P1: ${msg.author.tag} | P2: ${opponent.tag} \`\`\` - ${this.displayBoard(board, taken, owned)} + ${this.displayBoard(board, taken, userOwned, oppoOwned)} \`\`\` `); const filter = res => { @@ -99,14 +100,18 @@ module.exports = class DotsAndBoxesCommand extends Command { second = temp; } taken.push(`${first}-${second}`); - const newSquares = this.calcNewSquare(taken, owned); + const newSquares = this.calcNewSquare(taken, userOwned, oppoOwned); if (newSquares.length) { - for (const newSquare of newSquares) owned[newSquare] = userTurn ? 'P1' : 'P2'; + for (const newSquare of newSquares) { + if (userTurn) userOwned.push(newSquare); + else oppoOwned.push(newSquare); + } await msg.say(`${user}, great job! Keep going until you can\'t make any more!`); } else { userTurn = !userTurn; } } + winner = userOwned === oppoOwned ? null : userOwned > oppoOwned ? msg.author : opponent; this.client.games.delete(msg.channel.id); return msg.say(winner ? `Congrats, ${winner}!` : 'Looks like it\'s a draw...'); } catch (err) { @@ -122,10 +127,10 @@ module.exports = class DotsAndBoxesCommand extends Command { && taken.includes(`${num - 5}-${(num - 5) + 1}`); } - calcNewSquare(taken, owned) { + calcNewSquare(taken, userOwned, oppoOwned) { const newSquares = []; for (const square of squareIDs) { - if (owned[square]) continue; + if (userOwned.includes(square) || oppoOwned.includes(square)) continue; if (this.calcSquare(square, taken)) newSquares.push(square); } return newSquares; @@ -141,7 +146,7 @@ module.exports = class DotsAndBoxesCommand extends Command { return arr; } - displayBoard(board, taken, owned) { + displayBoard(board, taken, userOwned, oppoOwned) { const displayed = []; displayed.push(new Array(24).fill('█').join('')); displayed.push('█ █'); @@ -151,7 +156,7 @@ module.exports = class DotsAndBoxesCommand extends Command { for (let i = 0 + (row * 5); i < 5 + (row * 5); i++) { if (taken.includes(`${i - 5}-${i}`)) takenMids += '||'; else takenMids += ' '; - takenMids += owned[i] || ' '; + takenMids += userOwned.includes(i) ? 'P1' : oppoOwned.includes(i) ? 'P2' : ' '; } takenMids += '█'; displayed.push(takenMids);