Created
January 22, 2021 07:35
-
-
Save Untrusted-Game/47963ae09944155dc730d66794bacc00 to your computer and use it in GitHub Desktop.
Solution to level 5 in Untrusted: http://alex.nisnevich.com/untrusted/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************** | |
* 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) | |
&& (x != map.getWidth() - 5 || y != 5)) { | |
// don't place mine over exit or player! | |
map.placeObject(x, y, 'mine'); | |
map.setSquareColor(x, y, ''); | |
/* | |
This is apparently one of the intended solutions. | |
I'm not feeling inventive enough at 2:30 AM to try to outdo it | |
Twisted Code (sometimes called macks2008 or macks2010) was here | |
(but gave up when printing the positions to a text buffer failed) | |
*/ | |
} | |
} | |
map.placeObject(2, map.getHeight() - 1, 'exit'); | |
} | |
function validateLevel(map) { | |
map.validateAtLeastXObjects(40, 'mine'); | |
map.validateExactlyXManyObjects(1, 'exit'); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm done. it's now 2:40 AM. Goodnight coding world