forked from kokonior/Javascript-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRube Martin.js
46 lines (39 loc) · 1.03 KB
/
Rube Martin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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))
console.log(increment(board))
console.log(increment(board))