mercredi 23 décembre 2015

Need help javaScript

var grid = {
  _grid: [],

  createGrid: function (c) {
    for (var i = 0; i < c; i++) {
      this._grid.push([]);
      for (var a = 0; a < c; a++) {
        this._grid[i][a] = 'Empty';
      }
    }
  },

  pushBombs: function (b) {
    for (var i = 0; i < b; i++) {
      if (this._grid[Math.round(Math.random() * (this._grid.length - 1))][Math.round(Math.random() * (this._grid.length - 1))] == 'Empty') {
        this._grid[Math.round(Math.random() * (this._grid.length - 1))][Math.round(Math.random() * (this._grid.length - 1))] = 'Bomb';
      }
    }
  },

  • Hello everyone. I'm trying to make JavaScript Minesweeper and I've got some problems in 'pushBombs' function, I guess. I'm trying to put bombs in array, sometimes it puts exact number that is said but sometimes it puts less number.

    My questions:

    1. When condition is true it puts bomb in array but when it's false it passes this itaration or looks for another index in array that is true?
    2. Math.random() returns different values in condition and in code block.
    3. It puts bomb where it already was but then why?

Aucun commentaire:

Enregistrer un commentaire