Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create minesweeper.js #871

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions JavaScript/minesweeper.js
Original file line number Diff line number Diff line change
@@ -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))