Guest User

Untitled

a guest
Apr 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 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. // move randomly
  31. var moves = map.getAdjacentEmptyCells(me.getX(), me.getY());
  32. // getAdjacentEmptyCells gives array of ((x, y), direction) pairs
  33. if(me.getX() ==map.getWidth() -2){me.move('down')}
  34. else{
  35. var queue = [];
  36. for (var i=0; i<moves.length; i++) { queue.push([moves[i]]); }
  37. var visited = {}
  38. visited [[me.getX(), me.getY()]] = true
  39.  
  40. while (queue.length >0){
  41. var path = queue.shift()
  42. var move = path [path.length-1]
  43. var x = move [0][0]
  44. var y = move [0][1]
  45. if (x == map.getWidth() -2 && y == 7){
  46. me.move(path[0][1]);
  47. break;}
  48. if (!visited[[x, y]]){
  49. visited [[x,y]] = true
  50. var newmoves = map.getAdjacentEmptyCells(x,y)
  51. for (var i = 0; i < newmoves.length; i++){
  52. queue.push(path.concat([newmoves[i]]))}
  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.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. }
  100. });
  101.  
  102. map.defineObject('barrier', {
  103. 'symbol': '░',
  104. 'color': 'purple',
  105. 'impassable': true,
  106. 'passableFor': ['robot']
  107. });
  108.  
  109. map.placeObject(0, map.getHeight() - 1, 'exit');
  110. map.placeObject(1, 1, 'robot');
  111. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  112. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  113.  
  114. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  115. autoGeneratedMaze.create( function (x, y, mapValue) {
  116. // don't write maze over robot or barrier
  117. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  118. return 0;
  119. } else if (mapValue === 1) { //0 is empty space 1 is wall
  120. map.placeObject(x,y, 'block');
  121. } else {
  122. map.placeObject(x,y,'empty');
  123. }
  124. });
  125. }
  126.  
  127. function validateLevel(map) {
  128. map.validateExactlyXManyObjects(1, 'exit');
  129. map.validateExactlyXManyObjects(1, 'robot');
  130. map.validateAtMostXObjects(1, 'blueKey');
  131. }
  132.  
  133. function onExit(map) {
  134. if (!map.getPlayer().hasItem('blueKey')) {
  135. map.writeStatus("We need to get that key!");
  136. return false;
  137. } else {
  138. return true;
  139. }
  140. }
Add Comment
Please, Sign In to add comment