Skip to content

Instantly share code, notes, and snippets.

@patio11
Forked from anonymous/untrusted-lvl13-solution.js
Last active August 29, 2015 14:25
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 patio11/81e0285dd65148f2dae9 to your computer and use it in GitHub Desktop.
Save patio11/81e0285dd65148f2dae9 to your computer and use it in GitHub Desktop.
Patio11's 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) {
/*
Our strategy is a depth-first-search from the key outwards. We color each square we hit in each wave with
a gradually decreasing color iff it isn't yet colored, stopping when our robot's square is colored. He then
simply ascends the color gradient greedily, guaranteeing that he ends up on the key.
After he has the key, he can no longer ascend the color gradient since he is at a maximum, so he moves down.
Slight annoyance: since the map doesn't have map.getSquareColor() available we have to keep memory of it
locally. After we're doing that the color abstraction doesn't necessarily make sense but it makes for a really
pretty visualization of the bot's behavior.
*/
if (typeof (me.mapSquareColors) == "undefined") {
var currentSet = map.getAdjacentEmptyCells(map.getWidth() - 2, 8);
//currentSet = new Set(currentSet);
var currentColor = 0xFFD700; // gold
map.setSquareColor(map.getWidth() - 2, 8, "#" + currentColor.toString(16));
me.mapSquareColors = new Array(map.getWidth());
for (x = 0; x < map.getWidth(); x++) {
me.mapSquareColors[x] = new Array(map.getHeight());
}
while (me.mapSquareColors[me.getX()][me.getY()] == undefined) {
currentColor -= 2;
var newSet = []
for (i = 0; i < currentSet.length; i++) {
var move = currentSet[i];
var coordinates = move[0];
var x = coordinates[0];
var y = coordinates[1];
var colorString = "#" + currentColor.toString(16);
if (me.mapSquareColors[x][y] == undefined) {
map.setSquareColor(x, y, colorString);
me.mapSquareColors[x][y] = colorString;
var newMoves = map.getAdjacentEmptyCells(x, y);
for (j = 0; j < newMoves.length; j++) {
move = newMoves[j];
newSet.push(move);
}
}
}
currentSet = newSet;
}
} else {
var moves = map.getAdjacentEmptyCells(me.getX(), me.getY())
console.log(["Turn 2: ", moves]);
var bestColor = me.mapSquareColors[me.getX()][me.getY()];
var bestMove = "down";
for (i = 0; i < moves.length; i++) {
var move = moves[i];
var x = move[0][0];
var y = move[0][1];
if (me.mapSquareColors[x][y] > bestColor) {
bestColor = me.mapSquareColors[x][y];
bestMove = move[1];
}
}
me.move(bestMove);
}
}
});
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