alguien me podria decir como hago para que despues de cada movimiento, tenga un travelLog con las coordenadas por las que he pasado, y guardado en mi travelLog array de mi objeto? Obviamente hago rover.travelLog y me da todo el tiempo la coordenada actual, varios arrays pero con las mismas, entiendo porque pasa esto pero no caigo en como guardar las anteriores.
//UPDATE INFO
$(document).ready(function() {
setTimeout(messages, 1000);
});
function messages() {
let mensajeDireccion = document.getElementById('direction').textContent = 'rover is facing ' + rover.direction;
let mensajePosicion = document.getElementById('direction2').textContent = rover.position[0] + ',' + rover.position[1];
}
//ROVER GRID
let grid = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 'o', 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 'o', 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[0, 1, 'o', 3, 4, 5, 6, 7, 8, 9],
[0, 1, 2, 3, 4, 5, 'o', 7, 8, 9],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
];
//ROVER OBJECT
let rover = {
direction: 'N',
position: [0, 0],
travelLog: []
};
//LOGIC DECISSIONS
document.getElementById("decision").onkeyup = function(e) {
let texto = e.target.value;
let textoSanedo = texto.replace(" ", "").toUpperCase();
e.target.value = textoSanedo;
let commands;
for (var i = 0; i < textoSanedo.length; i++) {
commands = textoSanedo[i];
}
switch (commands) {
case 'L':
forward(rover);
break;
case 'R':
turnRight(rover);
break;
case 'F':
moveForward(rover);
break;
case 'B':
moveBackwards(rover);
break;
}
// console.log(rover.position);
// console.log(rover.travelLog);
// for (var j = 0; j < rover.position.length; j++) {
// // rover.travelLog.push(rover.position[i]);
// console.log(rover.position[i]);
// }
if (rover.position[0] < 0 || rover.position[0] > 9 || rover.position[1] < 0 || rover.position[1] > 9) {
alert('out');
}
return;
};
//TURN LEFT
function turnLeft(rover) {
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;
}
messages();
}
//TURN RIGHT
function turnRight(rover) {
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;
}
messages();
}
//MOVE FORWARD
function moveForward(rover) {
switch (rover.direction) {
case 'N':
rover.position[0]--;
break;
case 'W':
rover.position[1]--;
break;
case 'S':
rover.position[0]++;
break;
case 'E':
rover.position[1]++;
break;
}
rover.travelLog.push(rover.position);
messages();
}
//MOVE BACKWARDS
function moveBackwards(rover) {
switch (rover.direction) {
case 'N':
rover.position[0]++;
break;
case 'W':
rover.position[1]++;
break;
case 'S':
rover.position[0]--;
break;
case 'E':
rover.position[1]--;
break;
}
rover.travelLog.push(rover.position);
messages();
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>rover</title>
</head>
<body>
<input id="decision" type="text" name="" value="" placeholder="l: Left, r: Right, f: Forward, b: Backwards">
<p id="direction"></p>
<p id="direction2"></p>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="js/app.js" charset="utf-8"></script>
</body>
</html>