/
05_minesweeper.jsx
52 lines (45 loc) · 1.32 KB
/
05_minesweeper.jsx
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
#BEGIN_PROPERTIES#
{
"commandsIntroduced": ["map.setSquareColor"],
"music": "intricate_cloudy_sin"
}
#END_PROPERTIES#
/******************
* minesweeper.js *
******************
*
* So much for Asimov's Laws. They're actually trying to kill
* you now. Not to be alarmist, but the floor is littered
* with mines. Rushing for the exit blindly may be unwise.
* I need you alive, after all.
*
* If only there was some way you could track the positions
* of the mines ...
*/
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function startLevel(map) {
for (x = 0; x < map.getWidth(); x++) {
for (y = 0; y < map.getHeight(); y++) {
map.setSquareColor(x, y, '#f00');
}
}
map.placePlayer(map.getWidth() - 5, 5);
for (var i = 0; i < 75; i++) {
var x = getRandomInt(0, map.getWidth() - 1);
var y = getRandomInt(0, map.getHeight() - 1);
if (x != 2 || y != map.getHeight() - 1) {
// don't place mine over exit!
map.placeObject(x, y, 'mine', '#f00');
#BEGIN_EDITABLE#
#END_EDITABLE#
}
}
map.placeObject(2, map.getHeight() - 1, 'exit');
#END_OF_START_LEVEL#
}
function validateLevel(map) {
map.validateAtLeastXObjects(40, 'mine');
map.validateExactlyXManyObjects(1, 'exit');
}