Sha256: e19ae63ee328397ffe28a8d6830dda59f280cbce9c6d751c8fdeea2898b2a651

Contents?: true

Size: 1.13 KB

Versions: 66

Compression:

Stored size: 1.13 KB

Contents

var Minesweeper = function () {
  this.distanceXdistanceYs = [
    [-1, -1],
    [-1, 0],
    [-1, 1],

    [1, 1],
    [1, 0],
    [1, -1],

    [0, 1],
    [0, -1]

  ];
};


Minesweeper.prototype.annotate = function (rows) {
  if (rows.length === 0 || rows[0].length === 0) {
    return rows;
  }
  var board = rows.map(function (row) { return row.split(''); });
  var outBoard = [];
  board.forEach(function (memberX, x)  {
    outBoard[x] = [];
    memberX.forEach(function (memberXY, y) {
      var spot = memberXY;
      if (spot === '*') {
        outBoard[x][y] = spot;
      } else {
        var bombCount = this.distanceXdistanceYs.map(function (dxdy) {
          if (typeof board[x + dxdy[0]] === 'undefined') {
            return 0;
          }
          return board[x + dxdy[0]][y + dxdy[1]] === '*' ? 1 : 0;
        }).reduce(function (total, num) {
          return total + num;
        });
        outBoard[x][y] = bombCount > 0 ? bombCount : ' ';
      }
    }, this);
  }, this);
  return outBoard.map(function (row) {
    return row.join('');
  });
};

module.exports = Minesweeper;

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.118 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.117 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.116 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.115 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.114 tracks/javascript/exercises/minesweeper/example.js