Created
September 30, 2017 15:57
-
-
Save anonymous/ba3da1982d08141cda3aca591afee3be to your computer and use it in GitHub Desktop.
Solution to level 13 in Untrusted: http://alex.nisnevich.com/untrusted/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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) { | |
//Afficher les coordonnées du Robot | |
map.writeStatus("Robot X : "+me.getX()+" Robot Y : "+me.getY()); | |
//previous coordonates | |
var previousX = me.getX(); | |
var previousY = me.getY(); | |
//Si on jouxte une clef, alors on va sur la | |
//case contenant le clef ! | |
//N | |
if(map.getObjectTypeAt(previousX, previousY-1)=='blueKey'){ | |
//Alors on bouche dans la direction de la clef | |
me.move('up'); | |
} | |
//E | |
if(map.getObjectTypeAt(previousX+1, previousY)=='blueKey'){ | |
//Alors on bouche dans la direction de la clef | |
me.move('right'); | |
} | |
//S | |
if(map.getObjectTypeAt(previousX, previousY+1)=='blueKey'){ | |
//Alors on bouche dans la direction de la clef | |
me.move('down'); | |
} | |
//W | |
if(map.getObjectTypeAt(previousX-1, previousY)=='blueKey'){ | |
//Alors on bouche dans la direction de la clef | |
me.move('left'); | |
} | |
//Pareil pour le portail : barrier (qui est toujours au Sud) | |
if(map.getObjectTypeAt(previousX, previousY+1)=='barrier'){ | |
//Alors on bouche dans la direction de la clef | |
me.move('down'); | |
//Et on rebouche l'entrée du maze | |
map.placeObject(previousX, previousY, 'block'); | |
} | |
// 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]); | |
//next coordonates | |
var nextX = me.getX(); | |
var nextY = me.getY(); | |
//Si on vient de passer par une porte | |
//Alors on la bouche | |
if((map.getObjectTypeAt(previousX-1, previousY)=='block' | |
&& map.getObjectTypeAt(previousX+1, previousY)=='block') | |
| (map.getObjectTypeAt(previousX, previousY-1)=='block' | |
&& map.getObjectTypeAt(previousX, previousY+1)=='block')){ | |
//Alors on bouche la porte avec un bloc | |
//map.placeObject(previousX, previousY, 'block'); | |
} | |
//Pour accélérer, on va boucher aussi les coins | |
//Coin S-E | |
if(map.getObjectTypeAt(previousX, previousY+1)=='block' | |
&& map.getObjectTypeAt(previousX+1, previousY)=='block'){ | |
//Alors on bouche la porte avec un bloc | |
map.placeObject(previousX, previousY, 'block'); | |
} | |
//Coin S-W | |
if(map.getObjectTypeAt(previousX, previousY+1)=='block' | |
&& map.getObjectTypeAt(previousX-1, previousY)=='block'){ | |
//Alors on bouche la porte avec un bloc | |
map.placeObject(previousX, previousY, 'block'); | |
} | |
//Coin N-E | |
if(map.getObjectTypeAt(previousX, previousY-1)=='block' | |
&& map.getObjectTypeAt(previousX+1, previousY)=='block'){ | |
//Alors on bouche la porte avec un bloc | |
map.placeObject(previousX, previousY, 'block'); | |
} | |
//Coin N-W | |
if(map.getObjectTypeAt(previousX, previousY-1)=='block' | |
&& map.getObjectTypeAt(previousX-1, previousY)=='block'){ | |
//Alors on bouche la porte avec un bloc | |
map.placeObject(previousX, previousY, 'block'); | |
} | |
} | |
}); | |
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