Guest User

Untitled

a guest
Aug 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. /*
  2. * robotMaze.js
  3. *
  4. * The blue key is inside a labyrinth, and extracting
  5. * it will not be easy.
  6. *
  7. * It's a good thing that you're a AI expert, or
  8. * we would have to leave empty-handed.
  9. */
  10.  
  11. function startLevel(map) {
  12. // Hint: you can press R or 5 to "rest" and not move the
  13. // player, while the robot moves around.
  14.  
  15. map.getRandomInt = function(min, max) {
  16. return Math.floor(Math.random() * (max - min + 1)) + min;
  17. }
  18.  
  19. map.placePlayer(map.getWidth()-1, map.getHeight()-1);
  20. var player = map.getPlayer();
  21.  
  22. map.defineObject('robot', {
  23. 'type': 'dynamic',
  24. 'symbol': 'R',
  25. 'color': 'gray',
  26. 'onCollision': function (player, me) {
  27. me.giveItemTo(player, 'blueKey');
  28. },
  29. 'behavior': function (me) {
  30. if(player.atLocation(map.getWidth() - 3, map.getHeight() - 2))
  31. me.move('left');
  32. if(player.atLocation(map.getWidth() - 2, map.getHeight() - 3))
  33. me.move('up');
  34. if(player.atLocation(map.getWidth() - 1, map.getHeight() - 2))
  35. me.move('right');
  36. if(player.atLocation(map.getWidth() - 2, map.getHeight() - 1))
  37. me.move('down');
  38.  
  39. }
  40. });
  41.  
  42. map.defineObject('barrier', {
  43. 'symbol': '░',
  44. 'color': 'purple',
  45. 'impassable': true,
  46. 'passableFor': ['robot']
  47. });
  48.  
  49. map.placeObject(0, map.getHeight() - 1, 'exit');
  50. map.placeObject(1, 1, 'robot');
  51. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  52. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  53.  
  54. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  55. autoGeneratedMaze.create( function (x, y, mapValue) {
  56. // don't write maze over robot or barrier
  57. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  58. return 0;
  59. } else if (mapValue === 1) { //0 is empty space 1 is wall
  60. map.placeObject(x,y, 'block');
  61. } else {
  62. map.placeObject(x,y,'empty');
  63. }
  64. });
  65. }
  66.  
  67. function validateLevel(map) {
  68. map.validateExactlyXManyObjects(1, 'exit');
  69. map.validateExactlyXManyObjects(1, 'robot');
  70. map.validateAtMostXObjects(1, 'blueKey');
  71. }
  72.  
  73. function onExit(map) {
  74. if (!map.getPlayer().hasItem('blueKey')) {
  75. map.writeStatus("We need to get that key!");
  76. return false;
  77. } else {
  78. return true;
  79. }
  80. }
Add Comment
Please, Sign In to add comment