Skip to content

Instantly share code, notes, and snippets.

@seanmcelroy
Created April 8, 2014 02:14
Show Gist options
  • Save seanmcelroy/10083269 to your computer and use it in GitHub Desktop.
Save seanmcelroy/10083269 to your computer and use it in GitHub Desktop.
untrusted-lvl13-solution.js
/*
* 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 (map.getPlayer().hasItem('blueKey'))
me.move('down');
else if ((me.dir === undefined || me.dir === 'u'))
{
if (me.canMove('left')) {
me.dir = 'l';
me.move('left');
}
else if (me.canMove('up'))
{
me.dir = 'u';
me.move('up');
}
else if (me.canMove('right')) {
me.dir = 'r';
me.move('right');
}
else {
me.dir = 'd';
me.move('down');
}
}
else if ((me.dir === undefined || me.dir === 'l'))
{
if (me.canMove('down')) {
me.dir = 'd';
me.move('down');
}
else if (me.canMove('left')) {
me.dir = 'l';
me.move('left');
}
else if (me.canMove('up')) {
me.dir = 'u';
me.move('up');
}
else if (me.canMove('right')) {
me.dir = 'r';
me.move('right');
}
}
else if ((me.dir === undefined || me.dir === 'd'))
{
if (me.canMove('right')) {
me.dir = 'r';
me.move('right');
}
else if (me.canMove('down')) {
me.dir = 'd';
me.move('down');
}
else if (me.canMove('left')) {
me.dir = 'l';
me.move('left');
}
else if (me.canMove('up')) {
me.dir = 'u';
me.move('up');
}
}
else if ((me.dir === undefined || me.dir === 'r'))
{
if (me.canMove('up'))
{
me.dir = 'u';
me.move('up');
}
else if (me.canMove('right')) {
me.dir = 'r';
me.move('right');
}
else if (me.canMove('down')) {
me.dir = 'd';
me.move('down');
}
else if (me.canMove('left')) {
me.dir = 'l';
me.move('left');
}
}
}
});
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