Skip to content

Instantly share code, notes, and snippets.

Created April 25, 2014 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/11280946 to your computer and use it in GitHub Desktop.
Save anonymous/11280946 to your computer and use it in GitHub Desktop.
Solution to level 13 in Untrusted: http://alex.nisnevich.com/untrusted/
/*
* robotMaze.js
*
* The blue key is inside a labyrinth, and extracting
* it will not be easy.
*
* It's a good thing that you're a AI expert, or
* we would have to leave empty-handed.
*/
function startLevel(map) {
// Hint: you can press R or 5 to "rest" and not move the
// player, while the robot moves around.
map.getRandomInt = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
map.placePlayer(map.getWidth()-1, map.getHeight()-1);
var player = map.getPlayer();
map.defineObject('robot', {
'type': 'dynamic',
'symbol': 'R',
'color': 'gray',
'onCollision': function (player, me) {
me.giveItemTo(player, 'blueKey');
},
'behavior': function (me) {
var table = []
for (x = 0; x < map.getWidth(); x++) {
table[x] = []
for (y = 0; y < 11; y++) {
table[x][y] = map.getObjectTypeAt(x,y) == 'empty'
}
}
table[map.getWidth()-2][8] = true; // blue key
table[map.getWidth()-2][9] = true; // door
function cost(px, py, pc, x,y) {
if (typeof table[x][y] == "number" && table[x][y] < pc) {
pc = table[x][y];
} else {
table[x][y] = pc;
}
//console.log("cost for ",x,y,": ",pc)
function valid(x,y) {
return (x != px || y != py) && table[x] && table[x][y] == true
}
function recurse(i,j) {
if (valid(i,j)) {cost(x, y, pc+1, i, j)}
}
recurse(x+1, y);
recurse(x-1, y);
recurse(x, y+1);
recurse(x, y-1);
}
cost(map.getWidth()-2, 7, 0, map.getWidth()-2, 8);
function move_if_cheap(x,y, name) {
var cur = table[me.getX()][me.getY()];
var next = table[x+me.getX()][y+me.getY()];
if (typeof next == "number" && cur > next) {
console.log("Successful move towards: ",name,next);
me.move(name);
return true;
}
console.log("Failed move towards: ",name)
return false;
}
function move_mindlessly() {
if (me.getX() == map.getWidth()-2 && me.getY() > 7) {
me.move('down');
return true;
}
return false;
}
console.log("Trying to move from ", table[me.getX()][me.getY()]);
move_mindlessly() ||
move_if_cheap(1,0, "right") ||
move_if_cheap(-1,0, "left") ||
move_if_cheap(0,1, "down") ||
move_if_cheap(0,-1, "up");
}
});
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, 'blueKey');
map.placeObject(map.getWidth() - 2, 9, 'barrier');
var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
autoGeneratedMaze.create( function (x, y, mapValue) {
// don't write maze over robot or barrier
if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
return 0;
} else if (mapValue === 1) { //0 is empty space 1 is wall
map.placeObject(x,y, 'block');
} else {
map.placeObject(x,y,'empty');
}
});
}
function validateLevel(map) {
map.validateExactlyXManyObjects(1, 'exit');
map.validateExactlyXManyObjects(1, 'robot');
map.validateAtMostXObjects(1, 'blueKey');
}
function onExit(map) {
if (!map.getPlayer().hasItem('blueKey')) {
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