1

I have the following code, to set up the mines in a minesweeper game, but I keep getting an error in the plantMines() function, "TypeError: data[randomx] is undefined". I'm new to React, so perhaps I'm missing something simple, but I just cannot see where the problem is.

initBoardData(height, width, mines) {
    let data = this.createEmptyArray(height, width);
    data = this.plantMines(data, height, width, mines);
    return data;
}

// Create array of grid
createEmptyArray(height, width) {
    let data = [];

    for (let i = 0; i < height; i++) {
        data.push([]);
        for (let j = 0; j < width; j++) {
            data[i][j] = {
                x: i,
                y: j,
                isMine: false,
                neighbour: 0,
                isRevealed: false,
                isEmpty: false,
                isFlagged: false
            };
        }
    }
    return data;
}

// Place mines on board
plantMines(data, height, width, mines) {
    let randomx, randomy, minesPlanted = 0;

    while (minesPlanted < mines) {
        randomx = this.getRandomNumber(width);
        randomy = this.getRandomNumber(height);

        if (!(data[randomx][randomy].isMine)) {    //--ERROR ON THIS LINE
            data[randomx][randomy].isMine = true;
            minesPlanted++;
        }
    }
    return (data);
}

getRandomNumber(dimension) {
    return Math.floor((Math.random() * 1000) + 1 % dimension);
}

EDIT: codesandbox - https://codesandbox.io/s/minesweeper-luono?file=/src/Board.js

4
  • Please create a small demo for this using codesandbox.io to show the issue happening. Commented Apr 20, 2020 at 10:01
  • codesandbox.io/s/minesweeper-luono?file=/src/Board.js Commented Apr 20, 2020 at 10:49
  • Your demo has other errors. Please fix those also except if (!(data[randomx][randomy].isMine)) which is the original issue. Commented Apr 20, 2020 at 10:54
  • @T.Baer Fix <Board height={height} width={width} mines={mines} /> Commented Apr 20, 2020 at 11:31

5 Answers 5

3

You have error in getRandomNumber - missing parentheses:

getRandomNumber(dimension) {
    return Math.floor(((Math.random() * 1000) + 1) % dimension);
  }

There is one more problem though, use the solution that @dellink presented too:

randomx = this.getRandomNumber(height);
randomy = this.getRandomNumber(width);

You have to make BOTH changes, so it starts working!!

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

4 Comments

Thanks, that hasn't solved the original error though
What do you mean? Is there a new error now? I tested it out and it seemed to work.
When I was making my tests, i was using a square - 10/10, but when you have rectangular dimension, the error was triggered. Now it got fixed.
Hmm that still hasn't changed anything for me
3

I figured out the problem. height and width were not properly defined, so randomx and randomy were being taken as strings, not numbers, thus the array definition error.

Comments

2

You are trying to not existing index of the array. Thus; you are getting undefined error. You can use below or @Enchew's answer.

getRandomNumber(dimension) {
   return Math.floor(Math.random() * Math.floor(dimension));
}

Comments

1

According to your code you should access your array like: data[randomy][randomx]

1 Comment

Switching them doesn't solve the error, just changes it to "TypeError: data[randomy] is undefined"
1

try change in this way

randomx = this.getRandomNumber(height);
randomy = this.getRandomNumber(width);

1 Comment

Thanks for the suggestion, though it didn't seem to make a difference

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.