2017-01-20 59 views
0

我很難找出如何存儲以前的myRover.position值。我需要訪問它以便在接近障礙物時進行備份。如何訪問舊值 - JavaScript

var myRover = { 
position: [0, 0], 
direction: 'N', 
obstacleX: [0], 
obstacleY: [2], 
marsGrid: [ 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 
] 
}; 

//prevent rover from falling off of planet Mars! 
//my best attempt at wrapping a 2d array :-(
function preventSuddenDeath() { 
if (myRover.position[0] > 9) { 
myRover.position[0] = 0; 
} 
if (myRover.position[0] < 0) { 
myRover.position[0] = 9; 
} 
if (myRover.position[1] > 9) { 
myRover.position[1] = 0; 
} 
if (myRover.position[1] < 0) { 
myRover.position[1] = 9; 
} 
} 

這是我操縱障礙功能。

function maneuverObstacle() { 
if (myRover.position[0] == myRover.obstacleX && myRover.position[1] == myRover.obstacleY) { 
console.log("Crater ahead at " + myRover.position + ". Please change direction!"); 
return myRover.position(); 
} 
} 

我已經嘗試設置屬性oldValue等於CurrentValue的這樣和我會假設,它只是重寫屬性oldValue當前的一個。

function manueverObstacle() { 
var oldPosition = myRover.position; 
if (myRover.position[0] == myRover.obstacleX && myRover.position[1] == myRover.obstacleY) { 
console.log("Display value of myRover.position: " + myRover.position); 
console.log("Display value of oldPosition: " + oldPosition); 
return myRover.position(); 
} 
---->Rover is at 0,0 and heading N 
//here I've given it the commands to turn right and go forward twice in order to test it 
---->0,0 
---->0,1 
//breaks at 0,2 due to obstacle 
----> Display value of myRover.position: 0,2 
----> Display value of oldPosition: 0,2 

其餘的如果有幫助。

function move_forward() { 
if (myRover.direction == 'N') { 
myRover.position[0] -= 1; 
} else if (myRover.direction == 'E') { 
myRover.position[1] += 1; 
} else if (myRover.direction == 'S') { 
myRover.position[0] += 1; 
} else if (myRover.direction == 'W') { 
myRover.position[1] -= 1; 
} preventSuddenDeath(); 
} 

function move_backward() { 
if (myRover.direction == 'N') { 
myRover.position[0] += 1; 
} else if (myRover.direction == 'E') { 
myRover.position[1] -= 1; 
} else if (myRover.direction == 'S') { 
myRover.position[0] -= 1; 
} else if (myRover.direction == 'W') { 
myRover.position[1] += 1; 
} preventSuddenDeath(); 
} 

function turn_left() { 
if (myRover.direction == 'N') { 
myRover.direction = 'W'; 
} else if (myRover.direction == 'E') { 
myRover.direction = 'N'; 
} else if (myRover.direction == 'S') { 
myRover.direction = 'E'; 
} else if (myRover.direction == 'W') { 
myRover.direction = 'S'; 
} 
} 


function turn_right() { 
if (myRover.direction == 'N') { 
myRover.direction = 'E'; 
} else if (myRover.direction == 'E') { 
myRover.direction = 'S'; 
} else if (myRover.direction == 'S') { 
myRover.direction = 'W'; 
} else if (myRover.direction == 'W') { 
myRover.direction = 'N'; 
} 
} 

function letsGoMrRover() { 

//take user input from html prompt and convert the string to an array 
var commandString = document.getElementById("command").value; 
var output = document.getElementById("demo"); 
var csLength = commandString.length; 
var commandArray = []; 

//console.log the starting position 
output.innerHTML = "Rover is at " + myRover.position + " and heading " + myRover.direction + "<br />"; 

for (var i=0; i < csLength; i++) { 
commandArray[i] = commandString[i]; 

switch (commandArray[i]) { 
    case 'f': 
    move_forward(); 
    break; 
    case 'b': 
    move_backward(); 
    break; 
    case 'l': 
    turn_left(); 
    break; 
    case 'r': 
    turn_right(); 
    break; 
    } 
maneuverObstacle(); 
console.log(myRover.position); 
} 
} 
+0

只需創建一個名爲'prev'的變量或其他東西。 –

+2

移動漫遊車之前,我會檢查障礙物。 make checkObstacle是一個以機器人x,y爲參數的函數,如果它可以移動,則返回該函數,這樣在實際更改機器人x之前可以傳遞x + 1,y到x + 1 – softwarenewbie7331

+0

爲什麼障礙物和mars網格部分流浪者對象的?這些看起來像三個不同的東西。 –

回答

0

一種方法是製作一個newPosition變量來計算新的位置。然後在更新流動站的位置之前檢查這個新位置是否有障礙物。

或者,您可以將prevPosition添加到myRover以存儲以前的位置。

+0

@ KH2017請編輯你的問題,以顯示你已經嘗試過。還要確保顯示結果並解釋它與你想要的不同之處。 –

+0

@ KH2017重讀我以前的評論。 –