Guest User

Untitled

a guest
Nov 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 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. var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
  31.  
  32. var x_goal = map.getWidth() - 2; var y_goal = 7;
  33. if (me.getX() == x_goal) {
  34. me.move('down');
  35. } else {
  36. var queue = [];
  37. for (var i=0; i<moves.length; i++) { queue.push([moves[i]]); }
  38. var visited = {};
  39. visited[[me.getX(), me.getY()]] = true;
  40. while (queue.length > 0) {
  41. var path = queue.shift();
  42. var move = path[path.length - 1]
  43. var x = move[0][0]; var y = move[0][1];
  44. if (x == x_goal && y == y_goal) {
  45. me.move(path[0][1]); break;
  46. }
  47. if (!visited[[x, y]]) {
  48. visited[[x, y]] = true;
  49. new_moves = map.getAdjacentEmptyCells(x, y)
  50. for (var i=0; i<new_moves.length; i++) {
  51. queue.push(path.concat([new_moves[i]]));
  52. }
  53. }
  54. }
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. }
  90. });
  91.  
  92. map.defineObject('barrier', {
  93. 'symbol': '░',
  94. 'color': 'purple',
  95. 'impassable': true,
  96. 'passableFor': ['robot']
  97. });
  98.  
  99. map.placeObject(0, map.getHeight() - 1, 'exit');
  100. map.placeObject(1, 1, 'robot');
  101. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  102. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  103.  
  104. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  105. autoGeneratedMaze.create( function (x, y, mapValue) {
  106. // don't write maze over robot or barrier
  107. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  108. return 0;
  109. } else if (mapValue === 1) { //0 is empty space 1 is wall
  110. map.placeObject(x,y, 'block');
  111. } else {
  112. map.placeObject(x,y,'empty');
  113. }
  114. });
  115. }
  116.  
  117. function validateLevel(map) {
  118. map.validateExactlyXManyObjects(1, 'exit');
  119. map.validateExactlyXManyObjects(1, 'robot');
  120. map.validateAtMostXObjects(1, 'blueKey');
  121. }
  122.  
  123. function onExit(map) {
  124. if (!map.getPlayer().hasItem('blueKey')) {
  125. map.writeStatus("We need to get that key!");
  126. return false;
  127. } else {
  128. return true;
  129. }
  130. }
Add Comment
Please, Sign In to add comment