0

I'm writing this code related to a rover moving within a grid.

In the grid there are obstacles and another rover. If rover goes against an obstacle I've already written a condition in the moveForward(rover, rover2) (and moveBackward)functions to make it go back to previous position.

I have an issue in case of collision between the 2 rovers. Here is the code:

  let rover= {
  direction: "N",
  x: 0,
  y: 0,
  travelLog: ["x=0, y=0"],
};

let rover2= {
  direction: "N",
  x: 4,
  y: 0,
  travelLog: ["x=4, y=0"],
};

let grid = [
  [null,null,null,null,null,null,null,null,null,null,],
  [null,null,"obs",null,null,null,null,null,null,null,],
  [null,null,null,null,null,null,null,null,null,null,],
  [null,null,null,null,null,null,null,null,null,null,],
  [null,null,null,null,null,null,null,null,null,null,],
  [null,null,null,null,null,"obs",null,null,null,null,],
  [null,null,null,null,null,null,null,null,null,null,],
  [null,null,null,null,null,null,null,null,null,null,],
  [null,null,null,null,null,null,null,null,null,"obs",],
  [null,null,null,null,null,null,null,null,null,null,],];

 var tempX = rover.x;
  var tempY = rover.y;

let obsList= [];  
function obstacle (grid) {
for (i=0; i< grid.length; i++) {
    for(let j = 0; j < grid[i].length; j++) {
      if(grid[i][j] !== null) {
        obsList.push("x=" + i + " y=" + j);
      }
    }
  }
 //console.log(obsList);
}
obstacle (grid);


function turnLeft(rover,rover2) {
    switch (rover.direction) {
    case "N":
    rover.direction = "W";
      break;
      case "W":
       rover.direction = "S";
      break;
      case "S":
      rover.direction = "E";
      break;
       case "E":
        rover.direction = "N";
      break;      
  }
   console.log(`turnLeft was called!Rover direction is:${rover.direction}`); 
   }

 function turnRight(rover, rover2){
  switch (rover.direction) {
    case "N":
     rover.direction = "E";
      break;
      case "E":
       rover.direction = "S";
      break;
    case "S":
     rover.direction = "W";
      break;
       case "W":
        rover.direction = "N";
      break;      
  }
  console.log(`turnRight was called!Rover direction is:${rover.direction}`);
   }

function moveForward(rover, rover2) {
  console.log("move Forward was called");
  var tempX = rover.x;
  var tempY = rover.y;
  
  switch(rover.direction) {
   case "N":
  if (rover.y> 0) {
    rover.y--;
  } else {
console.log("Can't get out of the grid");
  }
  break;
   case  "W":
  if (rover.x> 0) {
    rover.x--;
  } else {
console.log("Can't get out of the grid");
  }
  break;
  case "E":
  if (rover.x <9) {
    rover.x++;
    } else {
console.log("Can't get out of the grid");
    }  
 break;
case "S":
  if (rover.y < 9) {
    rover.y++;
  } else {
console.log("Can't get out of the grid");
  }
break;
}


if(collide(rover, rover2)) {
  rover.x = tempX;
  rover.y = tempY; 
  } else {
    rover.travelLog.push("x=" +rover.x + " y=" +rover.y);
  }

for(a=0; a<rover.travelLog.length; a++) {
  for(o=0; o<obsList.length; o++) {
    if(rover.travelLog[a] == obsList[o]) {
      console.log("Obstacle!");
      rover.travelLog.splice(rover.travelLog.length-1,1);
      if (rover.x == obsList.x) {
        rover.x++;
      } else {
        rover.y++;
    }
  }
}
}
}

 function moveBackward(rover, rover2) { 
  console.log("move Backward was called");
   switch(rover.direction) {
    case "N":
      if (rover.y < 9) {
        rover.y ++;
      } else {
        console.log("You can't get out of the grid");
      }
      break;
    case "E":
      if (rover.x > 0) {
        rover.x --;
      } else {
        console.log("You can't get out of the grid");
      }
      break;
    case "S":
      if (rover.y > 0) {
        rover.y --;
      } else {
        console.log("You can't get out of the grid");
      }
      break;
    case "W":
      if (rover.x < 9) {
        rover.x ++;
      } else {
        console.log("You can't get out of the grid");
      break;
      }
   }
    rover.travelLog.push("x=" +rover.x + " y=" +rover.y);



for(a=0; a<rover.travelLog.length; a++) {
  for(b=0; b<obsList.length; b++) {
    if(rover.travelLog[a] == obsList[b]) {
      console.log("Obstacle!");
      rover.travelLog.splice(rover.travelLog.length-1,1);
      if (rover.x == obsList.x) {
        rover.x--;
      } else {
        rover.y--;
    }
  }
}
} 
}
function command(rover, rover2, orders){

  for (let i = 0; i < orders.length; i++){
    let order = orders[i];
    switch(order){
      case "l": // left
        turnLeft(rover);
        break;
      case "r": // right
        turnRight(rover);
        break;  
      case "f": // forward
        moveForward(rover, rover2);
        break; 
      case "b": // backward
        moveBackward(rover, rover2);
        break;
      default:
      console.log("Incorrect input! Try with: l,r,f or b!!")
    }
  } 
}
command(rover, rover2, "rf");
console.log(rover.travelLog);

command(rover2, rover, "lffflff");
console.log(rover2.travelLog);


function collide(rover, rover2) {
if (rover.x == rover2.x && rover.y == rover2.y){
console.log("collision detected!");
rover.travelLog.splice(rover.travelLog.length-1,1);
}
}
//collide(rover, rover2);

The issue is that if I apply:

rover.travelLog.splice(rover.travelLog.length-1,1);

to the:

function collide(rover, rover2) {
if (rover.x == rover2.x && rover.y == rover2.y){
console.log("collision detected!");
rover.travelLog.splice(rover.travelLog.length-1,1);
}
}

it actually cuts the second last position and not the last position of the rover. I believe that this happens because I consider the temporary:

 var tempX = rover.x;
  var tempY = rover.y;

when I call the function collide(rover, rover2) in moveForward(rover, rover2).

Do you have any suggestions/workarounds in order to cut just the last position of rover.travelLog, which are the coordinates of where the rover collides with the other one? Thank you for any help!!

Paola
  • 1
  • 1
  • Does this answer your question? [Remove last item from array](https://stackoverflow.com/questions/19544452/remove-last-item-from-array) – PM 77-1 Jan 24 '20 at 18:34
  • Well if I put this ``` rover.travelLog.splice(rover.travelLog.length-2,1); ``` it should delete the second to last element of the travelLog, but it actually does not work – Paola Jan 25 '20 at 12:03

0 Answers0