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;
}
}
これは私の障害物を操作する機能です。私は、このような、私は引き受けるなどとしてcurrentValueはに等しいOLDVALUEを設定しようとしました
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();
}
}
、それだけで、現在の1に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);
}
}
'prev'という名前の変数を作るだけです。 –
私はローバーを動かす前に障害物をチェックします。 checkObstacleをロボットのx、yを引数とする関数にし、実際にロボットxをx + 1に変更する前にx + 1、yを渡すことができるかどうかを返します。 – softwarenewbie7331
なぜ障害物と火星グリッドの部分ですか?ローバーオブジェクトの?これらは3つの異なるもののように見えます。 –