forked from eltotoloco/js-katas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.js
97 lines (87 loc) · 1.98 KB
/
minesweeper.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Create a function `sweep` which takes a grid and two numbers as parameters :
* grid : a minesweeper matrix where 0 are empty cells and 1 are bombs. The width and height of the grid can vary.
* row : the vertical coordinate of the cell to sweep
* column : the horizontal coordinate of the cell to sweep
The function will check the cell using the coordinates and returns :
* "kaboom", if it contains a boom
* the number of adjacent bombs, if it's empty
The function will throw :
* TypeError if grid is null or not an array
* TypeError if at least one value of the grid is not 0 or 1
* TypeError if row or column are null or not a number
* RangeError if grid has a width or height inferior to one
* RangeError if row or column are out of bounds
Example :
grid :
[
[0, 0, 0, 1],
[0, 1, 0, 0],
[1, 0, 0, 0]
]
row : 1
column : 2
result : 2
*/
// TODO add your code here
// Begin of tests
const assert = require("assert");
assert.strictEqual(typeof sweep, "function");
assert.strictEqual(sweep.length, 3);
const grid = [
[0, 0, 0, 1],
[0, 1, 0, 0],
[1, 0, 0, 0],
];
assert.strictEqual(sweep(grid, 1, 1), "kaboom");
assert.strictEqual(sweep(grid, 0, 3), "kaboom");
assert.strictEqual(sweep(grid, 2, 0), "kaboom");
assert.strictEqual(sweep(grid, 0, 0), 1);
assert.strictEqual(sweep(grid, 1, 2), 2);
assert.strictEqual(sweep(grid, 2, 3), 0);
assert.throws(() => {
sweep(null, 1, 1);
}, TypeError);
assert.throws(() => {
sweep("a", 1, 1);
}, TypeError);
assert.throws(() => {
sweep(
[
[0, 0, "0", 1],
[0, 1, 0, 1],
[1, 0, 0, 0],
],
1,
1
);
}, TypeError);
assert.throws(() => {
sweep(
[
[0, 0, 0, 1],
[0, -1, 0, 1],
[1, 0, 0, 0],
],
1,
1
);
}, TypeError);
assert.throws(() => {
sweep(
[
[0, 0, 0, 1],
[0, 1, null, 1],
[1, 0, 0, 0],
],
0,
1
);
}, TypeError);
assert.throws(() => {
sweep(grid, -1, 1);
}, RangeError);
assert.throws(() => {
sweep(grid, 1, 5);
}, RangeError);
// End of tests