From e8cb473fd84157f132db42b9c3ade13615a1f6bf Mon Sep 17 00:00:00 2001 From: John Dao Date: Mon, 18 Oct 2021 10:35:01 +1300 Subject: [PATCH] Create minesweeper.js --- JavaScript/minesweeper.js | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 JavaScript/minesweeper.js diff --git a/JavaScript/minesweeper.js b/JavaScript/minesweeper.js new file mode 100644 index 00000000..7713afe7 --- /dev/null +++ b/JavaScript/minesweeper.js @@ -0,0 +1,43 @@ +//Simple minesweeper +// function take the board and change 1 into the bomb and 0 into the number show how many bombs around. to change the bomb location just change 1 and 0 location in the board variable +let board = [ + [1, 0, 0, 0], + [0, 1, 0, 1], + [0, 0, 0, 0], + [0, 0, 1, 0] +]; +//first loop for 4 nested array +function change1tox(board) { + for (i = 0; i < 4; i++) { + for (j = 0; j < 4; j++) { + if (board[i][j] === 1) { + board[i][j] = 'x' + } + } + + } + return board +} + +//second function increment around x position +function increment(board) { + let newBoard = [...(change1tox(board))] + for (i = 0; i < 4; i++) { + for (j = 0; j < 4; j++) { + if (board[i][j] === 'x') { + for (let row = i - 1; row < i + 2; row++) { + for (let col = j - 1; col < j + 2; col++) { + if (row >= 0 && col >= 0 && row <= 3 && col <= 3 && newBoard[row][col] !== 'x') { + newBoard[row][col]++ + } + } + } + + } + } + + + } + return newBoard +} +console.log(increment(board))