1

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!`);
});
Shadow
  • 9
  • 4
  • 3
    In the class, it's just `board = []`, but why not make it a `this` variable in the constructor, i.e. `this.board = []`? If you want a preallocated size, did you want something like `board = [...Array(boardSize)].map(() => Array(boardSize).fill(0))` or `board = [[0,0,0],[0,0,0],[0,0,0]]`? – ggorlen Jul 21 '21 at 17:21
  • You have to initialize the array and each row of the array with `[]`. – Pointy Jul 21 '21 at 17:21
  • Does this answer your question? [How can I create a two dimensional array in JavaScript?](https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript) – Wyck Jul 21 '21 at 17:57

2 Answers2

1

You can create an array with a given size, then use .map() to change each element to an array of a given size.

let size = 3;

let board = Array.from({
  length: size
}).map(() => Array(size).fill("-"));

console.log(board);

This is what it would look like in your constructor

constructor(boardSize, emptyPiece, firstPiece, secondPiece) {
    this.boardSize = boardSize;
    this.#emptyPiece = emptyPiece;
    this.#firstPiece = firstPiece;
    this.#secondPiece = secondPiece;

    /* Initializing it here */
    this.board = Array.from({
      length: size
    }).map(() => Array(size).fill("-"));

}
lejlun
  • 4,140
  • 2
  • 15
  • 31
0

An array with each element also an array.
So if you want to change the middle zero, you do board[1][1] = 1

let board = [
    [0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]
];
Bataklik
  • 155
  • 3
  • 10