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.159 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.158 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.157 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.156 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.155 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.154 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.153 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.152 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.151 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.150 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.149 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.148 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.147 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.146 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.145 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.144 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.143 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.142 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.141 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.140 tracks/javascript/exercises/minesweeper/example.js