1
for (var i = 0; i < 10; i++) {
    bommen[i] = [
    Math.floor(Math.random() * 10),
    Math.floor(Math.random() * 10)];
}

This is my code for generating random 'bomb-coordinates'. It works, but it can put two bombs at the same place. The first Math.floor() is the X coordinate, and the second the Y coordinate. Does anyone have an idea how to fix this?

Tormod Fjeldskår
  • 5,952
  • 1
  • 29
  • 47

5 Answers5

0

This what you're looking for? They'll never be the same location.

    var locationOne = Math.floor(Math.random() * 10);
    var locationTwo = Math.floor(Math.random() * 10);
    while(locationOne === locationTwo){
      locationTwo = Math.floor(Math.random() * 10);
    }
    for (var i = 0; i < 10; i++) {
        bommen[i] = [
        locationOne,
        locationTwo];
    }
Tyler McGinnis
  • 34,836
  • 16
  • 72
  • 77
0

You can loop through the previous bomb co-ordinates, and if you've found one that has the same coordinates, retry. Something like this should work:

for (var i = 0; i < 100; i++) {
    var x = Math.floor(Math.random() * 10),
        y = Math.floor(Math.random() * 10),
        retry = false;
    for (var j = 0; j < i; j++) {
        if (bommen[j][0] == x && bommen[j][1] == y) {
            retry = true;
            break;
        }
    }
    if (retry) 
        i--;
    else 
        bommen[i] = [x, y];
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
0

The easiest way to do this is to add a close along the lines of

for (var i = 0; i < 10; i++) {
    if (bommen[i] != 0) // basically if there is already a bomb here
         i--;           //add another bomb
    bommen[i] = [
                Math.floor(Math.random() * 10),
                Math.floor(Math.random() * 10)];
    }

Though not very efficient this will insure that you have ten unique bombs.

Gibby
  • 458
  • 1
  • 4
  • 14
0

Here is my take:

var bommen = [[]];
var bommenObj = {};
var numMines = 10;
var rows = 10;
var columns = 10;
while(Object.keys(bommenObj).length < numMines) {
  var randX = Math.floor(Math.random() * rows);
  var randY = Math.floor(Math.random() * columns);
  bommenObj[[randX, randY]] = [randX,randY];
}
Object.keys(bommenObj).forEach(function(d,i) {
    bommen[i] = bommenObj[d];
});

The idea is to leverage Javascript's Associative Array (a.k.a. Objects) to ignore duplicate entries.

And then keep trying until you have 10 random coordinates.

Once we have 10 coordinates, we simply convert the Object into an Array.

bits
  • 8,110
  • 8
  • 46
  • 55
0

There is an issue with answers that check whether previous positions have already been used. You have 100 positions to use. Suppose you have used 99 positions and are picking the next position at random, then there is a small chance that this position is never picked. In general the more positions already picked the longer it takes to pick a new position.

See these answers for methods that sort the possible positions in random order then are read in order meaning each new pick must be unique and only chosen once with no repetitions.

Monopoly pick random card and pop array

https://stackoverflow.com/a/18806417/1937302

Community
  • 1
  • 1
jing3142
  • 1,821
  • 1
  • 11
  • 16