Guest User

Untitled

a guest
Jan 17th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 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. var currentX = me.getX();
  32. var currentY = me.getY();
  33. var moves = map.getAdjacentEmptyCells(currentX, currentY);
  34. // remove dead ends
  35. addVisit(currentX, currentY);
  36.  
  37. moves.sort((a, b) => {
  38. let visitsA = getVisits(a[0][0], a[0][1]);
  39. let visitsB = getVisits(b[0][0], b[0][1]);
  40. return visitsA - visitsB;
  41. }
  42. );
  43.  
  44. console.log("@["+currentX+","+currentY+"]");
  45. console.log(moves);
  46.  
  47. if(amIAboveTheKey()) {
  48. console.log("I am above the key");
  49. me.finale = 4;
  50. me.move('down');
  51. } else {
  52. if(me.finale--) {
  53. me.move('down');
  54. } else {
  55. me.move(moves[0][1]);
  56. }
  57. }
  58.  
  59. if(isDeadEnd()) {
  60. //this is a dead end
  61. this.color = 'red';
  62. map.placeObject(currentX, currentY, 'block');
  63. } else if (isACorner()) {
  64. this.color = 'yellow';
  65. map.placeObject(currentX, currentY, 'block');
  66. } else {
  67. this.color = 'white';
  68. }
  69.  
  70. function amIAboveTheKey() {
  71. me.keyLocation = me.findNearest('blueKey');
  72. console.log("location of the key:");
  73. console.log(me.keyLocation);
  74. if( me.keyLocation ) {
  75. if( currentX == me.keyLocation.x &&
  76. currentY == me.keyLocation.y-1) {
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82.  
  83. function getVisits(x, y){
  84. if( Array.isArray(me.visited) &&
  85. Array.isArray(me.visited[x]) &&
  86. me.visited[x][y] ) {
  87. //console.log("Cell ["+x+","+y+"] visited "
  88. // + me.visited[x][y].times + " times");
  89. return me.visited[x][y].times;
  90. } else {
  91. //console.log("Cell ["+x+","+y+"] visited "
  92. // + 0 + " times");
  93. return 0;
  94. }
  95. }
  96.  
  97. function addVisit(currentX, currentY){
  98. if (!Array.isArray(me.visited)) {
  99. me.visited = new Array();
  100. }
  101. if (!Array.isArray(me.visited[currentX])) {
  102. me.visited[currentX] = new Array();
  103.  
  104. }
  105. if (!me.visited[currentX][currentY]) {
  106. me.visited[currentX][currentY] = {};
  107. }
  108. if(!me.visited[currentX][currentY].times){
  109. console.log(me.visited[currentX][currentY]);
  110. me.visited[currentX][currentY].times = 1;
  111. console.log(me.visited[currentX][currentY]);
  112. } else {
  113. let t = me.visited[currentX][currentY].times;
  114. me.visited[currentX][currentY].times = t + 1;
  115. }
  116. map.writeStatus("@[" + currentX + "," + currentY + "]:" +
  117. me.visited[currentX][currentY].times
  118. );
  119. }
  120.  
  121.  
  122.  
  123. function isDeadEnd(){
  124. return moves.length == 1;
  125. }
  126.  
  127. function isAPassage(){
  128. return moves.length == 2;
  129. }
  130.  
  131. function isACorner(){
  132. if (moves.length == 1) {
  133. if(me.canMove('left') && me.canMove('right')){
  134. return false;
  135. }
  136. if(me.canMove('up') && me.canMove('down')){
  137. return false;
  138. }
  139. return true;
  140. }
  141. return false;
  142. }
  143.  
  144.  
  145.  
  146. }
  147. });
  148.  
  149. map.defineObject('barrier', {
  150. 'symbol': '░',
  151. 'color': 'purple',
  152. 'impassable': true,
  153. 'passableFor': ['robot']
  154. });
  155.  
  156. map.placeObject(0, map.getHeight() - 1, 'exit');
  157. map.placeObject(1, 1, 'robot');
  158. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  159. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  160.  
  161. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  162. autoGeneratedMaze.create( function (x, y, mapValue) {
  163. // don't write maze over robot or barrier
  164. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  165. return 0;
  166. } else if (mapValue === 1) { //0 is empty space 1 is wall
  167. map.placeObject(x,y, 'block');
  168. } else {
  169. map.placeObject(x,y,'empty');
  170. }
  171. });
  172. }
  173.  
  174. function validateLevel(map) {
  175. map.validateExactlyXManyObjects(1, 'exit');
  176. map.validateExactlyXManyObjects(1, 'robot');
  177. map.validateAtMostXObjects(1, 'blueKey');
  178. }
  179.  
  180. function onExit(map) {
  181. if (!map.getPlayer().hasItem('blueKey')) {
  182. map.writeStatus("We need to get that key!");
  183. return false;
  184. } else {
  185. return true;
  186. }
  187. }
Add Comment
Please, Sign In to add comment