Created
          March 26, 2019 01:29 
        
      - 
      
- 
        Save Untrusted-Game/4a4d392f91a96f9ce39fdf8763fe04e0 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) { | |
| var targetX = map.getWidth() - 2 | |
| var targetY = 8 | |
| var m = [] | |
| for (var i = 0; i < map.getHeight(); i++) { | |
| m[i] = [] | |
| for (var j = 0; j < map.getWidth(); j++) { | |
| m[i][j] = false | |
| } | |
| } | |
| function assignScores(x, y, score) { | |
| var moves = map.getAdjacentEmptyCells(x, y) | |
| // console.log(moves) | |
| for (var move of moves) { | |
| // console.log(move) | |
| var setX = x | |
| var setY = y | |
| switch (move[1]) { | |
| case "up": | |
| setY = y - 1 | |
| break | |
| case "left": | |
| setX = x - 1 | |
| break | |
| case "right": | |
| setX = x + 1 | |
| break | |
| case "down": | |
| setY = y + 1 | |
| break | |
| } | |
| if (m[setY][setX] === false || score + 1 < m[setY][setX]) { | |
| m[setY][setX] = score + 1 | |
| assignScores(setX, setY, score + 1) | |
| } | |
| } | |
| } | |
| m[targetY][targetX] = 0 | |
| assignScores(targetX, targetY, 0) | |
| function getDirection(x, y) { | |
| var min = false | |
| var dir = false | |
| if (x - 1 >= 0 && m[y][x - 1] !== false) { | |
| if (min === false || m[y][x - 1] < min) { | |
| min = m[y][x - 1] | |
| dir = "left" | |
| } | |
| } | |
| if (x + 1 < m[0].length && m[y][x + 1] !== false) { | |
| if (min === false || m[y][x + 1] < min) { | |
| min = m[y][x + 1] | |
| dir = "right" | |
| } | |
| } | |
| if (y - 1 >= 0 && m[y - 1][x] !== false) { | |
| if (min === false || m[y - 1][x] < min) { | |
| min = m[y - 1][x] | |
| dir = "up" | |
| } | |
| } | |
| if (y + 1 < m.length && m[y + 1][x] !== false) { | |
| if (min === false || m[y + 1][x] < min) { | |
| min = m[y + 1][x] | |
| dir = "down" | |
| } | |
| } | |
| console.log(dir, min) | |
| return dir | |
| } | |
| if (me.getX() == targetX && me.getY() == targetY) { | |
| me.move("down") | |
| return | |
| } | |
| if (me.getY() > targetY) { | |
| me.move("down") | |
| return | |
| } | |
| me.move(getDirection(me.getX(), me.getY())) | |
| } | |
| }); | |
| 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