Guest User

Untitled

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