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.180 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.179 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.178 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.177 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.176 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.175 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.174 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.173 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.172 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.171 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.170 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.169 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.167 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.166 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.165 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.164 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.163 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.162 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.161 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.160 tracks/javascript/exercises/minesweeper/example.js