Advertisement
Guest User

Untitled

a guest
May 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 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. if (me.dir == undefined) {
  32. me.dir = "right";
  33. }
  34. var turnRight = {
  35. "right":"down",
  36. "down":"left",
  37. "left":"up",
  38. "up":"right"
  39. };
  40. var turnLeft = {
  41. "right":"up",
  42. "down":"right",
  43. "left":"down",
  44. "up":"left"
  45. }
  46. var moved = false;
  47. while(!moved) {
  48. if (me.canMove(turnRight[me.dir])) {
  49. me.move(turnRight[me.dir]);
  50. me.dir = turnRight[me.dir];
  51. moved = true;
  52. } else if (me.canMove(me.dir)) {
  53. me.move(me.dir);
  54. moved = true;
  55. } else {
  56. me.dir = turnLeft[me.dir];
  57. }
  58. }
  59.  
  60. }
  61. });
  62.  
  63. map.defineObject('barrier', {
  64. 'symbol': '░',
  65. 'color': 'purple',
  66. 'impassable': true,
  67. 'passableFor': ['robot']
  68. });
  69.  
  70. map.placeObject(0, map.getHeight() - 1, 'exit');
  71. map.placeObject(1, 1, 'robot');
  72. map.placeObject(map.getWidth() - 2, 8, 'blueKey');
  73. map.placeObject(map.getWidth() - 2, 9, 'barrier');
  74.  
  75. var autoGeneratedMaze = new ROT.Map.DividedMaze(map.getWidth(), 10);
  76. autoGeneratedMaze.create( function (x, y, mapValue) {
  77. // don't write maze over robot or barrier
  78. if ((x == 1 && y == 1) || (x == map.getWidth() - 2 && y >= 8)) {
  79. return 0;
  80. } else if (mapValue === 1) { //0 is empty space 1 is wall
  81. map.placeObject(x,y, 'block');
  82. } else {
  83. map.placeObject(x,y,'empty');
  84. }
  85. });
  86. }
  87.  
  88. function validateLevel(map) {
  89. map.validateExactlyXManyObjects(1, 'exit');
  90. map.validateExactlyXManyObjects(1, 'robot');
  91. map.validateAtMostXObjects(1, 'blueKey');
  92. }
  93.  
  94. function onExit(map) {
  95. if (!map.getPlayer().hasItem('blueKey')) {
  96. map.writeStatus("We need to get that key!");
  97. return false;
  98. } else {
  99. return true;
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement