Guest User

Untitled

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