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.139 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.138 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.137 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.136 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.135 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.134 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.133 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.132 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.131 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.130 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.129 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.128 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.127 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.126 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.125 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.124 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.123 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.122 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.121 tracks/javascript/exercises/minesweeper/example.js
trackler-2.2.1.120 tracks/javascript/exercises/minesweeper/example.js