Guest User

Untitled

a guest
Jan 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 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.  
  31.  
  32.  
  33. let choose = (x) => {
  34.  
  35. switch (x) {
  36. case 0:
  37. return 'right';
  38. case 1:
  39. return 'down';
  40. case 2:
  41. return 'left';
  42. case 3:
  43. return 'up';
  44. default:
  45. return 'right';
  46.  
  47. }
  48. }
  49.  
  50. let findNext = () => {
  51. let i = me.lastMove - 1;
  52. if (i == -1) {
  53. i = 3;
  54. }
  55.  
  56. let dir = choose(i);
  57. while(!me.canMove(dir)){
  58.  
  59. i++;
  60. if (i == 4) {
  61. i = 0;
  62. }
  63.  
  64. dir = choose(i);
  65. }
  66.  
  67. me.lastMove = i;
  68.  
  69. return dir;
  70. }
  71.  
  72. if (me.lastMove == undefined) {
  73. me.lastMove = 0;
  74. }
  75.  
  76. if (me.lastMove == -1) {
  77. me.lastMove = 3;
  78. }
  79.  
  80. if (me.lastMove == 4) {
  81. me.lastMove = 0;
  82. }
  83.  
  84.  
  85. let dir = findNext();
  86. me.move(dir);
  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.  
  135.  
  136.  
  137. }
  138. });
  139.  
  140. map.defineObject('barrier', {
  141. 'symbol': '░',
  142. 'color': 'purple',
  143. 'impassable': true,
  144. 'passableFor': ['robot']
  145. });
  146.  
  147. map.placeObject(0, map.getHeight() - 1, 'exit');
  148. map.placeObject(1, 1, 'robot');
  149. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  150. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  151.  
  152. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  153. autoGeneratedMaze.create( function (x, y, mapValue) {
  154. // don't write maze over robot or barrier
  155. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  156. return 0;
  157. } else if (mapValue === 1) { //0 is empty space 1 is wall
  158. map.placeObject(x,y, 'block');
  159. } else {
  160. map.placeObject(x,y,'empty');
  161. }
  162. });
  163. }
  164.  
  165. function validateLevel(map) {
  166. map.validateExactlyXManyObjects(1, 'exit');
  167. map.validateExactlyXManyObjects(1, 'robot');
  168. map.validateAtMostXObjects(1, 'blueKey');
  169. }
  170.  
  171. function onExit(map) {
  172. if (!map.getPlayer().hasItem('blueKey')) {
  173. map.writeStatus("We need to get that key!");
  174. return false;
  175. } else {
  176. return true;
  177. }
  178. }
Add Comment
Please, Sign In to add comment