{"id":1274,"date":"2019-11-03T01:01:57","date_gmt":"2019-11-03T05:01:57","guid":{"rendered":"https:\/\/resrvoir.com\/?page_id=1274"},"modified":"2019-11-04T09:56:35","modified_gmt":"2019-11-04T14:56:35","slug":"minesweeper","status":"publish","type":"page","link":"https:\/\/resrvoir.com\/?page_id=1274","title":{"rendered":"Minesweeper"},"content":{"rendered":"\n<p>Given a number of rows, columns, and mines, create a minesweeper board. Print the board where each mine is shown as an &#8220;X&#8221;.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const makeBoard = (rows, cols, mines) => {\n  let board = initBoard(rows, cols)\n  board = plantMines(rows, cols, board, mines)\n  board = plantMinesCount(rows, cols, board)\n  return board\n}\n\nconst initBoard = (rows, cols) => {\n  let board = []\n\n  for (let i = 0; i &lt; rows; i++) {\n    board[i] = new Array(cols)\n\n    for (let j = 0; j &lt; cols; j++) {\n      board[i][j] = 0\n    }\n  }\n\n  return board\n}\n\nconst plantMines = (rows, cols, board, mines) => {\n\n  while (mines) {\n    const r = randomNum(rows)\n    const c = randomNum(cols)\n\n    if (board[r][c] !== -1) {\n      board[r][c] = -1\n      mines -= 1\n    }\n  }\n\n  return board\n}\n\nconst plantMinesCount = (rows, cols, board) => {\n  for (let i = 0; i &lt; rows; i++) {\n    for (let j = 0; j &lt; cols; j++) {\n      board = countAdjacentMines(i, j, board)\n    }\n  }\n\n  return board\n}\n\nconst countAdjacentMines = (row, col, board) => {\n  if (board[row][col] == -1) {\n    return board\n  }\n\n  const rowAbove = row - 1\n  const rowBelow = row + 1\n  const colLeft  = col - 1\n  const colRight = col + 1\n\n  const adjacent = [\n    [rowAbove, colLeft],\n    [rowAbove, col],\n    [rowAbove, colRight],\n    [row, colLeft],\n    [row, colRight],\n    [rowBelow, colLeft],\n    [rowBelow, col],\n    [rowBelow, colRight]\n  ]\n\n  let count = 0\n\n  for (let i = 0; i &lt; adjacent.length; i++) {\n    const coordinates = adjacent[i]\n    const r = coordinates[0]\n    const c = coordinates[1]\n\n    if (r >= 0 &amp;&amp; r &lt; board.length &amp;&amp; c >= 0 &amp;&amp; c &lt; board[r].length) {\n      if (board[r][c] == -1) {\n        count += 1\n      }\n    }\n  }\n\n  board[row][col] = count\n\n  return board\n}\n\nconst drawBoard = (board) => {\n  const rows = board.length\n  const cols =  board[0].length\n  let output = \"\"\n\n  for (let i = 0; i &lt; rows; i++) {\n    for (let j = 0; j &lt; cols; j++) {\n      const value = board[i][j]\n\n      if (value == -1) {\n        output += \" X\"\n      }\n      else {\n        output = output + \" \" + value\n      }\n    }\n\n    output += \"\\n\"\n  }\n\n  return output\n}\n\nconst randomNum = (max) => {\n  return Math.floor(Math.random() * max)\n}\n\nconst board = makeBoard(5, 5, 5)\nconst drawn = drawBoard(board)\n\nconsole.log(board)\nconsole.log(drawn)<\/pre>\n\n\n\n<p>Here is the output:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[\n  [ 2, -1, 3, 2, -1 ],\n  [ 3, -1, 4, -1, 2 ],\n  [ 2, -1, 3, 1, 1 ],\n  [ 1, 1, 1, 0, 0 ],\n  [ 0, 0, 0, 0, 0 ]\n]\n 2 X 3 2 X\n 3 X 4 X 2\n 2 X 3 1 1\n 1 1 1 0 0\n 0 0 0 0 0<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a number of rows, columns, and mines, create a minesweeper board. Print the board where each mine is shown as an &#8220;X&#8221;. Here is the output:<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":26,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1274"}],"collection":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1274"}],"version-history":[{"count":5,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1274\/revisions"}],"predecessor-version":[{"id":1281,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/1274\/revisions\/1281"}],"up":[{"embeddable":true,"href":"https:\/\/resrvoir.com\/index.php?rest_route=\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/resrvoir.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}