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!`);
});

3
  • 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]]? Commented Jul 21, 2021 at 17:21
  • You have to initialize the array and each row of the array with []. Commented Jul 21, 2021 at 17:21
  • Does this answer your question? How can I create a two dimensional array in JavaScript? Commented Jul 21, 2021 at 17:57

2 Answers 2

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("-"));

}
Sign up to request clarification or add additional context in comments.

Comments

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]
];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.