Skip to content

Instantly share code, notes, and snippets.

Created April 9, 2014 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/81d115d07d2924f03429 to your computer and use it in GitHub Desktop.
Save anonymous/81d115d07d2924f03429 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) {
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) {
if (me.heat == undefined)
{
me.heat = [];
for (var i = 0; i < map.getWidth(); ++i)
{
me.heat[i] = new Array(map.getHeight());
for (var j = 0; j < map.getHeight(); ++j)
{
me.heat[i][j] = -255;
}
}
me.heat[map.getWidth() - 2][8] = 0;
me.heat[map.getWidth() - 2][9] = -1;
me.heat[map.getWidth() - 2][10] = -2;
var stak = [[map.getWidth() - 2, 8]];
do
{
var coords = stak.pop();
var newheat = me.heat[coords[0]][coords[1]] + 1;
var moves = map.getAdjacentEmptyCells(coords[0], coords[1]);
for (var i = 0; i < moves.length; ++i) {
var move = moves[i];
if (me.heat[move[0][0]][move[0][1]] == -255)
{
me.heat[move[0][0]][move[0][1]] = newheat;
stak.push([move[0][0], move[0][1]]);
var color = newheat.toString(16);
if (color.length == 1)
{
color += '00';
}
else
{
color += '0';
}
map.setSquareColor(move[0][0], move[0][1], '#' +
color);
}
}
} while (stak.length != 0);
}
else
{
if (me.getX() == map.getWidth() - 2 && me.getY() > 6)
{
me.move('down');
}
else
{
var minheat = 255;
var whichmove = null;
var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
moves.forEach(function(move) {
if (me.heat[move[0][0]][move[0][1]] < minheat)
{
minheat = me.heat[move[0][0]][move[0][1]];
whichmove = move;
}
});
me.move(whichmove[1]);
}
}
// move randomly
//var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
// getAdjacentEmptyCells gives array of ((x, y), direction) pairs
//me.move(moves[map.getRandomInt(0, moves.length - 1)][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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment