Created
January 24, 2021 03:32
-
-
Save Untrusted-Game/d26ab8c3ea8ef562491e1844893d8cde to your computer and use it in GitHub Desktop.
Solution to level 11 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
/* | |
* robot.js | |
* | |
* You'll need three keys in order to unlock the | |
* Algorithm: the red key, the green key, and the | |
* blue key. Unfortunately, all three of them are | |
* behind human-proof barriers. | |
* | |
* The plan is simple: reprogram the maintenance | |
* robots to grab the key and bring it through | |
* the barrier to us. | |
* | |
* Let's try it on the red key first. | |
*/ | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
function startLevel(map) { | |
// Hint: you can press R or 5 to "rest" and not move the | |
// player, while the robot moves around. | |
map.placePlayer(map.getWidth()-2, map.getHeight()-2); | |
var player = map.getPlayer(); | |
map.defineObject('robot', { | |
'type': 'dynamic', | |
'symbol': 'R', | |
'color': 'gray', | |
'onCollision': function (player, me) { | |
me.giveItemTo(player, 'redKey'); | |
}, | |
'behavior': function (me) { | |
// Available commands: me.move(direction) | |
// and me.canMove(direction) | |
if (me.getX()==map.getWidth() - 2) { | |
me.move('down'); | |
} | |
else { | |
me.move('right'); | |
} | |
/* | |
Yes I know the idea was probably to make it act like a Roomba | |
but really I didn't feel like coding all that. I just need the key | |
Twisted Code (sometimes called macks2008 or macks2010) was here | |
*/ | |
} | |
}); | |
map.defineObject('barrier', { | |
'symbol': '░', | |
'color': 'purple', | |
'impassable': true, | |
'passableFor': ['robot'] | |
}); | |
map.placeObject(0, map.getHeight() - 1, 'exit'); | |
map.placeObject(1, 1, 'robot'); | |
map.placeObject(map.getWidth() - 2, 8, 'redKey'); | |
map.placeObject(map.getWidth() - 2, 9, 'barrier'); | |
for (var x = 0; x < map.getWidth(); x++) { | |
map.placeObject(x, 0, 'block'); | |
if (x != map.getWidth() - 2) { | |
map.placeObject(x, 9, 'block'); | |
} | |
} | |
for (var y = 1; y < 9; y++) { | |
map.placeObject(0, y, 'block'); | |
map.placeObject(map.getWidth() - 1, y, 'block'); | |
} | |
} | |
function validateLevel(map) { | |
map.validateExactlyXManyObjects(1, 'exit'); | |
map.validateExactlyXManyObjects(1, 'robot'); | |
map.validateAtMostXObjects(1, 'redKey'); | |
} | |
function onExit(map) { | |
if (!map.getPlayer().hasItem('redKey')) { | |
map.writeStatus("We need to get that key!"); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You know, Once upon a time I remember programming something in pylab (at least, I think it was using pylab? It's been like 8 or so years since I took "introduction to computer science and programming" which also introduced Python) to estimate the area coverage of a Roomba that bounces randomly. Even though I didn't program it to bounce off walls in a clockwise pattern (I totally could've, given the "canMove" method), the thought of programming a Roomba-like simulation brings back memories