Skip to content

Instantly share code, notes, and snippets.

Created November 26, 2014 13:59
Show Gist options
  • Save anonymous/1681506d131cc33d72cf to your computer and use it in GitHub Desktop.
Save anonymous/1681506d131cc33d72cf to your computer and use it in GitHub Desktop.
Solution to level 11 in Untrusted: http://alex.nisnevich.com/untrusted/
/*
* 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) {
if(map.dirTab === undefined){
//just to show you can do it
map.placeObject(map.getWidth() - 3, 20, 'greenKey');
map.placeObject(map.getWidth() - 3, 21, 'blueKey');
map.placeObject(map.getWidth() - 3, 22, 'yellowKey');
// actual code
map.dirTab= ['up','right','down','left']
map.stick = (function(){
for(var i=0;i<map.dirTab.length;i++){
if(!me.canMove(map.dirTab[i])){
return i;
}
}
})();
}
var cw = (map.stick+1)%map.dirTab.length;
var ccw = (map.stick+map.dirTab.length-1)%map.dirTab.length;
if(me.canMove(map.dirTab[map.stick])){
me.move(map.dirTab[map.stick]);
map.stick=ccw;
return;
}
if(me.canMove(map.dirTab[cw])){
me.move(map.dirTab[cw]);
return;
}
map.stick=cw;
/**
** old way of doing things :3
map.go=!map.go;
me.move(map.go?'right':'down');
*/
}
});
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