Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. // Rover Object Goes Here
  2.  
  3. function Rover(){
  4. this.direction = "N";
  5. this.x = 0;
  6. this.y = 0;
  7. this.travelLog = [];
  8. }
  9. // ======================
  10. var commandsList = "rffrfflfrff";
  11.  
  12. // ======================
  13. function reset(rover, grid){
  14. rover.direction = "N";
  15. rover.x = 0;
  16. rover.y = 0;
  17. rover.travelLog = [];
  18. grid = [];
  19. }
  20.  
  21. function createGrid(grid){
  22. //creamos tabla
  23. for (var i = 0; i < 10; i++){
  24. grid[i] = new Array(9);
  25. for (var j = 0; j < 10; j++){
  26. grid[i][j] =" ";
  27. }
  28. }
  29. }
  30. grid[rover.y][rover.x] = rover.direction;
  31.  
  32.  
  33. function turnLeft(rover){
  34. switch (rover.direction) {
  35. case "N":
  36. rover.direction = "W";
  37. break;
  38. case "W":
  39. rover.direction = "S";
  40. break;
  41. case "S":
  42. rover.direction = "E";
  43. break;
  44. case "E":
  45. rover.direction = "N";
  46. break;
  47. }
  48. }
  49.  
  50. function turnRight(rover){
  51. switch (rover.direction) {
  52. case "N":
  53. rover.direction = "E";
  54. break;
  55. case "W":
  56. rover.direction = "N";
  57. break;
  58. case "S":
  59. rover.direction = "W";
  60. break;
  61. case "E":
  62. rover.direction = "S";
  63. break;
  64. }
  65. }
  66.  
  67. function moveForward(rover, grid){
  68.  
  69. switch (rover.direction) {
  70.  
  71. case "N":
  72. if (rover.y === 0) {
  73. console.log("MOVEMENT NOT ALLOWED!");
  74. }
  75. else {
  76. rover.y--;
  77. }
  78. break;
  79.  
  80. case "W":
  81. if (rover.x === 0) {
  82. console.log("MOVEMENT NOT ALLOWED!");
  83. } else {
  84. rover.x--;
  85. }
  86. break;
  87.  
  88. case "S":
  89. if (rover.y === 9) {
  90. console.log("MOVEMENT NOT ALLOWED!");
  91. } else {
  92. rover.y++;
  93. }
  94. break;
  95.  
  96. case "E":
  97. if (rover.x === 9) {
  98. console.log("MOVEMENT NOT ALLOWED!");
  99. } else {
  100. rover.x++;
  101. }
  102. break;
  103. }
  104. }
  105.  
  106.  
  107.  
  108. function commands(commandsList){
  109.  
  110. var rover = new Rover();
  111. var grid = [];
  112. var commandsRegex = /[fbrl]+/i;
  113. var stringPosition = "";
  114.  
  115. reset(rover, grid);
  116. createGrid(grid);
  117. createObstacles(grid, rover);
  118. console.log(grid.join("\n"));
  119. if ((commandsList.match(commandsRegex) === null) || (commandsList !== commandsList.match(commandsRegex)[0])) {
  120. console.log("THIS IS NOT A CORRECT LIST OF COMMANDS: inputs must be 'f', 'r', or 'l'");
  121. } else {
  122. for (var i = 0; i < commandsList.length; i++) {
  123. grid[rover.y][rover.x] = " ";
  124. switch (commandsList[i]) {
  125. case "f":
  126. console.log("moveForward was called");
  127. moveForward(rover, grid);
  128. break;
  129. case "r":
  130. console.log("turnRight was called!");
  131. turnRight(rover);
  132. break;
  133. case "l":
  134. console.log("turnLeft was called!");
  135. turnLeft(rover);
  136. break;
  137. }
  138. console.log("Current position and direction: (" + rover.x + ", " + rover.y + ", " + rover.direction +")");
  139. stringPosition = "\n\tCommand " + commandsList[i] + ": (" + rover.x + ", " + rover.y + ", " + rover.direction +")";
  140. rover.travelLog.push(stringPosition);
  141. grid[rover.y][rover.x] = rover.direction;
  142. console.log(grid.join("\n"));
  143. }
  144. console.log("TRACKING:" + rover.travelLog);
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement