Created
May 12, 2014 05:45
-
-
Save anonymous/12a6b1f82a5ea7b9d510 to your computer and use it in GitHub Desktop.
Solution to level 12 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
/* | |
* robotNav.js | |
* | |
* The green key is located in a slightly more | |
* complicated room. You'll need to get the robot | |
* past these obstacles. | |
*/ | |
function startLevel(map) { | |
// Hint: you can press R or 5 to "rest" and not move the | |
// player, while the robot moves around. | |
map.placePlayer(0, map.getHeight() - 1); | |
var player = map.getPlayer(); | |
map.defineObject('robot', { | |
'type': 'dynamic', | |
'symbol': 'R', | |
'color': 'gray', | |
'onCollision': function (player, me) { | |
me.giveItemTo(player, 'greenKey'); | |
}, | |
'behavior': function (me) { | |
var blockRight = !me.canMove("right"); | |
var blockLeft = !me.canMove("left"); | |
var blockUp = !me.canMove("up"); | |
var blockDown = !me.canMove("down"); | |
// go down the left wall | |
if (blockLeft && !blockDown) { | |
me.move("down"); | |
return; | |
} | |
// crawl right on the bottom wall | |
if (blockDown && !blockRight) { | |
me.move("right") | |
return; | |
} | |
// get trapped, move left and fill in the hole I was in | |
if (blockRight && blockUp) { | |
me.move("left"); | |
map.placeObject(me.getX()+1, me.getY(), 'block'); | |
return; | |
} | |
// after filling in that hole, I have a chance to go up | |
// but only do this early on | |
if (blockRight && !blockUp && (me.getX() < map.getWidth() - 3)) { | |
me.move("up"); | |
return; | |
} | |
// I am free | |
if (!blockRight && !blockLeft && !blockUp && !blockDown) { | |
me.move("right"); | |
return; | |
} | |
// on the far end, go down the right wall | |
if (blockRight && !blockUp && (me.getX() > map.getWidth() - 3)) { | |
me.move("down"); | |
return; | |
} | |
} | |
}); | |
map.defineObject('barrier', { | |
'symbol': '░', | |
'color': 'purple', | |
'impassable': true, | |
'passableFor': ['robot'] | |
}); | |
map.placeObject(map.getWidth() - 1, map.getHeight() - 1, 'exit'); | |
map.placeObject(1, 1, 'robot'); | |
map.placeObject(map.getWidth() - 2, 8, 'greenKey'); | |
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'); | |
} | |
for (var i = 0; i < 4; i++) { | |
map.placeObject(20 - i, i + 1, 'block'); | |
map.placeObject(35 - i, 8 - i, 'block'); | |
} | |
} | |
function validateLevel(map) { | |
map.validateExactlyXManyObjects(1, 'exit'); | |
map.validateExactlyXManyObjects(1, 'robot'); | |
map.validateAtMostXObjects(1, 'greenKey'); | |
} | |
function onExit(map) { | |
if (!map.getPlayer().hasItem('greenKey')) { | |
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