Guest User

Untitled

a guest
Oct 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 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 (!me.last_move) me.last_move = 'right';
  31. if (me.last_move == 'right') {
  32. if (me.canMove('up')) {
  33. me.last_move = 'up';
  34. me.move('up');
  35. } else if (me.canMove('right')) {
  36. me.last_move = 'right';
  37. me.move('right');
  38. } else if (me.canMove('down')) {
  39. me.last_move = 'down';
  40. me.move('down');
  41. } else if (me.canMove('left')) {
  42. me.last_move = 'left';
  43. me.move('left');
  44. }
  45. } else if (me.last_move == 'up') {
  46. if (me.canMove('left')) {
  47. me.last_move = 'left';
  48. me.move('left');
  49. } else if (me.canMove('up')) {
  50. me.last_move = 'up';
  51. me.move('up');
  52. } else if (me.canMove('right')) {
  53. me.last_move = 'right';
  54. me.move('right');
  55. } else if (me.canMove('down')) {
  56. me.last_move = 'down';
  57. me.move('down');
  58. }
  59. } else if (me.last_move == 'left') {
  60. if (me.canMove('down')) {
  61. me.last_move = 'down';
  62. me.move('down');
  63. } else if (me.canMove('left')) {
  64. me.last_move = 'left';
  65. me.move('left');
  66. } else if (me.canMove('up')) {
  67. me.last_move = 'up';
  68. me.move('up');
  69. } else if (me.canMove('right')) {
  70. me.last_move = 'right';
  71. me.move('right');
  72. }
  73. } else if (me.last_move == 'down') {
  74. if (me.canMove('right')) {
  75. me.last_move = 'right';
  76. me.move('right');
  77. } else if (me.canMove('down')) {
  78. me.last_move = 'down';
  79. me.move('down');
  80. } else if (me.canMove('left')) {
  81. me.last_move = 'left';
  82. me.move('left');
  83. } else if (me.canMove('up')) {
  84. me.last_move = 'up';
  85. me.move('up');
  86. }
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. }
  132. });
  133.  
  134. map.defineObject('barrier', {
  135. 'symbol': '░',
  136. 'color': 'purple',
  137. 'impassable': true,
  138. 'passableFor': ['robot']
  139. });
  140.  
  141. map.placeObject(0, map.getHeight() - 1, 'exit');
  142. map.placeObject(1, 1, 'robot');
  143. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  144. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  145.  
  146. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  147. autoGeneratedMaze.create( function (x, y, mapValue) {
  148. // don't write maze over robot or barrier
  149. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  150. return 0;
  151. } else if (mapValue === 1) { //0 is empty space 1 is wall
  152. map.placeObject(x,y, 'block');
  153. } else {
  154. map.placeObject(x,y,'empty');
  155. }
  156. });
  157. }
  158.  
  159. function validateLevel(map) {
  160. map.validateExactlyXManyObjects(1, 'exit');
  161. map.validateExactlyXManyObjects(1, 'robot');
  162. map.validateAtMostXObjects(1, 'blueKey');
  163. }
  164.  
  165. function onExit(map) {
  166. if (!map.getPlayer().hasItem('blueKey')) {
  167. map.writeStatus("We need to get that key!");
  168. return false;
  169. } else {
  170. return true;
  171. }
  172. }
Add Comment
Please, Sign In to add comment