Skip to content

Instantly share code, notes, and snippets.

Created April 29, 2014 10:38
Show Gist options
  • Save anonymous/11396499 to your computer and use it in GitHub Desktop.
Save anonymous/11396499 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 d = ['right', 'down', 'left', 'up'],
x = me.getX(), y = me.getY(),
_ = [], // already traversed this cell
tx = map.getWidth() - 2, // target x
ty = 7; // target y
hasTraversed = function(x, y) {
for (var j = 0; j < _.length; j++)
if (_[j][0] == x && _[j][1] == y)
return true;
return false;
},
discover = function(x, y, c) {
var s = [],
e = map.getAdjacentEmptyCells(x, y);
_.push([x, y]);
// find possible steps
for (var i = 0; i < e.length; i++) {
if (e[i][0][0] == tx && e[i][0][1] == ty) {
return [{ f: 1, d: e[i][1] }];
} else {
if (!hasTraversed(e[i][0][0], e[i][0][1])) {
s.push({
x: e[i][0][0],
y: e[i][0][1],
d: e[i][1],
f: 0
});
}
}
}
// now discover what happens at the next step
for (var i = 0; i < s.length; i++) {
var u = discover(s[i].x, s[i].y, c+1);
for(var j = 0; j < u.length; j++) {
if (u[j].f) {
s[i].f = 1;
return [s[i]];
} else {
}
}
}
return s;
};
if (me.getX() == tx && me.getY() > ty - 2) {
me.move('down');
} else {
next = discover(x, y, 0);
me.move(next[0].d);
}
}
});
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