-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom-bomb.js
44 lines (38 loc) · 1.05 KB
/
random-bomb.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
'use strict'
export function randomFromList(list, random) {
const index = Math.floor(random * list.length)
const rest = [
...list.slice(0, index),
...list.slice(index + 1)
]
return {
rest,
selected: list[index]
}
}
export function randomsFromRange(rangeMax, length, randomGen) {
const initalAcc = {rest: range(rangeMax), selected: []}
return Array.from({length}, randomGen)
.reduce(selectOneFromList, initalAcc)
.selected
}
function selectOneFromList(acc, random) {
const {rest, selected} = randomFromList(acc.rest, random)
return {
rest,
selected: [...acc.selected, selected]
}
}
function range(length) {
return Array.from({length}, (e, i) => i)
}
export function generateMap({width, height, bombs}) {
return Array.from({length: width * height}, () => 0)
.map((e, i) => {
const neighbors = getNeighborIndexes(i, width, height)
return neighbors.filter(tile => bombs.includes(tile)).length
})
}
export function getNeighborIndexes(i, width, height) {
return [i - 1, i + 1].filter(e => e >= 0)
}