Guest User

Untitled

a guest
Jun 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 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 player = map.getPlayer();
  31. if (player.getColor()==="#f00")
  32. me.move('left');
  33. else
  34. if (player.getColor()==="#0f0")
  35. me.move('down');
  36. else
  37. if (player.getColor()==="#ff0")
  38. me.move('right');
  39. else
  40. if (player.getColor()==="#f0f")
  41. me.move('up');
  42.  
  43.  
  44.  
  45. }
  46. });
  47.  
  48.  
  49.  
  50. map.getPlayer().setPhoneCallback(function () {
  51. var player = map.getPlayer();
  52. if (player.getColor()==="#f00")
  53. player.setColor("#0f0");
  54. else
  55. if (player.getColor()==="#0f0")
  56. player.setColor("#ff0");
  57. else
  58. if (player.getColor()==="#ff0")
  59. player.setColor("#f0f");
  60. else
  61. if (player.getColor()==="#f0f")
  62. player.setColor("#f00");
  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.  
  103. map.defineObject('barrier', {
  104. 'symbol': '░',
  105. 'color': 'purple',
  106. 'impassable': true,
  107. 'passableFor': ['robot']
  108. });
  109.  
  110. map.placeObject(0, map.getHeight() - 1, 'exit');
  111. map.placeObject(1, 1, 'robot');
  112. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  113. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  114.  
  115. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  116. autoGeneratedMaze.create( function (x, y, mapValue) {
  117. // don't write maze over robot or barrier
  118. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  119. return 0;
  120. } else if (mapValue === 1) { //0 is empty space 1 is wall
  121. map.placeObject(x,y, 'block');
  122. } else {
  123. map.placeObject(x,y,'empty');
  124. }
  125. });
  126. }
  127.  
  128. function validateLevel(map) {
  129. map.validateExactlyXManyObjects(1, 'exit');
  130. map.validateExactlyXManyObjects(1, 'robot');
  131. map.validateAtMostXObjects(1, 'blueKey');
  132. }
  133.  
  134. function onExit(map) {
  135. if (!map.getPlayer().hasItem('blueKey')) {
  136. map.writeStatus("We need to get that key!");
  137. return false;
  138. } else {
  139. return true;
  140. }
  141. }
Add Comment
Please, Sign In to add comment