So basically, I am writing a Tic Tac Toe game for a Discord bot project I took. I just want the board[];
to be a multidimensional array. How do I do it?
Here's the code:
require('dotenv').config();
const { Client } = require('discord.js');
const client = new Client();
const PREFIX = process.env.DISCORD_BOT_PREFIX;
class TicTacToe {
/* here's the variable */
board[];
boardSize;
#emptyPiece;
#firstPiece;
#secondPiece;
constructor(boardSize, emptyPiece, firstPiece, secondPiece) {
this.boardSize = boardSize;
this.#emptyPiece = emptyPiece;
this.#firstPiece = firstPiece;
this.#secondPiece = secondPiece;
/* Initializing it here */
for (let i = 0; i < boardSize; i++)
for (let j = 0; j < boardSize; j++)
this.board[i][j] = emptyPiece;
}
isBoardEmpty() {
for (let i = 0; i < this.boardSize; i++)
for (let j = 0; j < this.boardSize; j++)
if (this.board[i][j] !== this.#emptyPiece) return false;
return true;
}
isPieceEmpty(x, y) {
return this.board[x][y] === this.#emptyPiece;
}
}
let ticTacToe = new TicTacToe(3, '-', 'x', 'o');
client.on('message', (message) => {
if (message.author.bot && !message.content.startsWith(PREFIX)) return;
const [COMMAND_NAME, ...args] = message.content.toLowerCase().trim().substring(PREFIX.length).split(/\s+/g);
if (COMMAND_NAME === 'showBoard') message.channel.send(ticTacToe.board);
});
client.login(process.env.DISCORD_BOT_TOKEN).then(r => {
console.log(`${client.user.tag} logged in!`);
});