# Solution to level 13 in Untrusted: http://alex.nisnevich.com/untrusted/

Home   »   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) {
// check if finished
if (me.delivered > 0) {
me.move('down');
me.delivered--;
return;
}
else if (me.delivered == 0) {
return;
}

// check if we have the key
me.move('down');
me.delivered = 2;
return;
}

// check if close to key
if (map.getObjectTypeAt(me.getX()+1, me.getY()) == 'blueKey') {
me.move('right');
return;
}
else if (map.getObjectTypeAt(me.getX(), me.getY()+1) == 'blueKey') {
me.move('down');
return;
}

// start visit wander
if (me.visited == undefined) {
me.visited = [];
}

var current = null;
for (var i = 0; i < me.visited.length; i++) {
var check = me.visited[i];
if (check.x == me.getX() && check.y == me.getY()) {
current = check;
}
}
if (current == null) {
current = {x: me.getX(), y: me.getY(), count: 0};
me.visited.push(current);

}
current.count++;

for (var i = 0; i < moves.length; i++) {
var move = moves[i];
for (var j = 0; j < me.visited.length; j++) {
var check = me.visited[j];
if (check.x == move[0][0] && check.y == move[0][1]) {
move.push(check.count);
break;
}
}
}

var leastVisited = null;
for (var i = 0; i < moves.length; i++) {
if (leastVisited == null
|| moves[i][2] == undefined
|| moves[i][2] < leastVisited[2]) {
leastVisited = moves[i];
}
}

me.move(leastVisited[1]);
}
});

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;
}
}
``````